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

2

11

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?

Joerg answered 31/3, 2012 at 0:47 Comment(4)
Do you need to remap colon? I just use nnoremap ; : and that's it.Lottery
@alberge: Using only the first one of those mappings, you shadow the ; Normal mode command. When the second mapping is added, the meanings of ; and : are swapped.Lepidote
Wow, how did I never think of using this mapping? I'm going to go add it to my .vimrc right now.Incalescent
Similar thought occured to me just now as well, decided to globally swap them since I use colon much more often :) unix.stackexchange.com/questions/615799/…Roulers
L
11

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.

Lepidote answered 31/3, 2012 at 13:10 Comment(0)
R
0

Try this plugin,In normal mode, use ; to enter the command line mode, and use ; when ft/FT jump is supported.

https://github.com/edte/normal-colon.nvim

Rickettsia answered 5/6 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.