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:
function! NERDTreeQuit()
redir => buffersoutput
silent buffers
redir END
" 1BufNo 2Mods. 3File 4LineNo
let pattern = '^\s*\(\d\+\)\(.....\) "\(.*\)"\s\+line \(\d\+\)$'
let windowfound = 0
for bline in split(buffersoutput, "\n")
let m = matchlist(bline, pattern)
if (len(m) > 0)
if (m[2] =~ '..a..')
let windowfound = 1
endif
endif
endfor
if (!windowfound)
quitall
endif
endfunction
autocmd WinEnter * call NERDTreeQuit()
Greetings
Michael
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!
Most helpful comment
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