Closed michaelkebe closed 12 years ago
thanks! this is exactly what I was looking for!
(if you have a StackOverflow account, you may want to link this here)
A better version appears to be the one used in the janus repo:
autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft()
" Close all open buffers on entering a window if the only
" buffer that's left is the NERDTree buffer
function! s:CloseIfOnlyNerdTreeLeft()
if exists("t:NERDTreeBufName")
if bufwinnr(t:NERDTreeBufName) != -1
if winnr("$") == 1
q
endif
endif
endif
endfunction
It's perfect thanks you :)
Hey guys
This is currently not something that im keen to put in the core plugin, but you can stick something like this in your vimrc to achieve the same thing:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
excellent
Thanks! Been looking for this for some time, got it from here; http://stackoverflow.com/questions/2066590/automatically-quit-vim-if-nerdtree-is-last-and-only-buffer
The solutions above don't seem to account for buffers that are hidden. They close out of vim with hidden buffers still present. Is there something I'm missing?
Thanks! This really helps me.
thanks,it works!
O joy! scrooloose's one liner works!
For anyone coming here from Stack Overflow, the one liner above no longer works. The README now says to use this:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
michaelkebe's is also not working - It also closes vim when I try to switch to the nerdtree window with Ctrl-w p
@justonia thanks !! it is wonderful . I am the guy come from stackoverflow. By the way , I just want to ask why this old code no work ? cause I upgrade the vim ? My current vim version is 7.4 ; os is MAC OSX
//old code no working
autocmd bufenter * if (winnr("$") == 1 && exists('b:NERDTreeType') &&b:NERDTreeType == 'primary') | q | endif
Any how. Thanks you very much!
@justonia thank you!
Thank you! Worked great!
Checking on NERDTreeType no longer works after an update. Now only @justonia 's answer works.
I tried @justonia 's answer, and find it normally work if default open with tree view.
If default open without tree view, and type :NERDTreeCWD
or :NERDTreeToggle
or ctrl+n
, then still need :q
twice.
Any idea?
Hi.
It seems the script in github page no longer work.
I have tried for times but it doesn't work for me when I want to quit vim with nerdtree there.
Exactly this script:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
My vim version is 8.0 and I have installed NERDTree from Fedora repo.
Any way, How can I solve this problem guys?
I'm afraid I can't replicate your problem. The above snippet does exactly what it claims to do in my Vim installation.
What other customizations are you using? In what script are you running this snippet?
Sorry guys. I tried several times again with other scripts and finally it seems my problem solved with this script: `autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft()
" Close all open buffers on entering a window if the only " buffer that's left is the NERDTree buffer function! s:CloseIfOnlyNerdTreeLeft() if exists("t:NERDTreeBufName") if bufwinnr(t:NERDTreeBufName) != -1 if winnr("$") == 1 q endif endif endif endfunction But thanks anyway. ;)
I am also having an issue with the following script:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
Running Vim 8, ZSH, and iterm on OSX. :q will freeze the window. If I instead :w and then :qall, vim closes everything as expected. It only freezes when I have opened vim with a specific file.
@jcconnell Suspect that you might have some config/plugin that opens buffers in the background (hidden). Maybe posting the whole vimrc file will help to diagnose it
@texasbruce, thank you! Here is my vimrc:
Still can't figure this out. Any ideas?
@jcconnell Try adding these in the front of the whole .vimrc file:
set nocompatible " be iMproved, required
filetype off " required
and settings for NERDtree:
autocmd vimenter * NERDTree | wincmd l
autocmd bufenter * if (!exists("t:NERDTreeBufName") ) | silent NERDTreeMirror | wincmd l | endif
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
@texasbruce thank you!
I tried these settings but I'm still experiencing the same problem. I was able to make a little progress but removing the ! from the following line:
autocmd bufenter * if (!exists("t:NERDTreeBufName") ) | silent NERDTreeMirror | wincmd l | endif
The currently open file buffer closes, but the NERDTree buffer remains open and the whole pane freezes.
@jcconnell Have you tried to disable other plugins and see if this works?
Yes, I've tried disabling all plugins one by one and I'm still having the same problem.
@justonia your answer is work for me. but i use deoplete in neovim, now when i quit vim with a nerdtree opened, deoplete will launch 4 python progress and take 100% cpu.
@alexsunxl thank you so much! your solution works on neovim.
Variant with autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
doesn't work with Arch Linux 64-bit, VIM 8.1. However, older solution autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
works fine.
Any ideas?
@dvdesolve It looks like you have a very old version of NERDTree installed. The b:NERDTreeType
variable isn't used anymore. Can you find out what version you're using? Here are some ways to find out.
?
key. It should display the version number at the top of the buffer.git rev-parse HEAD
git remote -v
Method 1 gives 4.2.0
. However, Arch Linux package is vim-nerdtree 5.0.0
In vim, type :set rtp?
to see where NERDTree is installed. You'll have to search through the string to find the NERDTree path portion. Go to that folder and see if step 2 above works. I suspect it won't, and that an ancient NERDTree is bundled with ArchLinux.
Update to the latest NERDTree solved the problem. Thank you!
And so... after all of the back & forth, I'm confused. What is it exactly I need in my .vimrc file on a Linux machine running vim ver 8.2?
@seamusdemora I believe that the thing you are looking for has been addressed in this section of FAQ.
If you need any more help with your case, feel free to create a new issue for it.
@rzvxa : Thanks - I missed the FAQ when I looked earlier. Very handy collection!
the autocmd condition in all above scripts better be changed from BufEnter to SafeState, otherwise it won't work on newer versions of vim with mutiple tabs. See this thread for the discussion: https://groups.google.com/g/vim_dev/c/Cw8McBH6DDM
@SleepyToDeath Thanks for bringing it up, I have to admit it is a really good addition; Sadly it isn't still supported in the neovim
and it will also break the backward compatibility. It is possible to bind the command to a condition so we use the SafeState
if it exists otherwise fallback to the BufEnter
but I'm not sure if it is really necessary.
In the NERDTree itself, there are only 2 occurrences of BufEnter
, Both of which are in the initial setup of the plugin. One of these 2 is used for hijacking netrw, which I personally am not a fan of since I use both NERDTree and netrw. So it all boils down to only one autocmd
listening to the BufEnter
event which prevents race conditions that would break the plugin.
As for the FAQ
section in the README
, There are just there to show some useful tricks for configuring the plugin. While people may use them as it is, It shouldn't be considered as a part of the plugin and just some configuration examples. But it would certainly be nice if we added more detailed answers with more information about both modern Vim features and also examples written in Lua for Neovim users.
Feel free to contribute to any of these 2 areas. I personally think only adding examples with SafeState to FAQ is enough but I wouldn't object if you also change the plugin initialization behavior to get rid of BufEnter in environments where SafeState exists.
@rzvxa
@seamusdemora I believe that the thing you are looking for has been addressed in this section of FAQ.
If you need any more help with your case, feel free to create a new issue for it.
Sorry to be the pest, but the FAQ doesn't mention where (i.e. which file) to put this... e.g. in ~/.vimrc
??
@seamusdemora No worries mate, Yes these are some common configurations and macros for NERDTree, You can copy-paste them into your vimrc to use them. Use them to write your own config the way it suits your needs. You may not need most of them or don't like the way some of them work, So only use the ones that help your case.
hello, i tried to use the Q&A method Or the plugin but i got this error when nerdtree is the last buffer in a tab
Executing BufEnter Autocommands for "*"
autocommand if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
autocommand if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
Error detected while processing BufEnter Autocommands for "*":
: Not allowed to change the window layout in this autocmd
Executing BufEnter Autocommands for "*"
could somebody help pls?
vim version 9.1.150
Hey @zhitongLIU, check the #1411 out. We have to update our FAQ section to reflect Vim9 specifications (in short, you cannot use the quit
command in an auto command anymore).
Hey @zhitongLIU, check the #1411 out. We have to update our FAQ section to reflect Vim9 specifications (in short, you cannot use the
quit
command in an auto command anymore).
yes work like a charm, thank you for quick responds!
It would be great if there is no need to quit the NERD_tree* buffers to quit Vim. Maybe this can be put into the NERDTree as an option?
I have created a small function to get this feature. Just put this code into your
.vimrc
:Greetings Michael