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?
3 Answers 3
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.
-
7don't forget to add the above to your vimrc so you don't have to do it every time.rampion– rampion2008年09月22日 22:43:19 +00:00Commented Sep 22, 2008 at 22:43
-
15Note that in .vimrc, you need to omit the leading colon. It should read, for example:
command Wq wq
Luc– Luc2014年07月07日 20:33:20 +00:00Commented Jul 7, 2014 at 20:33 -
1
-
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– Flimm2018年08月24日 12:24:42 +00:00Commented Aug 24, 2018 at 12:24 -
1Using vim
8.0.1453
, in my.vimrc
, I had to usecmap WQ wq
, not "command". Vim gave meE128: Invalid command name
otherwise.Kingsley– Kingsley2018年10月10日 23:50:30 +00:00Commented Oct 10, 2018 at 23:50
-
2A helpful way to remember that: The full keyword is
cabbrev
. :)Ben Klein– Ben Klein2014年03月15日 03:51:46 +00:00Commented Mar 15, 2014 at 3:51 -
2+1 This also allows for aliases starting with a lower case letter (in contrast to
:command
).Matthias Braun– Matthias Braun2014年06月04日 16:07:35 +00:00Commented Jun 4, 2014 at 16:07 -
3This 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 is better.Jthorpe– Jthorpe2016年03月28日 17:45:05 +00:00Commented Mar 28, 2016 at 17:45
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"
-
1On which version if (G)Vim? What happens, when you type '<ESC>:cmap h tab help<CR>:h<SPACE>'?Zsolt Botykai– Zsolt Botykai2010年06月29日 10:03:57 +00:00Commented Jun 29, 2010 at 10:03
-
6-1: touches far too much. For example,
:echo 3
will then produce:ectab helpo 3
.:e sh.py
becomes:e stab help.py
. Et cetera.Chris Morgan– Chris Morgan2011年09月22日 13:51:39 +00:00Commented Sep 22, 2011 at 13:51 -
4This is off-topic, but you probably want something like
cnoreabbrev <expr> h getcmdtype() == ":" && getcmdline() == "h" ? "tab h" : "h"
for the help mapping, so that it only applies when you do:h<space>
.Jim Stewart– Jim Stewart2012年11月29日 05:38:42 +00:00Commented Nov 29, 2012 at 5:38 -
1Thank you @JimStewart it is a much better solution, I will add it to my answer!Zsolt Botykai– Zsolt Botykai2012年11月29日 07:46:24 +00:00Commented Nov 29, 2012 at 7:46
-
:cmap h tab help makes vim output tab all over the screen in a forever loop.trusktr– trusktr2013年04月08日 00:24:00 +00:00Commented Apr 8, 2013 at 0:24
;
back to:
!noremap ; :
noremap : ;
:P