39

These commands transform one set of keystrokes to another in some way, but I am somewhat fuzzy on which one is most appropriate for some situation. In addition, there are the ! variants of them as well. At the moment, my usage of them is pretty haphazard, so could I know what are the pitfalls associated with each? In particular notes about the various mode versions could be useful, since I learnt from Peter Rincker in a comment that cmap can expand pretty much anywhere in the line and not only when I'm using : commands. What precautions can I take against potential pitfalls?

2
  • The next part is a bit meta: Which form should we use while answering on this site? - should I ask this on Meta, or add it to the question?
    – muru
    Feb 22, 2015 at 2:50
  • I would say it depends. I would always use noremap version if demonstrating a mapping. This way, people get in the habit of using the non-recursive mappings over the recursive versions. Unless of course, there is a reason to use map specifically, for instance, in a <Plug> mapping.
    – akshay
    Feb 22, 2015 at 3:53

1 Answer 1

41

First, map and noremap are similar in that that each create mappings for normal, visual, select and operator pending modes simultaneously. Vim details this in :help map-overview:

Overview of which map command works in which mode.  More details below.
     COMMANDS                    MODES ~
:map   :noremap  :unmap     Normal, Visual, Select, Operator-pending
:nmap  :nnoremap :nunmap    Normal
:vmap  :vnoremap :vunmap    Visual and Select
:smap  :snoremap :sunmap    Select
:xmap  :xnoremap :xunmap    Visual
:omap  :onoremap :ounmap    Operator-pending
:map!  :noremap! :unmap!    Insert and Command-line
:imap  :inoremap :iunmap    Insert
:lmap  :lnoremap :lunmap    Insert, Command-line, Lang-Arg
:cmap  :cnoremap :cunmap    Command-line

As per the above help, if you wanted to restrict the mapping to a specific mode, you have to prepend:

'n' (for normal), 'v' (for visual and select), 'c' (for command), 'x' (for visual mode), 's' (for select), 'o' (for operator pending).

For instance,

nmap n nzz

will create a normal mode, recursive mapping of n.

Now, noremap is just a non-recursive version of map.

So what is non-recursive mapping? Vim has the answer to that too, with :help map-recursive:

If you include the {lhs} in the {rhs} you have a recursive mapping.  When
{lhs} is typed, it will be replaced with {rhs}.  When the {lhs} which is
included in {rhs} is encountered it will be replaced with {rhs}, and so on.
This makes it possible to repeat a command an infinite number of times.  The
only problem is that the only way to stop this is by causing an error.  The
macros to solve a maze uses this, look there for an example.  There is one
exception: If the {rhs} starts with {lhs}, the first character is not mapped
again (this is Vi compatible).
For example: >
   :map ab abcd
will execute the "a" command and insert "bcd" in the text.  The "ab" in the
{rhs} will not be mapped again.

An example of this is mapping the following:

:imap j k

:imap k j

Now, vim will replace j with k and k with j infinite number of times, and will therefore show you an error that you have created a recursive mapping.

This is why it is generally recommended that you almost always (except when you have <Plug> mappings or similar) use non-recursive mappings. This prevents Vim hanging when you inadvertently create recursive mappings. The non-recursive mapping is therefore a more safer way to map commands in Vim.

With the above information at hand, we can see that :noreabbrev is just a non-recursive version of :abbrev command.

You can use :abbrev only in insert, replace and command modes. :abbrev is used for creating abbreviations, (aka shortcuts that Vim can expand). The short expanation is to use :map/:noremap to create mappings, :abbrev/:noreabbrev to create abbreviations, or whenever you want Vim to expand out your typing.

4
  • 1
    Avoiding hangs isn't the only reason to use the nore forms of the map command. Another (and in my opinion, more important) reason to use them is that they make your mappings reliable/predictable. If you use the plain form and the rhs contains a key that happens to be mapped to something else, then your mapping could have entirely different behaviour than intended.
    – Rich
    Feb 13, 2017 at 9:45
  • What's the difference between map and remap?
    – Magnus
    Jul 5, 2020 at 16:42
  • Thanks for the clear explanation and illustration of the map/noremap distinction. (Merely saying that the noremaps don't expand recursively, etc., as in some of the answers to this old question on Stackoverflow, is intelligible to anyone who knows what "recursive" means, but doesn't convey why one should care.)
    – Mars
    Apr 26, 2022 at 3:02
  • @Magnus, there is no remap afaik.
    – Mars
    Apr 26, 2022 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.