Automatically quit vim if NERDTree is last and only buffer
Asked Answered
L

5

61

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 edits, and hit :q on the buffer, and I'm left with . . . NERDTree. This is annoying.

I could use :qa to close all buffers, and exit vim, but I'm used to the :q trope. So I'm wondering if there's a way to detect that the only remaining buffer is NERDTree, and "unify" the two buffers, for purposes of :q

Edit

Ask and ye shall receive: https://github.com/scrooloose/nerdtree/issues#issue/21

Lavern answered 14/1, 2010 at 18:37 Comment(1)
There correct URL is github.com/preservim/nerdtree/issues/21Protuberance
N
112

A script to do exactly this has been posted on the NERDTree issue list. Checkout issue-21 on GitHub for nerdtree.

This leads to the single line command for your vimrc here:

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
Nidia answered 30/11, 2010 at 22:5 Comment(6)
Thanks, that script is precisely what I was looking for. You should probably get the check, but alas, it's too late . . . you'll have to make do with an upvote.Lavern
It means you can't do vim . now though, which is a bit annoying (just insta-exits now).Henebry
This answer no longer works for me with Vim version 8.0.562 on Fedora. But scrooloose's older answer is working: autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endifSurbased
Worked for me on Mac OS X High Sierra (10.13.3 - 17D47), thanks! vim --version: VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jul 26 2017 19:10:24) Included patches: 1-503, 505-642Lintel
I've been using this since, well let's say 2018; it's stopped working for about a year now.. not sure what went wrong; given vimscript has been updated, probably this needs an update..Summerwood
Same here. The answer I'm using is THIS. But I'll say this switching about will become tiresome in short order. Hopefully, the authors have found a LTS spot.Paddle
O
15
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 a version from janus repo.

Enhancements: also close if only a quickfix window is left. It uses the BufEnter autocommand instead, which is required for &bt to work properly.

Omnirange answered 23/3, 2011 at 10:30 Comment(0)
F
1

An idea in need of implementation:

You could write a function which, when called, checks if the only buffer remaining (or perhaps the only non-help buffer, if you prefer) is a NERDTree buffer and, if so, deletes it (or just quits).

Then have an autocmd run it whenever a buffer is deleted / hidden / whatever actually happens when you :q (it shames me to admit I'm not entirely sure!).

Fredkin answered 14/1, 2010 at 18:54 Comment(1)
Great! I see it is your answer that your link points at – very cool. (Just to be clear, though, that answer is timestamped 13 months after this one, which I posted 17 minutes after the OP first posted their question on SO over four and a half years ago, so I'm going to treat this one as part of the historical record and leave it here.)Adiabatic
A
0

You could :cabbrv q qa but I'd advise against that because you'll forget about it when you actually want q.

Amatruda answered 14/1, 2010 at 20:17 Comment(0)
C
0

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

Clockmaker answered 15/1, 2010 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.