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?

  • 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 '15 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 '15 at 3:53

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.

  • 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 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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