Can I (re)map Ex commands in vim?

Vim

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

Alternative way to do it:

Use 'command abbreviations'

:ca WQ wq

Solution 3 - Vim

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 h getcmdtype() == ":" && getcmdline() == "h" ? "tab h" : "h"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioninnaMView Question on Stackoverflow
Solution 1 - VimWMRView Answer on Stackoverflow
Solution 2 - VimKent FredricView Answer on Stackoverflow
Solution 3 - VimZsolt BotykaiView Answer on Stackoverflow