4

I would like to change the :x command in Vim so that it closes a buffer unless it is the last buffer then it should behave as it does now (i.e close vim).

I've read some stuff here but it doesn't go all the way. I also use NERDTree and would like that to be ignored when considering if it's the last buffer.

I have a partially working solution based on the link I referred to but this makes it impossible to exit vim using a vim command (becase the exit command has been remapped).

I have

cnoreabbrev <expr> x getcmdtype() == ":" && getcmdline() == 'x' ? 'w<bar>bd' : 'x'
cnoreabbrev <expr> wq getcmdtype() == ":" && getcmdline() == 'wq' ? 'w<bar>bd' : 'wq'
cnoreabbrev <expr> q getcmdtype() == ":" && getcmdline() == 'q' ? 'bd' : 'q'

So :x (or :wq) will save and close the current buffer and :q will just close it.

What I would like to add is... If that buffer is also the last buffer (ignoring NERDTree) then it will also exit vim.

Is this possible ?

Community
  • 1
  • 1
starfry
  • 9,273
  • 7
  • 66
  • 96

1 Answers1

0

Answer from Automatically quit vim if NERDTree is last and only buffer works:

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
Community
  • 1
  • 1
Steve McKinney
  • 3,183
  • 1
  • 23
  • 26