Remapping 'wq' to save and close buffer instead of save and quit

*爱你&永不变心* 提交于 2019-12-08 16:15:45

问题


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

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