问题
Vim newbie here.
when I ':wq' I want it to save and delete the buffer instead of save and quit. Similarly hitting ':q' would execute ':bd' instead.
Any suggestions? Thanks!
回答1:
Vim allows to add key mappings for commands in all of the modes including command line mode, so you can define these mappings (in your .vimrc
):
:cnoreabbrev wq w<bar>bd
:cnoreabbrev q bd
The commands tell Vim that pressing wq in command line must be expanded into w|bd
and, similarly, q into bd
. See :help key-mapping
for more details.
回答2:
Check out the Sayonara plugin.
I tried the other answer but it was missing a use case. When I close the last buffer, I want it to close Vim, like a regular :q
command. :bd
will just leave you with a blank window and no buffer.
Here's the mapping, adapted from the other answer:
" Prevent accidental closing of all buffers when doing :wq or :q
cnoreabbrev wq w<bar>Sayonara
cnoreabbrev q Sayonara
来源:https://stackoverflow.com/questions/4115841/remapping-wq-to-save-and-close-buffer-instead-of-save-and-quit