Remap vim :x to close buffer, only exiting if it is the last buffer

自闭症网瘾萝莉.ら 提交于 2019-12-23 15:28:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!