Is it possible to remap an Ex command in Vim (remap :Ack to :ack)?

爷,独闯天下 提交于 2020-01-24 05:24:26

问题


I use the Vim plugin ack.vim, but I don't understand why the command is :Ack with a capital "A" (a little annoying to hold shift).

Is it possible to remap this to :ack?


回答1:


Built-in commands start with a lowercase character and custom commands start with an uppercase character. Those are the rules. Vim simply won't let you define a custom command starting with a lowercase.

If you don't like it, nothing prevents you from creating a normal mode mapping:

nnoremap <leader>a :Ack<Space>

which is even faster than :ack<Space>.




回答2:


Yes, but It is not as simple as it sounds:

cnoreabbrev <expr> ack getcmdtype() == ':' && getcmdline() ==# 'ack' ? 'Ack' : 'ack'

Long story short vim does not provide a native way to create lowercase commands. Using mappings causes delays so abbreviations are preferred. The trick is to be careful when the abbreviation should expand as cabbrev's expand at other times than just ex commands and in other places e.g. search. Here is another thread talking about this point.

Hari Krishna Dara created a plugin: cmdalias.vim. It uses a variation of the technique above



来源:https://stackoverflow.com/questions/16821907/is-it-possible-to-remap-an-ex-command-in-vim-remap-ack-to-ack

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