Vim
is my preferred text editor when I program, and thus I always run into a particularly annoying issue.
Frequently, when I quickly need to save the buffer and continue on to some other miscellaneous task, I do the typical
:w
However, I – what seems to be like more than 50% of the time – always manage to capitalise that :w
. Naturally, vim yells at me because W
is an invalid command
E492: Not an editor command: W
My question is how can one alias colon-commands in vim. Particularly, could you exemplify how to alias W
to w
.
I am aware of the process to map keys to certain commands. Unfortunately, that is not what I'm looking for.
I find that mapping the
;
key to:
would be a better solution, and would make you more productive for typing other commands.Suppose you want to add alias for tabnew command in gvim. you can simply type the following command in your .vimrc file (if not in home folder than create one)
To leave completion untouched, try using
, it will replace
W
in command line withw
, but only if it is neither followed nor preceded by word character, so:W<CR>
will be replaced with:w<CR>
, but:Write
won't. (Note that this affects any commands that match including ones which you might not expect, for example the command:saveas W Z
will be replaced by:saveas w Z
, so be careful with this.)Update
Here is how I would write it now:
As a function:
This checks that the command type is
:
and the command isW
, so it's safer than justcnoreabbrev W w
.The best solution involves writing a custom function for handling abbreviations that only take place in the beginning of the command bar.
For this, add the following your vimrc file or anywhere else.
A few useful abbreviations from the source material where I found this stuff:
Safest and easiest is a plugin such as cmdalias.vim or my recent update vim-alias of it that take into account
:sil(ent)(!)
or:redi(r)
,'<,'>
for the current visual selection,With supplementary searching, I've found that someone asked nearly the same question as I.
will do the trick.
Please be aware that, as Richo points out, the user command must begin with a capital letter.