74

I love vim and the speed it gives me. But sometimes, my fingers are too speedy and I find myself typing :WQ instead of :wq. (On a German keyboard, you have to press Shift to get the colon :.) Vim will then complain that WQ is Not an editor command.

Is there some way to make W and Q editor commands?

Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
innaM
  • 47,505
  • 4
  • 67
  • 87

3 Answers3

85

Try

 :command WQ wq
 :command Wq wq
 :command W w
 :command Q q

This way you can define your own commands. See :help command for more information.

WMR
  • 12,661
  • 5
  • 35
  • 30
  • 6
    don't forget to add the above to your vimrc so you don't have to do it every time. – rampion Sep 22 '08 at 22:43
  • 13
    Note that in .vimrc, you need to omit the leading colon. It should read, for example: `command Wq wq` – Luc Jul 07 '14 at 20:33
  • 1
    This solution is limited to a command names that begin with capitol letters. A much better solution can be found [here](http://stackoverflow.com/a/3879737/1519199). – Jthorpe Mar 28 '16 at 17:42
  • This does not let you use arguments, for instance, `:W example.txt` will fail to work if you set up the alias using `:command W w` – Flimm Aug 24 '18 at 12:24
  • 1
    Using vim `8.0.1453`, in my `.vimrc`, I had to use `cmap WQ wq`, not "command". Vim gave me `E128: Invalid command name` otherwise. – Kingsley Oct 10 '18 at 23:50
39

Alternative way to do it:

Use 'command abbreviations'

:ca WQ wq
Von
  • 4,365
  • 2
  • 29
  • 29
Kent Fredric
  • 56,416
  • 14
  • 107
  • 150
  • 2
    A helpful way to remember that: The full keyword is `cabbrev`. :) – Ben Klein Mar 15 '14 at 03:51
  • 2
    +1 This also allows for aliases starting with a lower case letter (in contrast to `:command`). – Matthias Braun Jun 04 '14 at 16:07
  • 3
    This solution will replace `WQ` where ever it is in the command, not just at the beginning, which might be rare for 'WQ' but nor for more commonly typed strings. Hence, [this solution](http://stackoverflow.com/a/3879737/1519199) is better. – Jthorpe Mar 28 '16 at 17:45
17

And you can use

:cmap WQ wq

as well. E.g. I have

cmap h tab help

in my .vimrc which means opening help pages in a new tab.

Thanks for the tip Jim Stewart:

But here is a much better solution as the above (for the help mapping, so that it only applies when you do :h):

cnoreabbrev <expr> h getcmdtype() == ":" && getcmdline() == "h" ? "tab h" : "h"
Community
  • 1
  • 1
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110