vim change :x function to delete buffer instead of save & quit

眉间皱痕 提交于 2019-11-26 01:54:45

问题


I want to set :x in vim gui-mode to delete buffer because I always kill the whole gvim, which is kind of annoying. I know i can specifically set gui problems with if has(\"gui running\") but don\'t know how to remap :x

thanks in advance

ps.: maybe the tag/term remap is wrong but I don\'t know the correct term, that\'s why google didn\'t provide any help at all.


回答1:


I find the safest alternative is to use an expression abbreviation:

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

This will ensure the abbreviation will only be expanded to bd when :x is used otherwise just expand to x.

For more help:

:h map-<expr>
:h getcmdtype()
:h getcmdline()

Upon further inspection there appears to be a plugin that does exactly this by Hari Krishna Dara called cmdalias.vim. It uses a variation of the technique above.




回答2:


This is not as easy as it looks. :map won't work with commands and :command only accepts commands that start with an uppercase letter. But you can use :cabbrev:

if has("gui_running")
  cabbrev x bd
endif

UPDATE: :cmap could actually be used: :cmap x bd, but it doesn't work right: each occurrence of x in a command is immediately replaced by bd.

EDIT: This question is a duplicate of Can I (re)map Ex commands in vim?.



来源:https://stackoverflow.com/questions/7513380/vim-change-x-function-to-delete-buffer-instead-of-save-quit

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