181

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 always — what seems to be like more than 50% of the time — manage to capitalize 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, but that is not what I’m looking for.

ib.
29.2k13 gold badges87 silver badges107 bronze badges
asked Oct 7, 2010 at 4:31
3
  • 1
    possible duplicate of Can I (re-) map commands in vim? Commented May 23, 2012 at 13:46
  • 1
    To avoid :W you could a map a key to perform the saving. If you are used to some program that saves with Ctrl-s, there are these mappings from $VIM/mswin.vim: " Use CTRL-S for saving, also in Insert mode noremap <C-S> :update<CR> vnoremap <C-S> <C-C>:update<CR> inoremap <C-S> <C-O>:update<CR> Commented May 23, 2012 at 14:21
  • Similar question on Vi Stack exchange: vi.stackexchange.com/q/2665/7244 Commented Aug 24, 2018 at 12:23

8 Answers 8

161

To leave completion untouched, try using

cnoreabbrev W w

It will replace W in command line with w, 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 that 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:

cnoreabbrev <expr> W ((getcmdtype() is# ':' && getcmdline() is# 'W')?('w'):('W'))

As a function:

fun! SetupCommandAlias(from, to)
 exec 'cnoreabbrev <expr> '.a:from
 \ .' ((getcmdtype() is# ":" && getcmdline() is# "'.a:from.'")'
 \ .'? ("'.a:to.'") : ("'.a:from.'"))'
endfun
call SetupCommandAlias("W","w")

This checks that the command type is : and the command is W, so it’s safer than just cnoreabbrev W w.

ib.
29.2k13 gold badges87 silver badges107 bronze badges
answered Oct 7, 2010 at 8:03
17
  • 3
    This answer is the safest and most reliable for me. Commented Oct 13, 2010 at 1:57
  • 2
    If you use the recommended solution, please, be aware both of the two below commands will work as the lower one which may present an unexpected result depending on the actual buffer content and VIM settings: :%s/W/foo/g<CR> :%s/w/foo/g<CR> Commented Apr 25, 2012 at 2:00
  • 2
    Actually, this would mean W will be replaced anywhere in the command bar, including, for example, in searches, so s/W foo/bar/g would be turned into s/w foo/bar/g. this can get annoying really fast. see my answer for a comprehensive solution. Commented May 22, 2012 at 19:17
  • 4
    Absolutely; this is a horrible idea. You should never, ever, ever do this. Commented May 23, 2012 at 13:43
  • 4
    :cnoreabbrev <expr> W getcmdtype()==':'&&getcmdline()=~#'^W'?'w':'W' Commented Jul 27, 2012 at 13:32
121

With supplementary searching, I've found that someone asked nearly the same question as I.

:command <AliasName> <string of command to be aliased>

will do the trick.

Please be aware that, as Richo points out, the user command must begin with a capital letter.

answered Oct 7, 2010 at 4:34
5
  • 6
    Using :command is good solution. :cnoreabbrev doesn't understand cmd1|cmd2, :command does. Commented Apr 14, 2011 at 0:01
  • 1
    A less confusing way to write this is, :command AliasName string of command to be aliased Commented Aug 21, 2013 at 16:36
  • 9
    This won't handle/forward any command arguments, like -nargs, -complete etc. Commented Feb 11, 2014 at 10:21
  • What about :Q! or :W!? Commented Jul 31, 2014 at 18:56
  • 14
    Just to be very literal: put :command W w in the .vimrc file. Commented Sep 25, 2014 at 19:54
28

I find that mapping the ; key to : would be a better solution, and would make you more productive for typing other commands.

nnoremap ; :
vnoremap ; :
answered Feb 13, 2014 at 17:50
7
  • 3
    This is the single best tip for vim. I'm so used to it now that every time I encounter the normal behavior, it takes me a few tried to get my mind retrained. Commented Feb 24, 2015 at 19:58
  • 4
    This is not an answer to the question. Commented Aug 24, 2018 at 11:45
  • 13
    @Flimm No, but it makes OP's issue go away. Commented Sep 24, 2018 at 18:02
  • 5
    When using t or f, one can usually use ; to go to the next occurrence. One can safely map that the other way, even if you mapped semicolon to colon. There won't be an alias loop. nnoremap : ; Commented Mar 5, 2019 at 8:27
  • 2
    @EricDuminil If you go from : to ;, then you're no longer using shift when typing ;w and therefore won't accidentally type W instead of w. Commented Mar 10, 2024 at 8:10
13

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.

" cabs - less stupidity {{{
fu! Single_quote(str)
 return "'" . substitute(copy(a:str), "'", "''", 'g') . "'"
endfu
fu! Cabbrev(key, value)
 exe printf('cabbrev <expr> %s (getcmdtype() == ":" && getcmdpos() <= %d) ? %s : %s',
 \ a:key, 1+len(a:key), Single_quote(a:value), Single_quote(a:key))
endfu
"}}}
" use this custom function for cabbrevations. This makes sure that they only
" apply in the beginning of a command. Else we might end up with stuff like
" :%s/\vfoo/\v/\vbar/
" if we happen to move backwards in the pattern.
" For example:
call Cabbrev('W', 'w')

A few useful abbreviations from the source material where I found this stuff:

call Cabbrev('/', '/\v')
call Cabbrev('?', '?\v')
call Cabbrev('s/', 's/\v')
call Cabbrev('%s/', '%s/\v')
call Cabbrev('s#', 's#\v')
call Cabbrev('%s#', '%s#\v')
call Cabbrev('s@', 's@\v')
call Cabbrev('%s@', '%s@\v')
call Cabbrev('s!', 's!\v')
call Cabbrev('%s!', '%s!\v')
call Cabbrev('s%', 's%\v')
call Cabbrev('%s%', '%s%\v')
call Cabbrev("'<,'>s/", "'<,'>s/\v")
call Cabbrev("'<,'>s#", "'<,'>s#\v")
call Cabbrev("'<,'>s@", "'<,'>s@\v")
call Cabbrev("'<,'>s!", "'<,'>s!\v")
jdhao
29.2k23 gold badges158 silver badges323 bronze badges
answered May 22, 2012 at 19:22
2
  • 3
    There is a built-in function string() that does the same thing as yours Single_quote(). Commented May 23, 2012 at 0:57
  • But don't you have to type <c-]>, space, or some other non-keyword character after abbreviations? So I'd end up typing :s/<c-]> or :s/ <bs>, which hardly seems faster. Commented May 23 at 23:38
7

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)

cabbrev t tabnew
Andrew Barber
40.2k20 gold badges97 silver badges124 bronze badges
answered Feb 27, 2013 at 3:46
1
  • 3
    This will cause a command like :saveas t example to be replaced with :saveas tabnew example Commented Aug 24, 2018 at 11:56
5

Maybe you would like to map one of your function keys (F1..F12) to :w ? Then put this into your .vimrc:

noremap <f1> :w<return>
inoremap <f1> <c-o>:w<return>

(ctrl-o in insert mode switches temporarily to normal mode).

answered Oct 7, 2010 at 6:36
0
5

Safest and easiest is a plugin such as cmdalias.vim or my recent update vim-alias of it that take into account

  • preceding blanks or modifiers such as :sil(ent)(!) or :redi(r),
  • range modifiers such as '<,'> for the current visual selection,
  • escape special characters such as quotes, and
  • check if the chosen alias is a valid command line abbreviation.
answered Apr 11, 2016 at 17:45
1

I think @ZyX's answer is great, but if you're using a newer version of neovim (0.5+), you might want to define the function using lua instead. Here's one way you could do it:

function _G.abbreviate_or_noop(input, output)
 local cmdtype = vim.fn.getcmdtype()
 local cmdline = vim.fn.getcmdline()
 if (cmdtype == ":" and cmdline == input) then 
 return output
 else
 return input
 end
end
function SetupCommandAlias(input, output)
 vim.api.nvim_command("cabbrev <expr> " .. input .. " " .. "v:lua.abbreviate_or_noop('" .. input .. "', '" .. output .. "')")
end

Then, you'd drop the call from call SetupCommandAlias("pg", "postgres://") and just use the function like this: SetupCommandAlias("pg", "postgres://").

n.b. If using it from a .vim file instead of a .lua file, you'd need to prefix the function call with lua, i.e. lua SetupCommandAlias("pg", "postgres://").

answered Nov 13, 2021 at 3:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.