Jay Taylor's notes
back to listing indexWhat is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim? - Stack Overflow
[web search]-
-
Home
-
Questions
-
Tags
-
Users
-
Companies
-
Labs
-
Jobs
-
Discussions
-
Communities for your favorite technologies. Explore all Collectives
-
-
Teams
Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat.
Learn more Explore Teams
What is the difference between the remap
, noremap
, nnoremap
and vnoremap
mapping commands in Vim?
-
18Caution,
vnoremap
andvmap
work in Visual AND Select mode. To have a mapping only in Visual mode, usexmap
andxnoremap
.– BenoitCommented Sep 24, 2010 at 13:58
5 Answers
remap
is an option that makes mappings work recursively. By default, it is on and I'd recommend you leave it that way. The rest are mapping commands, described below:
:map
and :noremap
are recursive and non-recursive versions of the various mapping commands. For example, if we run:
:map j gg (moves the cursor to the first line)
:map Q j (moves the cursor to the first line)
:noremap W j (moves the cursor down one line)
Then:
j
will be mapped togg
.Q
will also be mapped togg
, becausej
will be expanded for the recursive mapping.W
will be mapped toj
(and not togg
) becausej
will not be expanded for the non-recursive mapping.
Now remember that Vim is a modal editor. It has a normal mode, visual mode and other modes.
For each of these sets of mappings, there is a mapping that works in normal, visual, select and operator modes (:map
and :noremap
), one that works in normal mode (:nmap
and :nnoremap
), one in visual mode (:xmap
and :xnoremap
) and so on.
For more guidance on this, see:
:help :map
:help :noremap
:help recursive_mapping
:help :map-modes
-
16Thanks for your answer! Also, when is recursive used, and when is non-recursive used?– ChetanCommented Sep 29, 2010 at 22:35
-
17@Chetan: It depends what you want to achieve. I tend to use non-recursive more often, but if you've defined a relatively complicated mapping using non-recursive and what another mapping that does everything the first mapping does and more, it can be easier to use a recursive mapping that includes the original one rather than retyping the whole of the non-recursive one again (particularly if you then need to tweak the original one).– DrAlCommented Sep 30, 2010 at 7:02
-
15I had assumes
noremap
to be some opposite ofmap
. I mean something which removes a mapping. Thanks for the answer. It clarified me Commented Mar 14, 2012 at 9:07 -
6It isn't that important for the majority of use cases, but it should be noted that
:map
, etc. don't work in all modes, exactly, just all the common ones (specifically, normal mode, visual mode, select mode, and operator-pending mode). If you want a mapping to work in insert, command-line, or lang-arg mode, you need to use:map!
, etc. (Source: vimdoc.sourceforge.net/htmldoc/map.html#map-overview) Commented Jun 26, 2013 at 14:00 -
5
I think the Vim documentation should've explained the meaning behind the naming of these commands. Just telling you what they do doesn't help you remember the names.
map
is the "root" of all recursive mapping commands. The root form applies to "normal", "visual+select", and "operator-pending" modes. (I'm using the term "root" as in linguistics.)
noremap
is the "root" of all non-recursive mapping commands. The root form applies to the same modes as map
. (Think of the nore
prefix to mean "non-recursive".)
(Note that there are also the !
modes like map!
that apply to insert & command-line.)
See below for what "recursive" means in this context.
Prepending a mode letter like n
modify the modes the mapping works in. It can choose a subset of the list of applicable modes (e.g. only "visual"), or choose other modes that map
wouldn't apply to (e.g. "insert").
Use help map-modes
will show you a few tables that explain how to control which modes the mapping applies to.
Mode letters:
n
: normal onlyv
: visual and selecto
: operator-pendingx
: visual onlys
: select onlyi
: insertc
: command-linel
: insert, command-line, regexp-search (and others. Collectively called "Lang-Arg" pseudo-mode)
"Recursive" means that the mapping is expanded to a result, then the result is expanded to another result, and so on.
The expansion stops when one of these is true:
- the result is no longer mapped to anything else.
- a non-recursive mapping has been applied (i.e. the "noremap" [or one of its ilk] is the final expansion).
At that point, Vim's default "meaning" of the final result is applied/executed.
"Non-recursive" means the mapping is only expanded once, and that result is applied/executed.
Example:
nmap K H
nnoremap H G
nnoremap G gg
The above causes K
to expand to H
, then H
to expand to G
and stop. It stops because of the nnoremap
, which expands and stops immediately. The meaning of G
will be executed (i.e. "jump to last line"). At most one non-recursive mapping will ever be applied in an expansion chain (it would be the last expansion to happen).
The mapping of G
to gg
only applies if you press G
, but not if you press K
. This mapping doesn't affect pressing K
regardless of whether G
was mapped recursively or not, since it's line 2 that causes the expansion of K
to stop, so line 3 wouldn't be used.
-
6One thing:
map
only applies to normal, visual, select, and operator-pending modes, not to all modes. Commented Sep 20, 2014 at 3:36
I will explain mapping commands simply.
First, we have two general mapping commands:
map
- works recursively in normal, visual, select and operator pending modes.map!
- works recursively in insert and command-line modes.
The non-recursive variations of these commands are:
noremap
- works non-recursively in normal, visual, select and operator pending modes.noremap!
- works non-recursively in insert and command-line modes.
You can think of it as no[remap] {lhs} {rhs}
which means to map the key sequence {lhs}
to {rhs}
, but do not re-map any commands in {rhs}
to avoid nested and recursive mappings.
Then, we have mode-specific commands:
nmap
- works recursively in normal mode.imap
- works recursively in insert mode.vmap
- works recursively in visual and select modes.xmap
- works recursively in visual mode.smap
- works recursively in select mode.cmap
- works recursively in command-line mode.omap
- works recursively in operator pending mode.
And their non-recursive variations:
nnoremap
- works non-recursively in normal mode.inoremap
- works non-recursively in insert mode.vnoremap
- works non-recursively in visual and select modes.xnoremap
- works non-recursively in visual mode.snoremap
- works non-recursively in select mode.cnoremap
- works non-recursively in command-line mode.onoremap
- works non-recursively in operator pending mode.
Finally, remap
is a boolean option that allows for mappings to work recursively. It is worth mentioning that you should always keep this option at the default on
.
-
4This is a great explanation. However, I understand what recursion is, but I don't understand what it means in this context. What does it mean to work non-recursively? Commented Apr 5, 2022 at 20:06
-
5You can think of it as
no[remap] {lhs} {rhs}
which means to map the key sequence{lhs}
to{rhs}
, but do not re-map any commands in{rhs}
to avoid nested and recursive mappings.– MahmoudCommented Apr 6, 2022 at 14:23 -
7@Mahmoud it would be good if you could add the preceding comment to your answer, perhaps immediately after the part about
noremap
andnoremap!
. I was wondering exactly the same thing as Janac Meena. (I think I might have the ability to make the edit, but I don't want to add something new to someone else's answer.)– MarsCommented Apr 14, 2022 at 22:48
One difference is that:
:map
doesnvo
== normal + (visual + select) + operator pending:map!
doesic
== insert + command-line mode
as stated on help map-modes
tables.
So: map
does not map to all modes.
To map to all modes you need both :map
and :map!
.
-
5Careful !!
command mode
is another historical name fornormal mode
viz. chapt 5 vimdoc.sourceforge.net/htmldoc/intro.html#vim-modes-intro). The abbreviation 'c' is for 'command-line'. In other words, by default:map! lhs rhs
recursively maps lhs to rhs for insert + command-LINE modes. That is unless theremap
default option is explicitely disabled withset noremap[!]
in ~/.vimrc. In that case mapping would not be recursive (not advisable).– CbhiheCommented Aug 15, 2015 at 19:00 -
2This answer lacks context and does not answer the question. It might be better suited as a comment.– admdrewCommented Dec 22, 2022 at 21:16
In Vim, the mapping commands remap, noremap, nnoremap and vnoremap are used to define key mappings that allow you to execute a series of Vim commands by pressing a single key or key combination. Here's a brief overview of the differences between each of these mapping commands:
remap: This command allows you to remap an existing mapping to a new mapping. For example, if you want to remap the "jk" sequence to escape in insert mode, you can use the following command:
:remap jk <Esc>
This will replace the "jk" sequence with the "Esc" key.
noremap: This command creates a non-recursive mapping. This means that any mappings that are defined in the new mapping will not be expanded. For example, if you have the following mappings:
:map a b
:map b c
And you use the following command:
:noremap a b
This will create a non-recursive mapping for "a" that directly maps to "b" without expanding any other mappings.
nnoremap: This command creates a non-recursive mapping for normal mode only. This means that the mapping will only apply in normal mode, and any mappings that are defined in the new mapping will not be expanded. For example, if you have the following mappings:
:map a b
:map b c
And you use the following command:
:nnoremap a b
This will create a non-recursive mapping for "a" that directly maps to "b" without expanding any other mappings, but it will only apply in normal mode.
vnoremap: This command creates a non-recursive mapping for visual mode only. This means that the mapping will only apply in visual mode, and any mappings that are defined in the new mapping will not be expanded. For example, if you have the following mappings:
:map a b
:map b c
And you use the following command:
:vnoremap a b
This will create a non-recursive mapping for "a" that directly maps to "b" without expanding any other mappings, but it will only apply in visual mode.
It's important to note that when creating mappings, especially recursive mappings, it's possible to create unintended consequences or "mapping conflicts" that can cause issues when trying to use Vim. Careful attention to the mappings being created, especially when modifying the behavior of commonly used keys, is recommended to avoid unwanted side effects.
In general, noremap, nnoremap, and vnoremap are considered safer to use than remap, as they don't create recursive mappings and thus are less likely to cause unintended side effects. However, there may be situations where a recursive mapping is necessary, in which case remap can be used to overwrite an existing mapping with a new one.
It's also worth noting that there are other mapping commands in Vim, such as imap for insert mode mappings and map! for command-line mappings. Each of these commands behaves similarly to the mapping commands described above, but applies to a specific mode in Vim.
-
2Was this generated using ChatGPT (or similar)? There is a marked difference in writing style before and after December 2022. Commented Oct 10, 2023 at 20:15
-
@PeterMortensen it is possible I used one of those tools to help cleanup my writing, I am not the best writer and have been experimenting with using these tools to improve the quality and readability of my long form writing, if that goes against the policies of SO I can delete this answer or try to rewrite it in my inferior writing style Commented Oct 11, 2023 at 14:55
Your Answer
Post as a guest
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged or ask your own question.
- Featured on Meta
-
-
-
Linked
Related
Hot Network Questions
- Purpose of Green/Orange switch on old flash unit
- Is there a more concise method to solve the problem of finding tangent lines to curves?
- Conditional Constraint Formulation LP
- Can a bad paper title hurt you? Can a good one help you?
- Is "secco" really used in piano music?
- Has Blue Origin carried out any flight or landing tests of the New Glenn first stage?
- Are missiles aircraft?
- Does `chfn` provide a mobile phone parameter?
- Why can we treat a ball as a point mass to calculate torque?
- "Des fini ton plat"?
- What were the last three single moves?
- Was supposed to be co-signer on auto for daughter but I’m listed the buyer
- What happens if a leading Presidential candidate dies with no running mate?
- Changing values where two geometries are equal otherwise keep value with QGIS Virtual layer
- What does "steal so much as a single glance" mean?
- Was the head of the Secret Service ever removed for a security failure?
- How can a one get additional master proficiency (or something else) in order to get the critical specialization effect for a second weapon group?
- How to understand "...we could all see our way past thinking…"
- tikz diagram skier on a slope
- Is the ‘t’ in ‘witch’ considered a silent t?
- How do cables and cooling lines transverse the pressure hull of the International Space Station?
- Need help deciphering word written in Fraktur
- Constructing the interval [0, 1) via inverse powers of 2
- Has an aircraft ever crashed due to it exceeding its ceiling altitude?