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

5 Answers5

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
DankCoder
  • 369
  • 1
  • 4
  • 15
Andrew
  • 1,236
  • 1
  • 9
  • 5
  • 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. –  Dec 29 '10 at 16:22
  • It means you can't do `vim .` now though, which is a bit annoying (just insta-exits now). – alex Mar 08 '16 at 10:27
  • 8
    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 | endif – MrD Apr 24 '17 at 17:27
  • 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-642 – herrera Feb 12 '18 at 17:23
  • 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.. – stucash Dec 23 '22 at 16:00
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.

blueyed
  • 27,102
  • 4
  • 75
  • 71
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!).

Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
  • 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.) – Michał Marczyk Sep 04 '14 at 07:48
0

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

Randy Morris
  • 39,631
  • 8
  • 69
  • 76
0

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

Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58