nerdtree icon indicating copy to clipboard operation
nerdtree copied to clipboard

Error detected while processing BufEnter Autocommands for "*" with the "Close the tab if NERDTree is the only window remaining in it." config

Open lafrech opened this issue 4 months ago • 6 comments

Environment

Nope, I removed all plugins.

  • Provide a minimal .vimrc file that will reproduce the issue.
" Close the tab if NERDTree is the only window remaining in it.
autocmd BufEnter * if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif

Steps to Reproduce the Issue

Open a file, open NERDTree, open another file, open NERDTree, close a file.

Current Behavior (Include screenshots where appropriate.)

I get this error message

Error detected while processing BufEnter Autocommands for "*":

Press ENTER or type command to continue

and NERDTree is still open.

Expected Result

Tab closes.


This reminds me of https://github.com/jistr/vim-nerdtree-tabs/issues/102.

I'm trying to get rid of vim-nerdtree-tabs because it is unmaintained and because I realized what I'm using it for should be achievable using the sample config lines from NERDTree README.

I'm surprised I'm getting that same error.

I removed all plugins and kept that single line in the config file, which makes me wonder how come no one else is affected. I don't see anything specific in my setup.

lafrech avatar Mar 06 '24 10:03 lafrech

Hi, I've tried to reproduce the issue on my machine with no result. Among things that can be different between our setups, the version of Vim and NERDTree seems most relevant to me. I'm currently using neovim, But earlier versions of Vim9 had some rough edges that got patched out in subsequent releases. Could you update both your Vim and NERDTree?

In case there are any more details in the error message you are receiving please send them here. Try using the :messages command to see if there is any error that gets flushed before you get this message.

rzvxa avatar Mar 06 '24 10:03 rzvxa

Thank you for your answer.

I thought I was using latest NERDTree version. Perhaps an issue with scroolose vs. preservim. Anyway I updated to latest NERDTree and I'm still facing this issue.

I'm using vi from Debian repo. I shall try to see if I can install from testing or any other repo without screwing up my system.

I can also try neovim 0.7.2 but that's pretty old.

Here's what I get from the :messages command:

Error detected while processing BufEnter Autocommands for "*": E1312: Not allowed to change the window layout in this autocmd

Searching with that latest line, I found @mb6ockatf's #1354. Same issue, closed just after opening.

lafrech avatar Mar 06 '24 10:03 lafrech

Ok, I found the issue, It took a bit of digging but it seems like Vim9 from this patch onward won't allow changing the layout which is a breaking change. Since this snippet was from a FAQ section most people probably didn't use it and that's why it went unnoticed for more than 2 years. Neovim being backward compatible also didn't help it.

The one time that it came up the author of the issue closed it which is most probably because they found out about this change and used a new workaround for their case.

According to this mailing list thread you can use call feedkeys(":quit\<CR>:\<BS>") to achieve what quit command did before in the context of autocommands. I've only tested it on neovim since I don't have Vim9 on this machine, Can you confirm that this would also work on Vim9?

" Close the tab if NERDTree is the only window remaining in it.
autocmd BufEnter * if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | call feedkeys(":quit\<CR>:\<BS>") | endif

rzvxa avatar Mar 06 '24 11:03 rzvxa

Works a treat! Thank you so much!

This was frustrating and I'm totally illiterate when it comes to Vim scripts.

Indeed, that ML thread was referred to in the issue I linked to in OP.

Thanks again for sorting this out.

(Still wondering how people live without this config line. Not saying it should be default. Just surprised the issue didn't come up earlier.)

lafrech avatar Mar 06 '24 11:03 lafrech

That's great to hear! I'll keep this issue open as a reminder so we go through the FAQ snippets and update them. I'm not sure how far back the feedkeys function goes, I've never used it in my code so it has to wait until we figure out the compatibility side of things.

rzvxa avatar Mar 06 '24 11:03 rzvxa

OK, I'll be using this on a daily basis from now on and I'll be sure to report back if anything happens (which I'm sure won't happen). Thanks again.

lafrech avatar Mar 06 '24 13:03 lafrech