Automatically terminate vim if last NERDTree and only buffer

I have the following in my .vimrc:

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Open NERDTree by default """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" autocmd VimEnter * NERDTree autocmd VimEnter * wincmd p 

So,

 % vim file.txt 

opens NERDTree and focuses the cursor in the file.txt buffer. I make my changes and hit: q into the buffer and I stayed., NERDTree. This is annoying.

I could use: qa to close all buffers and exit vim, but I'm used to: q trope. Therefore, I wonder if there is a way to detect that the only remaining buffer is NERDTree and to β€œunify” two buffers for purposes: q

Edit

Ask and you will receive: https://github.com/scrooloose/nerdtree/issues#issue/21

+41
vim nerdtree
Jan 14 '10 at 18:37
source share
5 answers

A script to do just that was posted on the NERDTree problem list. Checkout issue-21 on GitHub for nerdtree.

This results in a single line command for your vimrc here :

 autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif 
+64
Nov 30 '10 at 22:05
source share
 function! s:CloseIfOnlyControlWinLeft() if winnr("$") != 1 return endif if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1) \ || &buftype == 'quickfix' q endif endfunction augroup CloseIfOnlyControlWinLeft au! au BufEnter * call s:CloseIfOnlyControlWinLeft() augroup END 

From my vimrc based on janus repo version.

Improvements: also close if only the quick delete window remains. Instead, it uses the BufEnter auto BufEnter , which is required for &bt to work properly.

+11
Mar 23 '11 at 10:30
source share

An idea that needs to be implemented:

You can write a function that, when called, checks to see if the only remaining buffer (or perhaps the only buffer left without help) is the NERDTree buffer and, if so, deletes it (or simply shuts down).

Then run autocmd when the buffer is deleted / hidden / what actually happens when you: q (I am ashamed to admit that I'm not quite sure!).

+1
Jan 14
source share

You could :cabbrv q qa , but I would recommend it because you forget about it when you want q .

0
Jan 14 '10 at 20:17
source share

I like to do this: cmap bq :bufdo q<CR> to close all buffers with two keystrokes in command mode.

0
Jan 15 '10 at 3:19
source share



All Articles