The best answers to the question “What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim?” in the category Dev.
What is the difference between the
vnoremap mapping commands in Vim?
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”).
help map-modes will show you a few tables that explain how to control which modes the mapping applies to.
n: normal only
v: visual and select
x: visual only
s: select only
l: 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.
nmap K H nnoremap H G nnoremap G gg
The above causes
K to expand to
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
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.
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:
:noremap are recursive and non-recursive versions of the various mapping commands. For example, if we run:
:map j gg (moves cursor to first line) :map Q j (moves cursor to first line) :noremap W j (moves cursor down one line)
jwill be mapped to
Qwill also be mapped to
jwill be expanded for the recursive mapping.
Wwill be mapped to
j(and not to
jwill 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 (
:noremap), one that works in normal mode (
:nnoremap), one in visual mode (
:vnoremap) and so on.
For more guidance on this, see:
:help :map :help :noremap :help recursive_mapping :help :map-modes
One difference is that:
nvo== normal + (visual + select) + operator pending
ic== insert + command-line mode
as stated on
help map-modes tables.
map does not map to all modes.
To map to all modes you need both