There are situations where key codes might not be recognized:
- Vim can only read part of the key code. Mostly this is only the first
character. This happens on some Unix versions in an xterm.
- The key code is after character(s) that are mapped. E.g., "<F1><F1>" or
"g<F1>".
The result is that the key code is not recognized in this situation, and the
mapping fails. There are two actions needed to avoid this problem:
- Remove the 'K' flag from 'cpoptions'. This will make Vim wait for the rest
of the characters of the function key.
- When using <F1> to <F4> the actual key code generated may correspond to
<xF1> to <xF4>. There are mappings from <xF1> to <F1>, <xF2> to <F2>, etc.,
but these are not recognized after another half a mapping. Make sure the
key codes for <F1> to <F4> are correct: >
:set <F1>=<type CTRL-V><type F1>
< Type the <F1> as four characters. The part after the "=" must be done with
the actual keys, not the literal text.
Another solution is to use the actual key code in the mapping for the second
special key: >
:map <F1><Esc>OP :echo "yes"<CR>
Don't type a real <Esc>, Vim will recognize the key code and replace it with
<F1> anyway.
*recursive_mapping*
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.
If you want to exchange the meaning of two keys you should use the :noremap
command. For example: >
:noremap k j
:noremap j k
This will exchange the cursor up and down commands.
With the normal :map command mapping takes place until the text is found not
to be a part of a {lhs}. For example, if you use: >
:map x y
:map y x
Vim will replace x with y, and then y with x, etc. When this has happened
'maxmapdepth' times (default 1000), Vim will give the error message
"recursive mapping".
*:map-undo*
If you include an undo command inside a mapped sequence, this will bring the
text back in the state before executing the macro. This is compatible with
the original Vi, as long as there is only one undo command in the mapped
sequence (having two undo commands in a mapped sequence did not make sense
in the original Vi, you would get back the text before the first undo).
1.10 MAPPING ALT-KEYS *:map-alt-keys*
For a readable mapping command the <A-k> form can be used. Note that <A-k>
and <A-K> are different, the latter will use an upper case letter. Actually,
<A-K> and <A-S-K> are the same. Instead of "A" you can use "M". If you have
an actual Meta modifier key, please see |:map-meta-keys|.
In the GUI Nvim handles the |ALT| key itself, thus mapping keys with ALT
should always work. But in a terminal Nvim gets a sequence of bytes and has
to figure out whether ALT was pressed. Terminals may use ESC to indicate that
ALT was pressed. If ESC is followed by a {key} within 'ttimeoutlen'
milliseconds, the ESC is interpreted as:
<ALT-{key}>
otherwise it is interpreted as two key presses:
<ESC> {key}
1.11 MAPPING META-KEYS *:map-meta-keys*
Mapping keys with the Meta modifier works very similar to using the Alt key.
What key on your keyboard produces the Meta modifier depends on your keyboard
and configuration.
Note that mapping <M-a> actually is for using the Alt key. That can be
confusing! It cannot be changed, it would not be backwards compatible.
For the Meta modifier the "T" character is used. For example,