1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
*map.txt* For Vim version 7.0aa. Last change: 2005 Feb 27
VIM REFERENCE MANUAL by Bram Moolenaar
Key mapping, abbreviations and user-defined commands.
This subject is introduced in sections |05.3|, |24.7| and |40.1| of the user
manual.
1. Key mapping |key-mapping|
2. Abbreviations |abbreviations|
3. Local mappings and functions |script-local|
4. User-defined commands |user-commands|
==============================================================================
1. Key mapping *key-mapping* *mapping* *macro*
Key mapping is used to change the meaning of typed keys. The most common use
is to define a sequence commands for a function key. Example: >
:map <F2> a<C-R>=strftime("%c")<CR><Esc>
This appends the current date and time after the cursor. (in <> notation |<>|)
There are commands to enter new mappings, remove mappings and list mappings.
See |map-overview| for the various forms of "map" and their relationships with
modes.
{lhs} means left-hand-side *{lhs}*
{rhs} means right-hand-side *{rhs}*
:map {lhs} {rhs} *:map*
:nm[ap] {lhs} {rhs} *:nm* *:nmap*
:vm[ap] {lhs} {rhs} *:vm* *:vmap*
:om[ap] {lhs} {rhs} *:om* *:omap*
:map! {lhs} {rhs} *:map!*
:im[ap] {lhs} {rhs} *:im* *:imap*
:lm[ap] {lhs} {rhs} *:lm* *:lmap*
:cm[ap] {lhs} {rhs} *:cm* *:cmap*
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. The result, including
{rhs}, is then further scanned for mappings. This
allows for nested and recursive use of mappings.
:no[remap] {lhs} {rhs} *:no* *:noremap*
:nn[oremap] {lhs} {rhs} *:nn* *:nnoremap*
:vn[oremap] {lhs} {rhs} *:vn* *:vnoremap*
:ono[remap] {lhs} {rhs} *:ono* *:onoremap*
:no[remap]! {lhs} {rhs} *:no!* *:noremap!*
:ino[remap] {lhs} {rhs} *:ino* *:inoremap*
:ln[oremap] {lhs} {rhs} *:ln* *:lnoremap*
:cno[remap] {lhs} {rhs} *:cno* *:cnoremap*
Map the key sequence {lhs} to {rhs} for the modes
where the map command applies. Disallow mapping of
{rhs}, to avoid nested and recursive mappings. Often
used to redefine a command. {not in Vi}
:unm[ap] {lhs} *:unm* *:unmap*
:nun[map] {lhs} *:nun* *:nunmap*
:vu[nmap] {lhs} *:vu* *:vunmap*
:ou[nmap] {lhs} *:ou* *:ounmap*
:unm[ap]! {lhs} *:unm!* *:unmap!*
:iu[nmap] {lhs} *:iu* *:iunmap*
:lu[nmap] {lhs} *:lu* *:lunmap*
:cu[nmap] {lhs} *:cu* *:cunmap*
Remove the mapping of {lhs} for the modes where the
map command applies. The mapping may remain defined
for other modes where it applies.
Note: Trailing spaces are included in the {lhs}. This
unmap does NOT work: >
:map @@ foo
:unmap @@ | print
:mapc[lear] *:mapc* *:mapclear*
:nmapc[lear] *:nmapc* *:nmapclear*
:vmapc[lear] *:vmapc* *:vmapclear*
:omapc[lear] *:omapc* *:omapclear*
:mapc[lear]! *:mapc!* *:mapclear!*
:imapc[lear] *:imapc* *:imapclear*
:lmapc[lear] *:lmapc* *:lmapclear*
:cmapc[lear] *:cmapc* *:cmapclear*
Remove ALL mappings for the modes where the map
command applies. {not in Vi}
Warning: This also removes the default mappings.
:map
:nm[ap]
:vm[ap]
:om[ap]
:map!
:im[ap]
:lm[ap]
:cm[ap]
List all key mappings for the modes where the map
command applies. Note that ":map" and ":map!" are
used most often, because they include the other modes.
:map {lhs} *:map_l*
:nm[ap] {lhs} *:nmap_l*
:vm[ap] {lhs} *:vmap_l*
:om[ap] {lhs} *:omap_l*
:map! {lhs} *:map_l!*
:im[ap] {lhs} *:imap_l*
:lm[ap] {lhs} *:lmap_l*
:cm[ap] {lhs} *:cmap_l*
List the key mappings for the key sequences starting
with {lhs} in the modes where the map command applies.
{not in Vi}
These commands are used to map a key or key sequence to a string of
characters. You can use this to put command sequences under function keys,
translate one key into another, etc. See |:mkexrc| for how to save and
restore the current mappings.
*:map-local* *:map-<buffer>* *E224* *E225*
If the first argument to one of these commands is "<buffer>" it will apply to
mappings locally to the current buffer only. Example: >
:map <buffer> ,w /[.,;]<CR>
Then you can map ",w" to something else in another buffer: >
:map <buffer> ,w /[#&!]<CR>
The local buffer mappings are used before the global ones.
The "<buffer>" argument can also be used to clear mappings: >
:unmap <buffer> ,w
:mapclear <buffer>
Local mappings are also cleared when a buffer is deleted, but not when it is
unloaded. Just like local option values.
*:map-<silent>* *:map-silent*
To define a mapping which will not be echoed on the command line, add
"<silent>" as the first argument. Example: >
:map <silent> ,h /Header<CR>
The search string will not be echoed when using this mapping. Messages from
the executed command are still given though. To shut them up too, add a
":silent" in the executed command: >
:map <silent> ,h :exe ":silent normal /Header\r"<CR>
Prompts will still be given, e.g., for inputdialog().
Using "<silent>" for an abbreviation is possible, but will cause redrawing of
the command line to fail.
*:map-<script>* *:map-script*
If the first argument to one of these commands is "<script>" and it is used to
define a new mapping or abbreviation, the mapping will only remap characters
in the {rhs} using mappings that were defined local to a script, starting with
"<SID>". This can be used to avoid that mappings from outside a script
interfere (e.g., when CTRL-V is remapped in mswin.vim), but do use other
mappings defined in the script.
Note: ":map <script>" and ":noremap <script>" do the same thing. The
"<script>" overrules the command name. Using ":noremap <script>" is
preferred, because it's clearer that remapping is (mostly) disabled.
*:map-<unique>* *E226* *E227*
If the first argument to one of these commands is "<unique>" and it is used to
define a new mapping or abbreviation, the command will fail if the mapping or
abbreviation already exists. Example: >
:map <unique> ,w /[#&!]<CR>
When defining a local mapping, there will also be a check if a global map
already exists which is equal.
Example of what will fail: >
:map ,w /[#&!]<CR>
:map <buffer> <unique> ,w /[.,;]<CR>
"<buffer>", "<silent>", "<script>" and "<unique>" can be used in any order.
They must appear right after the command, before any other arguments.
MAPPING AND MODES
There are five sets of mappings
- For Normal mode: When typing commands.
- For Visual mode: When typing commands while the Visual area is highlighted.
- For Operator-pending mode: When an operator is pending (after "d", "y", "c",
etc.). Example: ":omap { w" makes "y{" work like "yw" and "d{" like "dw".
- For Insert mode. These are also used in Replace mode.
- For Command-line mode: When entering a ":" or "/" command.
There are no separate mappings for Select mode. The same as for Visual mode
are used |Select-mode-mapping|.
Special case: While typing a count for a command in Normal mode, mapping zero
is disabled. This makes it possible to map zero without making it impossible
to type a count with a zero.
*map-overview* *map-modes*
Overview of which map command works in which mode:
commands: modes: ~
Normal Visual Operator-pending ~
:map :noremap :unmap :mapclear yes yes yes
:nmap :nnoremap :nunmap :nmapclear yes - -
:vmap :vnoremap :vunmap :vmapclear - yes -
:omap :onoremap :ounmap :omapclear - - yes
Insert Command-line Lang-Arg ~
:map! :noremap! :unmap! :mapclear! yes yes -
:imap :inoremap :iunmap :imapclear yes - -
:cmap :cnoremap :cunmap :cmapclear - yes -
:lmap :lnoremap :lunmap :lmapclear yes* yes* yes*
The original Vi did not have separate mappings for
Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
Therefore the ":map" and ":map!" commands enter and display mappings for
several modes. In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
":imap" commands to enter mappings for each mode separately.
To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
first define it for all three modes, then unmap it for Operator-pending mode:
:map xx something-difficult
:ounmap xx
Likewise for a mapping for Visual and Operator-pending mode or Normal and
Operator-pending mode.
*language-mapping*
":lmap" defines a mapping that applies to:
- Insert mode
- Command-line mode
- when entering a search pattern
- the argument of the commands that accept a text character, such as "r" and
"f"
- for the input() line
Generally: Whenever a character is to be typed that is part of the text in the
buffer, not a Vim command character. "Lang-Arg" isn't really another mode,
it's just used here for this situation.
The simplest way to load a set of related language mappings is by using the
'keymap' option. See |45.5|.
In Insert mode and in Command-line mode the mappings can be disabled with
the CTRL-^ command |i_CTRL-^| |c_CTRL-^|. When starting to enter a normal
command line (not a search pattern) the mappings are disabled until a CTRL-^
is typed. The state last used is remembered for Insert mode and Search
patterns separately. The state for Insert mode is also used when typing a
character as an argument to command like "f" or "t".
Language mappings will never be applied to already mapped characters. They
are only used for typed characters. This assumes that the language mapping
was already done when typing the mapping.
*map-multibyte*
It is possible to map multibyte characters, but only the whole character. You
cannot map the first byte only. This was done to prevent problems in this
scenario: >
:set encoding=latin1
:imap <M-C> foo
:set encoding=utf-8
The mapping for <M-C> is defined with the latin1 encoding, resulting in a 0xc3
byte. If you type the character � (0xea <M-a>) in UTF-8 encoding this is the
two bytes 0xc3 0xa1. You don't want the 0xc3 byte to be mapped then,
otherwise it would be impossible to type the � character.
*map-listing*
When listing mappings the characters in the first two columns are:
CHAR MODE ~
<Space> Normal, Visual and Operator-pending
n Normal
v Visual
o Operator-pending
! Insert and Command-line
i Insert
l ":lmap" mappings for Insert, Command-line and Lang-Arg
c Command-line
Just before the {rhs} a special character can appear:
* indicates that it is not remappable
& indicates that only script-local mappings are remappable
@ indicates a buffer-local mapping
Everything from the first non-blank after {lhs} up to the end of the line
(or '|') is considered to be part of {rhs}. This allows the {rhs} to end
with a space.
Note: When using mappings for Visual mode, you can use the "'<" mark, which
is the start of the last selected Visual area in the current buffer |'<|.
*map_backslash*
Note that only CTRL-V is mentioned here as a special character for mappings
and abbreviations. When 'cpoptions' does not contain 'B', a backslash can
also be used like CTRL-V. The <> notation can be fully used then |<>|. But
you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
follows.
To map a backslash, or use a backslash literally in the {rhs}, the special
sequence "<Bslash>" can be used. This avoids the need to double backslashes
when using nested mappings.
*map-ambiguous*
When two mappings start with the same sequence of characters, they are
ambiguous. Example: >
:imap aa foo
:imap aaa bar
When Vim has read "aa", it will need to get another character to be able to
decide if "aa" or "aaa" should be mapped. This means that after typing "aa"
that mapping won't get expanded yet, Vim is waiting for another character.
If you type a space, then "foo" will get inserted, plus the space. If you
type "a", then "bar" will get inserted.
{Vi does not allow ambiguous mappings}
*map_CTRL_C*
It's not possible to use a CTRL-C in the {lhs}. You just can't map CTRL-C.
The reason is that CTRL-C must always be available to break a running command.
Exception: When using the GUI version on MS-Windows CTRL-C can be mapped to
allow a Copy command to the clipboard. Use CTRL-Break to interrupt Vim.
*map_space_in_lhs*
To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
each space).
*map_space_in_rhs*
If you want a {rhs} that starts with a space, use "<Space>". To be fully Vi
compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
single CTRL-V (you have to type CTRL-V two times).
*map_empty_rhs*
You can create an empty {rhs} by typing nothing after a single CTRL-V (you
have to type CTRL-V two times). Unfortunately, you cannot do this in a vimrc
file.
*<Nop>*
A easier way to get a mapping that doesn't produce anything, is to use "<Nop>"
for the {rhs}. This only works when the |<>| notation is enabled. For
example, to make sure that function key 8 does nothing at all: >
:map <F8> <Nop>
:map! <F8> <Nop>
<
*<Leader>* *mapleader*
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead. Example: >
:map <Leader>A oanother line<Esc>
Works like: >
:map \A oanother line<Esc>
But after: >
:let mapleader = ","
It works like: >
:map ,A oanother line<Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
*<LocalLeader>* *maplocalleader*
Just like <Leader>, except that it uses "maplocalleader" instead of
"mapleader". <LocalLeader> is to be used for mappings which are local to a
buffer. Example: >
:map <LocalLeader>q \DoItNow
<
In a global plugin <Leader> should be used and in a filetype plugin
<LocalLeader>. "mapleader" and "maplocalleader" can be equal. Although, if
you make them different, there is a smaller chance of mappings from global
plugins to clash with mappings for filetype plugins. For example, you could
keep "mapleader" at the default backslash, and set "maplocalleader" to an
underscore.
*map-<SID>*
In a script the special key name "<SID>" can be used to define a mapping
that's local to the script. See |<SID>| for details.
*<Plug>*
The special key name "<Plug>" can be used for an internal mapping, which is
not to be matched with any key sequence. This is useful in plugins
|using-<Plug>|.
*<Char>* *<Char->*
To map a character by its decimal, octal or hexadecimal number the <Char>
construct can be used:
<Char-123> character 123
<Char-033> character 27
<Char-0x7f> character 127
This is useful to specify a (multi-byte) character in a 'keymap' file.
Upper and lowercase differences are ignored.
*map-comments*
It is not possible to put a comment after these commands, because the '"'
character is considered to be part of the {lhs} or {rhs}.
*map_bar*
Since the '|' character is used to separate a map command from the next
command, you will have to do something special to include a '|' in {rhs}.
There are three methods:
use works when example ~
<Bar> '<' is not in 'cpoptions' :map _l :!ls <Bar> more^M
\| 'b' is not in 'cpoptions' :map _l :!ls \| more^M
^V| always, in Vim and Vi :map _l :!ls ^V| more^M
(here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
cannot use the <> notation "<C-V>" here).
All three work when you use the default setting for 'cpoptions'.
When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
ending in a '\' and then another command. This is Vi compatible, but
illogical when compared to other commands.
*map_return*
When you have a mapping that contains an Ex command, you need to put a line
terminator after it to have it executed. The use of <CR> is recommended for
this (see |<>|). Example: >
:map _ls :!ls -l %<CR>:echo "the end"<CR>
To avoid mapping of the characters you type in insert or Command-line mode,
type a CTRL-V first. The mapping in Insert mode is disabled if the 'paste'
option is on.
Note that when an error is encountered (that causes an error message or beep)
the rest of the mapping is not executed. This is Vi-compatible.
Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
and CTRL-X is not mapped. This was done to be able to use all the named
registers and marks, even when the command with the same name has been
mapped.
*map-which-keys*
If you are going to map something, you will need to choose which key(s) to use
for the {lhs}. You will have to avoid keys that are used for Vim commands,
otherwise you would not be able to use those commands anymore. Here are a few
suggestions:
- Function keys <F2>, <F3>, etc.. Also the shifted function keys <S-F1>,
<S-F2>, etc. Note that <F1> is already used for the help command.
- Meta-keys (with the ALT key pressed).
- Use the '_' or ',' character and then any other character. The "_" and ","
commands do exist in Vim (see |_| and |,|), but you probably never use them.
- Use a key that is a synonym for another command. For example: CTRL-P and
CTRL-N. Use an extra character to allow more mappings.
See the file "index" for keys that are not used and thus can be mapped without
losing any builtin function. You can also use ":help {key}^D" to find out if
a key is used for some command. ({key} is the specific key you want to find
out about, ^D is CTRL-D).
*map-examples*
A few examples (given as you type them, for "<CR>" you type four characters;
the '<'
|