May 24, 2021 Vim
When you're ready, the following will be more difficult to understand.
So far,
map
nmap
vmap
imap
and imap. T
hey are convenient, but they have a drawback.
Run the following command:
:nmap - dd
:nmap \ -
Try pressing
\
. (in normal mode).
What's the phenomenon?
Vim
\
it as - when you
-
B
ut we're mapping
-
!
Vim continues to
-
dd
i.e. it deletes the entire row.
Maps that you create with those commands may be interpreted by Vim as other maps. A t first glance this may sound like an advantage, but it's actually perverted. Before we explain why, let's remove those maps with the following command:
:nunmap -
:nunmap \
Run the command:
:nmap dd O<esc>jddk
The command above looks like it's
dd
to:
It appears that the role of this mapping is to "clear the current row". But you can try it.
When you
dd
Vim doesn't move. P
ress
<c-c>
continue, but there will be a lot more empty lines in your file!
Think about what happened.
This mapping is
actually recursive!
When you
dd
Vim interprets as:
dd
mapping, and the contents of the mapping are executed.
dd
mapping, and the contents of the mapping are executed.
dd
mapping, and the contents of the mapping are executed.
And then it always has been.
This mapping never ends! Delete this scary map and continue:
:nunmap dd
*map
the map series commands is the danger of recursion.
The other is that if you install a plug-in that maps the same key to a different behavior, the two conflict and one map is invalid.
When you install a new plug-in, you may not use or remember each mapping it creates.
Even if you remember, you'll have to look back at your
~/.vimrc
make sure that your custom mapping doesn't conflict with the plug-in creation.
This makes plug-in installation tedious and error-prone. There must be a solution.
Vim provides another set of mapping commands that create maps that are not recursive at runtime. Run the command:
:nmap x dd
:nnoremap \ x
Press
\
to see what's going on.
When you
\
Vim ignores
x
map and only
x
action of x.
That is, delete the characters under the current cursor instead of deleting the entire line.
Each of the
*map
series has a corresponding
*noremap
command,
noremap
/
nnoremap
vnoremap
inoremap
These commands do not recursively interpret the contents of the map.
When should these non-recursive mapping commands be used?
The answer is: Anytime.
Yes, no kidding, anytime.
Using the
*map
series command when installing plug-ins or adding new custom maps is simply a hassle
for
yourself.
Tap a few more characters to make sure this doesn't happen and save yourself from the sea of fire.
Replace all the mapping commands added to the previous section in the file of
~/.vimrc
with non-recursive versions.
Read the help
:help unmap