Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap the semicolon and colon keys in Normal-mode mappings in Vim?

Tags:

vim

In an attempt to make myself use the shorter keystroke to get a colon, I have the following mappings defined in my .vimrc file:

noremap ; :
noremap : ;

However, this breakes some of my other mappings, since now it interprets a colon as a semicolon. For example, the mapping

map ,c :cd ~/code<CR>

becomes

map ,c ;cd ~/code<CR>

How can I fix this?

like image 779
butterywombat Avatar asked Mar 31 '12 00:03

butterywombat


1 Answers

The commands of the :map family interpret the characters in the mapping definition as if they were typed by the user, so that any of the currently defined mappings (including the one being defined) are triggered as usual. It is the reason why it becomes possible to define recursive or nested mappings when necessary. And this is also why the colon mapping gets applied to other mappings defined via :map, like the one in your example:

:map ,c :cd ~/code<cr>

To avoid this behavior, use the :noremap family of commands, which do not interpret any mappings in the right-hand side of a mapping definition (see :help :nore):

:noremap ,c :cd ~/code<cr>

In most cases, such interference with other mappings is an undesirable side effect. As a rule of thumb, I would recommend one to go by the following convention when defining a new mapping:

Always use the :noremap family of commands, unless there is a clear reason not to.

like image 116
ib. Avatar answered Nov 06 '22 20:11

ib.