1226

What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim?

Arsen Khachaturyan
  • 6,472
  • 4
  • 32
  • 36
Chetan
  • 41,886
  • 27
  • 101
  • 142
  • 9
    Caution, `vnoremap` and `vmap` work in Visual AND Select mode. To have a mapping only in Visual mode, use `xmap` and `xnoremap`. – Benoit Sep 24 '10 at 13:58

3 Answers3

1781

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 cursor to first line)
:map Q j            (moves cursor to first line)
:noremap W j        (moves cursor down one line)

Then:

  • j will be mapped to gg.
  • Q will also be mapped to gg, because j will be expanded for the recursive mapping.
  • W will be mapped to j (and not to gg) because j 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 (:vmap and :vnoremap) and so on.

For more guidance on this, see:

:help :map
:help :noremap
:help recursive_mapping
:help :map-modes
Mateen Ulhaq
  • 18,406
  • 13
  • 75
  • 112
DrAl
  • 64,451
  • 10
  • 98
  • 103
  • 10
    Thanks for your answer! Also, when is recursive used, and when is non-recursive used? – Chetan Sep 29 '10 at 22:35
  • 15
    @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). – DrAl Sep 30 '10 at 07:02
  • 11
    I had assumes `noremap` to be some opposite of `map`. I mean something which removes a mapping. Thanks for the answer. It clarified me – Pavan Manjunath Mar 14 '12 at 09:07
  • 2
    does `nore` stand for `[no] [re]map` ? – whytheq May 09 '13 at 11:53
  • @whytheq Basically, yeah. It means essentially "Don't use remap even if it is enabled." – Ken Bellows Jun 26 '13 at 13:55
  • 4
    It 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: http://vimdoc.sourceforge.net/htmldoc/map.html#map-overview) – Ken Bellows Jun 26 '13 at 14:00
  • 4
    @whytheq noremap = non-recursive mapping – Dan Bechard Mar 21 '14 at 17:50
  • 3
    And if I use `noremap j gg` and then `map Q j` is pressing Q going to call gg ? I understand the opposite wouldn't. – cram2208 Jul 22 '15 at 23:35
  • 1
    @AshwinNanjappa absolutely! I find the help inside of Vim so hard to understand most of the time! +cram2208 this is a great question and it's too bad no one has answered you! My guess would be that Q would indeed call gg. I would think that this is only stopping recursing of j to anything beyond gg. But maybe it *does* affect "inherited(?)" maps? I really don't know and hope someone can shed some light on this; I'm still not even 100% on what the purpose of recursive vs non-recursive maps are! EDIT: it looks like your question was answered in an answer below. Short answer: Q *will* call gg. – RastaJedi Mar 07 '16 at 06:55
  • I think I've got it now, you can essentially use recursive and non-recursive to bind *and* unbind keymaps, I think (it is how you can refer to the normal function of a key). – RastaJedi Mar 07 '16 at 06:58
  • 1
    Great answer! Thank you. After I read this, I dove into the docs and FINALLY found a references to this. If I hadn't known this was "recursive mapping", I never would have found it. `:help recursive_mapping` – OldTimeGuitarGuy May 22 '19 at 01:30
325

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 only
  • v: visual and select
  • o: operator-pending
  • x: visual only
  • s: select only
  • i: insert
  • c: command-line
  • 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:

  1. the result is no longer mapped to anything else.
  2. 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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kelvin
  • 17,790
  • 2
  • 54
  • 62
23

One difference is that:

  • :map does nvo == normal + (visual + select) + operator pending
  • :map! does ic == 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!.

  • 5
    Careful !! `command mode` is another historical name for `normal mode` viz. chapt 5 http://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 the `remap` default option is explicitely disabled with `set noremap[!]` in ~/.vimrc. In that case mapping would not be recursive (not advisable). – Cbhihe Aug 15 '15 at 19:00