Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 312
hobbes3 Avatar asked Dec 20 '22 05:12

hobbes3


2 Answers

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>.

like image 83
romainl Avatar answered Dec 28 '22 10:12

romainl


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

like image 39
Peter Rincker Avatar answered Dec 28 '22 08:12

Peter Rincker