问题
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 ?
回答1:
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
来源:https://stackoverflow.com/questions/9886576/remap-vim-x-to-close-buffer-only-exiting-if-it-is-the-last-buffer