blob: a5f6d0c621df62d1f92acca4bf51d374f297d692 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001*map.txt* For Vim version 7.0aa. Last change: 2005 Oct 14
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Key mapping, abbreviations and user-defined commands.
8
9This subject is introduced in sections |05.3|, |24.7| and |40.1| of the user
10manual.
11
121. Key mapping |key-mapping|
132. Abbreviations |abbreviations|
143. Local mappings and functions |script-local|
154. User-defined commands |user-commands|
16
17==============================================================================
181. Key mapping *key-mapping* *mapping* *macro*
19
20Key mapping is used to change the meaning of typed keys. The most common use
21is to define a sequence commands for a function key. Example: >
22
23 :map <F2> a<C-R>=strftime("%c")<CR><Esc>
24
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000025This appends the current date and time after the cursor (in <> notation |<>|).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27There are commands to enter new mappings, remove mappings and list mappings.
28See |map-overview| for the various forms of "map" and their relationships with
29modes.
30
31{lhs} means left-hand-side *{lhs}*
32{rhs} means right-hand-side *{rhs}*
33
34:map {lhs} {rhs} *:map*
35:nm[ap] {lhs} {rhs} *:nm* *:nmap*
36:vm[ap] {lhs} {rhs} *:vm* *:vmap*
37:om[ap] {lhs} {rhs} *:om* *:omap*
38:map! {lhs} {rhs} *:map!*
39:im[ap] {lhs} {rhs} *:im* *:imap*
40:lm[ap] {lhs} {rhs} *:lm* *:lmap*
41:cm[ap] {lhs} {rhs} *:cm* *:cmap*
42 Map the key sequence {lhs} to {rhs} for the modes
43 where the map command applies. The result, including
44 {rhs}, is then further scanned for mappings. This
45 allows for nested and recursive use of mappings.
46
47
48:no[remap] {lhs} {rhs} *:no* *:noremap*
49:nn[oremap] {lhs} {rhs} *:nn* *:nnoremap*
50:vn[oremap] {lhs} {rhs} *:vn* *:vnoremap*
51:ono[remap] {lhs} {rhs} *:ono* *:onoremap*
52:no[remap]! {lhs} {rhs} *:no!* *:noremap!*
53:ino[remap] {lhs} {rhs} *:ino* *:inoremap*
54:ln[oremap] {lhs} {rhs} *:ln* *:lnoremap*
55:cno[remap] {lhs} {rhs} *:cno* *:cnoremap*
56 Map the key sequence {lhs} to {rhs} for the modes
57 where the map command applies. Disallow mapping of
58 {rhs}, to avoid nested and recursive mappings. Often
59 used to redefine a command. {not in Vi}
60
61
62:unm[ap] {lhs} *:unm* *:unmap*
63:nun[map] {lhs} *:nun* *:nunmap*
64:vu[nmap] {lhs} *:vu* *:vunmap*
65:ou[nmap] {lhs} *:ou* *:ounmap*
66:unm[ap]! {lhs} *:unm!* *:unmap!*
67:iu[nmap] {lhs} *:iu* *:iunmap*
68:lu[nmap] {lhs} *:lu* *:lunmap*
69:cu[nmap] {lhs} *:cu* *:cunmap*
70 Remove the mapping of {lhs} for the modes where the
71 map command applies. The mapping may remain defined
72 for other modes where it applies.
73 Note: Trailing spaces are included in the {lhs}. This
74 unmap does NOT work: >
75 :map @@ foo
76 :unmap @@ | print
77
78:mapc[lear] *:mapc* *:mapclear*
79:nmapc[lear] *:nmapc* *:nmapclear*
80:vmapc[lear] *:vmapc* *:vmapclear*
81:omapc[lear] *:omapc* *:omapclear*
82:mapc[lear]! *:mapc!* *:mapclear!*
83:imapc[lear] *:imapc* *:imapclear*
84:lmapc[lear] *:lmapc* *:lmapclear*
85:cmapc[lear] *:cmapc* *:cmapclear*
86 Remove ALL mappings for the modes where the map
87 command applies. {not in Vi}
88 Warning: This also removes the default mappings.
89
90:map
91:nm[ap]
92:vm[ap]
93:om[ap]
94:map!
95:im[ap]
96:lm[ap]
97:cm[ap]
98 List all key mappings for the modes where the map
99 command applies. Note that ":map" and ":map!" are
100 used most often, because they include the other modes.
101
102:map {lhs} *:map_l*
103:nm[ap] {lhs} *:nmap_l*
104:vm[ap] {lhs} *:vmap_l*
105:om[ap] {lhs} *:omap_l*
106:map! {lhs} *:map_l!*
107:im[ap] {lhs} *:imap_l*
108:lm[ap] {lhs} *:lmap_l*
109:cm[ap] {lhs} *:cmap_l*
110 List the key mappings for the key sequences starting
111 with {lhs} in the modes where the map command applies.
112 {not in Vi}
113
114These commands are used to map a key or key sequence to a string of
115characters. You can use this to put command sequences under function keys,
116translate one key into another, etc. See |:mkexrc| for how to save and
117restore the current mappings.
118
119 *:map-local* *:map-<buffer>* *E224* *E225*
120If the first argument to one of these commands is "<buffer>" it will apply to
121mappings locally to the current buffer only. Example: >
122 :map <buffer> ,w /[.,;]<CR>
123Then you can map ",w" to something else in another buffer: >
124 :map <buffer> ,w /[#&!]<CR>
125The local buffer mappings are used before the global ones.
126The "<buffer>" argument can also be used to clear mappings: >
127 :unmap <buffer> ,w
128 :mapclear <buffer>
129Local mappings are also cleared when a buffer is deleted, but not when it is
130unloaded. Just like local option values.
131
132 *:map-<silent>* *:map-silent*
133To define a mapping which will not be echoed on the command line, add
134"<silent>" as the first argument. Example: >
135 :map <silent> ,h /Header<CR>
136The search string will not be echoed when using this mapping. Messages from
137the executed command are still given though. To shut them up too, add a
138":silent" in the executed command: >
139 :map <silent> ,h :exe ":silent normal /Header\r"<CR>
140Prompts will still be given, e.g., for inputdialog().
141Using "<silent>" for an abbreviation is possible, but will cause redrawing of
142the command line to fail.
143
144 *:map-<script>* *:map-script*
145If the first argument to one of these commands is "<script>" and it is used to
146define a new mapping or abbreviation, the mapping will only remap characters
147in the {rhs} using mappings that were defined local to a script, starting with
148"<SID>". This can be used to avoid that mappings from outside a script
149interfere (e.g., when CTRL-V is remapped in mswin.vim), but do use other
150mappings defined in the script.
151Note: ":map <script>" and ":noremap <script>" do the same thing. The
152"<script>" overrules the command name. Using ":noremap <script>" is
153preferred, because it's clearer that remapping is (mostly) disabled.
154
155 *:map-<unique>* *E226* *E227*
156If the first argument to one of these commands is "<unique>" and it is used to
157define a new mapping or abbreviation, the command will fail if the mapping or
158abbreviation already exists. Example: >
159 :map <unique> ,w /[#&!]<CR>
160When defining a local mapping, there will also be a check if a global map
161already exists which is equal.
162Example of what will fail: >
163 :map ,w /[#&!]<CR>
164 :map <buffer> <unique> ,w /[.,;]<CR>
165
166"<buffer>", "<silent>", "<script>" and "<unique>" can be used in any order.
167They must appear right after the command, before any other arguments.
168
169
170MAPPING AND MODES
171
172There are five sets of mappings
173- For Normal mode: When typing commands.
174- For Visual mode: When typing commands while the Visual area is highlighted.
175- For Operator-pending mode: When an operator is pending (after "d", "y", "c",
176 etc.). Example: ":omap { w" makes "y{" work like "yw" and "d{" like "dw".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000177- For Insert mode. These are also used in Replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178- For Command-line mode: When entering a ":" or "/" command.
179
180There are no separate mappings for Select mode. The same as for Visual mode
181are used |Select-mode-mapping|.
182
183Special case: While typing a count for a command in Normal mode, mapping zero
184is disabled. This makes it possible to map zero without making it impossible
185to type a count with a zero.
186
187 *map-overview* *map-modes*
188Overview of which map command works in which mode:
189
190 commands: modes: ~
191 Normal Visual Operator-pending ~
192:map :noremap :unmap :mapclear yes yes yes
193:nmap :nnoremap :nunmap :nmapclear yes - -
194:vmap :vnoremap :vunmap :vmapclear - yes -
195:omap :onoremap :ounmap :omapclear - - yes
196
197 Insert Command-line Lang-Arg ~
198:map! :noremap! :unmap! :mapclear! yes yes -
199:imap :inoremap :iunmap :imapclear yes - -
200:cmap :cnoremap :cunmap :cmapclear - yes -
201:lmap :lnoremap :lunmap :lmapclear yes* yes* yes*
202
203The original Vi did not have separate mappings for
204Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
205Therefore the ":map" and ":map!" commands enter and display mappings for
206several modes. In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
207":imap" commands to enter mappings for each mode separately.
208
209To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
210first define it for all three modes, then unmap it for Operator-pending mode:
211 :map xx something-difficult
212 :ounmap xx
213Likewise for a mapping for Visual and Operator-pending mode or Normal and
214Operator-pending mode.
215
216 *language-mapping*
217":lmap" defines a mapping that applies to:
218- Insert mode
219- Command-line mode
220- when entering a search pattern
221- the argument of the commands that accept a text character, such as "r" and
222 "f"
223- for the input() line
224Generally: Whenever a character is to be typed that is part of the text in the
225buffer, not a Vim command character. "Lang-Arg" isn't really another mode,
226it's just used here for this situation.
227 The simplest way to load a set of related language mappings is by using the
228'keymap' option. See |45.5|.
229 In Insert mode and in Command-line mode the mappings can be disabled with
230the CTRL-^ command |i_CTRL-^| |c_CTRL-^|. When starting to enter a normal
231command line (not a search pattern) the mappings are disabled until a CTRL-^
232is typed. The state last used is remembered for Insert mode and Search
233patterns separately. The state for Insert mode is also used when typing a
234character as an argument to command like "f" or "t".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 Language mappings will never be applied to already mapped characters. They
236are only used for typed characters. This assumes that the language mapping
237was already done when typing the mapping.
238
239 *map-multibyte*
240It is possible to map multibyte characters, but only the whole character. You
241cannot map the first byte only. This was done to prevent problems in this
242scenario: >
243 :set encoding=latin1
244 :imap <M-C> foo
245 :set encoding=utf-8
246The mapping for <M-C> is defined with the latin1 encoding, resulting in a 0xc3
247byte. If you type the character á (0xea <M-a>) in UTF-8 encoding this is the
248two bytes 0xc3 0xa1. You don't want the 0xc3 byte to be mapped then,
249otherwise it would be impossible to type the á character.
250
251 *map-listing*
252When listing mappings the characters in the first two columns are:
253
254 CHAR MODE ~
255 <Space> Normal, Visual and Operator-pending
256 n Normal
257 v Visual
258 o Operator-pending
259 ! Insert and Command-line
260 i Insert
261 l ":lmap" mappings for Insert, Command-line and Lang-Arg
262 c Command-line
263
264Just before the {rhs} a special character can appear:
265 * indicates that it is not remappable
266 & indicates that only script-local mappings are remappable
267 @ indicates a buffer-local mapping
268
269Everything from the first non-blank after {lhs} up to the end of the line
270(or '|') is considered to be part of {rhs}. This allows the {rhs} to end
271with a space.
272
273Note: When using mappings for Visual mode, you can use the "'<" mark, which
274is the start of the last selected Visual area in the current buffer |'<|.
275
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000276 *:map-verbose*
277When 'verbose' is non-zero, listing a key map will also display where it was
278last defined. Example: >
279
280 :verbose map <C-W>*
281 n <C-W>* * <C-W><C-S>*
282 Last set from /home/abcd/.vimrc
283
Bram Moolenaar5195e452005-08-19 20:32:47 +0000284See |:verbose-cmd| for more information.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 *map_backslash*
287Note that only CTRL-V is mentioned here as a special character for mappings
288and abbreviations. When 'cpoptions' does not contain 'B', a backslash can
289also be used like CTRL-V. The <> notation can be fully used then |<>|. But
290you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
291follows.
292
293To map a backslash, or use a backslash literally in the {rhs}, the special
294sequence "<Bslash>" can be used. This avoids the need to double backslashes
295when using nested mappings.
296
297 *map-ambiguous*
298When two mappings start with the same sequence of characters, they are
299ambiguous. Example: >
300 :imap aa foo
301 :imap aaa bar
302When Vim has read "aa", it will need to get another character to be able to
303decide if "aa" or "aaa" should be mapped. This means that after typing "aa"
304that mapping won't get expanded yet, Vim is waiting for another character.
305If you type a space, then "foo" will get inserted, plus the space. If you
306type "a", then "bar" will get inserted.
307{Vi does not allow ambiguous mappings}
308
Bram Moolenaar1e015462005-09-25 22:16:38 +0000309 *map_CTRL-C*
310Using CTRL-C in the {lhs} is possible, but it will only work when Vim is
311waiting for a key, not when Vim is busy with something. When Vim is busy
312CTRL-C interrupts/breaks the command.
313When using the GUI version on MS-Windows CTRL-C can be mapped to allow a Copy
314command to the clipboard. Use CTRL-Break to interrupt Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
316 *map_space_in_lhs*
317To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
318each space).
319 *map_space_in_rhs*
320If you want a {rhs} that starts with a space, use "<Space>". To be fully Vi
321compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
322single CTRL-V (you have to type CTRL-V two times).
323 *map_empty_rhs*
324You can create an empty {rhs} by typing nothing after a single CTRL-V (you
325have to type CTRL-V two times). Unfortunately, you cannot do this in a vimrc
326file.
327 *<Nop>*
328A easier way to get a mapping that doesn't produce anything, is to use "<Nop>"
329for the {rhs}. This only works when the |<>| notation is enabled. For
330example, to make sure that function key 8 does nothing at all: >
331 :map <F8> <Nop>
332 :map! <F8> <Nop>
333<
334 *<Leader>* *mapleader*
335To define a mapping which uses the "mapleader" variable, the special string
336"<Leader>" can be used. It is replaced with the string value of "mapleader".
337If "mapleader" is not set or empty, a backslash is used instead. Example: >
338 :map <Leader>A oanother line<Esc>
339Works like: >
340 :map \A oanother line<Esc>
341But after: >
342 :let mapleader = ","
343It works like: >
344 :map ,A oanother line<Esc>
345
346Note that the value of "mapleader" is used at the moment the mapping is
347defined. Changing "mapleader" after that has no effect for already defined
348mappings.
349
350 *<LocalLeader>* *maplocalleader*
351Just like <Leader>, except that it uses "maplocalleader" instead of
352"mapleader". <LocalLeader> is to be used for mappings which are local to a
353buffer. Example: >
354 :map <LocalLeader>q \DoItNow
355<
356In a global plugin <Leader> should be used and in a filetype plugin
357<LocalLeader>. "mapleader" and "maplocalleader" can be equal. Although, if
358you make them different, there is a smaller chance of mappings from global
359plugins to clash with mappings for filetype plugins. For example, you could
360keep "mapleader" at the default backslash, and set "maplocalleader" to an
361underscore.
362
363 *map-<SID>*
364In a script the special key name "<SID>" can be used to define a mapping
365that's local to the script. See |<SID>| for details.
366
367 *<Plug>*
368The special key name "<Plug>" can be used for an internal mapping, which is
369not to be matched with any key sequence. This is useful in plugins
370|using-<Plug>|.
371
372 *<Char>* *<Char->*
373To map a character by its decimal, octal or hexadecimal number the <Char>
374construct can be used:
375 <Char-123> character 123
376 <Char-033> character 27
377 <Char-0x7f> character 127
378This is useful to specify a (multi-byte) character in a 'keymap' file.
379Upper and lowercase differences are ignored.
380
381 *map-comments*
382It is not possible to put a comment after these commands, because the '"'
383character is considered to be part of the {lhs} or {rhs}.
384
385 *map_bar*
386Since the '|' character is used to separate a map command from the next
387command, you will have to do something special to include a '|' in {rhs}.
388There are three methods:
389 use works when example ~
390 <Bar> '<' is not in 'cpoptions' :map _l :!ls <Bar> more^M
391 \| 'b' is not in 'cpoptions' :map _l :!ls \| more^M
392 ^V| always, in Vim and Vi :map _l :!ls ^V| more^M
393
394(here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
395cannot use the <> notation "<C-V>" here).
396
397All three work when you use the default setting for 'cpoptions'.
398
399When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
400ending in a '\' and then another command. This is Vi compatible, but
401illogical when compared to other commands.
402
403 *map_return*
404When you have a mapping that contains an Ex command, you need to put a line
405terminator after it to have it executed. The use of <CR> is recommended for
406this (see |<>|). Example: >
407 :map _ls :!ls -l %<CR>:echo "the end"<CR>
408
409To avoid mapping of the characters you type in insert or Command-line mode,
410type a CTRL-V first. The mapping in Insert mode is disabled if the 'paste'
411option is on.
412
413Note that when an error is encountered (that causes an error message or beep)
414the rest of the mapping is not executed. This is Vi-compatible.
415
416Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
417and CTRL-X is not mapped. This was done to be able to use all the named
418registers and marks, even when the command with the same name has been
419mapped.
420
421 *map-which-keys*
422If you are going to map something, you will need to choose which key(s) to use
423for the {lhs}. You will have to avoid keys that are used for Vim commands,
424otherwise you would not be able to use those commands anymore. Here are a few
425suggestions:
426- Function keys <F2>, <F3>, etc.. Also the shifted function keys <S-F1>,
427 <S-F2>, etc. Note that <F1> is already used for the help command.
428- Meta-keys (with the ALT key pressed).
429- Use the '_' or ',' character and then any other character. The "_" and ","
430 commands do exist in Vim (see |_| and |,|), but you probably never use them.
431- Use a key that is a synonym for another command. For example: CTRL-P and
432 CTRL-N. Use an extra character to allow more mappings.
433
434See the file "index" for keys that are not used and thus can be mapped without
435losing any builtin function. You can also use ":help {key}^D" to find out if
436a key is used for some command. ({key} is the specific key you want to find
437out about, ^D is CTRL-D).
438
439 *map-examples*
440A few examples (given as you type them, for "<CR>" you type four characters;
441the '<' flag must not be present in 'cpoptions' for this to work). >
442
443 :map <F3> o#include
444 :map <M-g> /foo<CR>cwbar<Esc>
445 :map _x d/END/e<CR>
446 :map! qq quadrillion questions
447<
448 *map-typing*
449Vim will compare what you type with the start of a mapped sequence. If there
450is an incomplete match, it will get more characters until there either is a
451complete match or until there is no match at all. Example: If you map! "qq",
452the first 'q' will not appear on the screen until you type another
453character. This is because Vim cannot know if the next character will be a
454'q' or not. If the 'timeout' option is on (which is the default) Vim will
455only wait for one second (or as long as specified with the 'timeoutlen'
456option). After that it assumes that the 'q' is to be interpreted as such. If
457you type slowly, or your system is slow, reset the 'timeout' option. Then you
458might want to set the 'ttimeout' option.
459
460 *map-keys-fails*
461There is one situation where key codes might not be recognized:
462- Vim can only read part of the key code. Mostly this is only the first
463 character. This happens on some Unix versions in an xterm.
464- The key code is after character(s) that are mapped. E.g., "<F1><F1>" or
465 "g<F1>".
466The result is that the key code is not recognized in this situation, and the
467mapping fails.
468There are two actions needed to avoid this problem:
469- Remove the 'K' flag from 'cpoptions'. This will make Vim wait for the rest
470 of the characters of the function key.
471- When using <F1> to <F4> the actual key code generated may correspond to
472 <xF1> to <xF4>. There are mappings from <xF1> to <F1>, <xF2> to <F2>, etc.,
473 but these are not recognized after another half a mapping. Make sure the
474 key codes for <F1> to <F4> are correct: >
475 :set <F1>=<type CTRL-V><type F1>
476< Type the <F1> as four characters. The part after the "=" must be done with
477 the actual keys, not the literal text.
478Another solution is to use the actual key code in the mapping for the second
479special key: >
480 :map <F1><Esc>OP :echo "yes"<CR>
481Don't type a real <Esc>, Vim will recognize the key code and replace it with
482<F1> anyway.
483
484 *recursive_mapping*
485If you include the {lhs} in the {rhs} you have a recursive mapping. When
486{lhs} is typed, it will be replaced with {rhs}. When the {lhs} which is
487included in {rhs} is encountered it will be replaced with {rhs}, and so on.
488This makes it possible to repeat a command an infinite number of times. The
489only problem is that the only way to stop this is by causing an error. The
490macros to solve a maze uses this, look there for an example. There is one
491exception: If the {rhs} starts with {lhs}, the first character is not mapped
492again (this is Vi compatible).
493For example: >
494 :map ab abcd
495will execute the "a" command and insert "bcd" in the text. The "ab" in the
496{rhs} will not be mapped again.
497
498If you want to exchange the meaning of two keys you should use the :noremap
499command. For example: >
500 :noremap k j
501 :noremap j k
502This will exchange the cursor up and down commands.
503
504With the normal :map command, when the 'remap' option is on, mapping takes
505place until the text is found not to be a part of a {lhs}. For example, if
506you use: >
507 :map x y
508 :map y x
509Vim will replace x with y, and then y with x, etc. When this has happened
510'maxmapdepth' times (default 1000), Vim will give the error message
511"recursive mapping".
512
513 *:map-undo*
514If you include an undo command inside a mapped sequence, this will bring the
515text back in the state before executing the macro. This is compatible with
516the original Vi, as long as there is only one undo command in the mapped
517sequence (having two undo commands in a mapped sequence did not make sense
518in the original Vi, you would get back the text before the first undo).
519
520 *:map-special-keys*
521There are three ways to map a special key:
5221. The Vi-compatible method: Map the key code. Often this is a sequence that
523 starts with <Esc>. To enter a mapping like this you type ":map " and then
524 you have to type CTRL-V before hitting the function key. Note that when
525 the key code for the key is in the termcap (the t_ options), it will
526 automatically be translated into the internal code and become the second
527 way of mapping (unless the 'k' flag is included in 'cpoptions').
5282. The second method is to use the internal code for the function key. To
529 enter such a mapping type CTRL-K and then hit the function key, or use
530 the form "#1", "#2", .. "#9", "#0", "<Up>", "<S-Down>", "<S-F7>", etc.
531 (see table of keys |key-notation|, all keys from <Up> can be used). The
532 first ten function keys can be defined in two ways: Just the number, like
533 "#2", and with "<F>", like "<F2>". Both stand for function key 2. "#0"
534 refers to function key 10, defined with option 't_f10', which may be
535 function key zero on some keyboards. The <> form cannot be used when
536 'cpoptions' includes the '<' flag.
5373. Use the termcap entry, with the form <t_xx>, where "xx" is the name of the
538 termcap entry. Any string entry can be used. For example: >
539 :map <t_F3> G
540< Maps function key 13 to "G". This does not work if 'cpoptions' includes
541 the '<' flag.
542
543The advantage of the second and third method is that the mapping will work on
544different terminals without modification (the function key will be
545translated into the same internal code or the actual key code, no matter what
546terminal you are using. The termcap must be correct for this to work, and you
547must use the same mappings).
548
549DETAIL: Vim first checks if a sequence from the keyboard is mapped. If it
550isn't the terminal key codes are tried (see |terminal-options|). If a
551terminal code is found it is replaced with the internal code. Then the check
552for a mapping is done again (so you can map an internal code to something
553else). What is written into the script file depends on what is recognized.
554If the terminal key code was recognized as a mapping the key code itself is
555written to the script file. If it was recognized as a terminal code the
556internal code is written to the script file.
557
558==============================================================================
5592. Abbreviations *abbreviations* *Abbreviations*
560
561Abbreviations are used in Insert mode, Replace mode and Command-line mode.
562If you enter a word that is an abbreviation, it is replaced with the word it
563stands for. This can be used to save typing for often used long words. And
564you can use it to automatically correct obvious spelling errors.
565Examples:
566
567 :iab ms MicroSoft
568 :iab tihs this
569
570There are three types of abbreviations:
571
572full-id The "full-id" type consists entirely of keyword characters (letters
573 and characters from 'iskeyword' option). This is the most common
574 abbreviation.
575
576 Examples: "foo", "g3", "-1"
577
578end-id The "end-id" type ends in a keyword character, but all the other
579 characters are not keyword characters.
580
581 Examples: "#i", "..f", "$/7"
582
583non-id The "non-id" type ends in a non-keyword character, the other
584 characters may be of any type, excluding space and Tab. {this type
585 is not supported by Vi}
586
587 Examples: "def#", "4/7$"
588
589Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"
590
591An abbreviation is only recognized when you type a non-keyword character.
592This can also be the <Esc> that ends insert mode or the <CR> that ends a
593command. The non-keyword character which ends the abbreviation is inserted
594after the expanded abbreviation. An exception to this is the character <C-]>,
595which is used to expand an abbreviation without inserting any extra
596characters.
597
598Example: >
599 :ab hh hello
600< "hh<Space>" is expanded to "hello<Space>"
601 "hh<C-]>" is expanded to "hello"
602
603The characters before the cursor must match the abbreviation. Each type has
604an additional rule:
605
606full-id In front of the match is a non-keyword character, or this is where
607 the line or insertion starts. Exception: When the abbreviation is
608 only one character, it is not recognized if there is a non-keyword
609 character in front of it, other than a space or a <Tab>.
610
611end-id In front of the match is a keyword character, or a space or a <Tab>,
612 or this is where the line or insertion starts.
613
614non-id In front of the match is a space, <Tab> or the start of the line or
615 the insertion.
616
617Examples: ({CURSOR} is where you type a non-keyword character) >
618 :ab foo four old otters
619< " foo{CURSOR}" is expanded to " four old otters"
620 " foobar{CURSOR}" is not expanded
621 "barfoo{CURSOR}" is not expanded
622>
623 :ab #i #include
624< "#i{CURSOR}" is expanded to "#include"
625 ">#i{CURSOR}" is not expanded
626>
Bram Moolenaar81695252004-12-29 20:58:21 +0000627 :ab ;; <endofline>
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628< "test;;" is not expanded
629 "test ;;" is expanded to "test <endofline>"
630
631To avoid the abbreviation in insert mode: Type part of the abbreviation, exit
632insert mode with <Esc>, re-enter insert mode with "a" and type the rest. Or
633type CTRL-V before the character after the abbreviation.
634To avoid the abbreviation in Command-line mode: Type CTRL-V twice somewhere in
635the abbreviation to avoid it to be replaced. A CTRL-V in front of a normal
636character is mostly ignored otherwise.
637
638It is possible to move the cursor after an abbreviation: >
639 :iab if if ()<Left>
640This does not work if 'cpoptions' includes the '<' flag. |<>|
641
642You can even do more complicated things. For example, to consume the space
643typed after an abbreviation: >
644 func Eatchar(pat)
645 let c = nr2char(getchar())
646 return (c =~ a:pat) ? '' : c
647 endfunc
648 iabbr <silent> if if ()<Left><C-R>=Eatchar('\s')<CR>
649
650There are no default abbreviations.
651
652Abbreviations are never recursive. You can use ":ab f f-o-o" without any
653problem. But abbreviations can be mapped. {some versions of Vi support
654recursive abbreviations, for no apparent reason}
655
656Abbreviations are disabled if the 'paste' option is on.
657
658 *:abbreviate-local* *:abbreviate-<buffer>*
659Just like mappings, abbreviations can be local to a buffer. This is mostly
660used in a |filetype-plugin| file. Example for a C plugin file: >
661 :abb <buffer> FF for (i = 0; i < ; ++i)
662<
663 *:ab* *:abbreviate*
664:ab[breviate] list all abbreviations. The character in the first
665 column indicates the mode where the abbreviation is
666 used: 'i' for insert mode, 'c' for Command-line
667 mode, '!' for both. These are the same as for
668 mappings, see |map-listing|.
669
Bram Moolenaare344bea2005-09-01 20:46:49 +0000670 *:abbreviate-verbose*
671When 'verbose' is non-zero, listing an abbreviation will also display where it
672was last defined. Example: >
673
674 :verbose abbreviate
675 ! teh the
676 Last set from /home/abcd/vim/abbr.vim
677
678See |:verbose-cmd| for more information.
679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680:ab[breviate] {lhs} list the abbreviations that start with {lhs}
681 You may need to insert a CTRL-V (type it twice) to
682 avoid that a typed {lhs} is expanded, since
683 command-line abbreviations apply here.
684
685:ab[breviate] {lhs} {rhs}
686 add abbreviation for {lhs} to {rhs}. If {lhs} already
687 existed it is replaced with the new {rhs}. {rhs} may
688 contain spaces.
689
690 *:una* *:unabbreviate*
691:una[bbreviate] {lhs} Remove abbreviation for {lhs} from the list. If none
692 is found, remove abbreviations in which {lhs} matches
693 with the {rhs}. This is done so that you can even
694 remove abbreviations after expansion. To avoid
695 expansion insert a CTRL-V (type it twice).
696
697 *:norea* *:noreabbrev*
698:norea[bbrev] [lhs] [rhs]
699 same as ":ab", but no remapping for this {rhs} {not
700 in Vi}
701
702 *:ca* *:cabbrev*
703:ca[bbrev] [lhs] [rhs] same as ":ab", but for Command-line mode only. {not
704 in Vi}
705
706 *:cuna* *:cunabbrev*
707:cuna[bbrev] {lhs} same as ":una", but for Command-line mode only. {not
708 in Vi}
709
710 *:cnorea* *:cnoreabbrev*
711:cnorea[bbrev] [lhs] [rhs]
712 same as ":ab", but for Command-line mode only and no
713 remapping for this {rhs} {not in Vi}
714
715 *:ia* *:iabbrev*
716:ia[bbrev] [lhs] [rhs] same as ":ab", but for Insert mode only. {not in Vi}
717
718 *:iuna* *:iunabbrev*
719:iuna[bbrev] {lhs} same as ":una", but for insert mode only. {not in
720 Vi}
721
722 *:inorea* *:inoreabbrev*
723:inorea[bbrev] [lhs] [rhs]
724 same as ":ab", but for Insert mode only and no
725 remapping for this {rhs} {not in Vi}
726
727 *:abc* *:abclear*
728:abc[lear] Remove all abbreviations. {not in Vi}
729
730 *:iabc* *:iabclear*
731:iabc[lear] Remove all abbreviations for Insert mode. {not in Vi}
732
733 *:cabc* *:cabclear*
734:cabc[lear] Remove all abbreviations for Command-line mode. {not
735 in Vi}
736
737 *using_CTRL-V*
738It is possible to use special characters in the rhs of an abbreviation.
739CTRL-V has to be used to avoid the special meaning of most non printable
740characters. How many CTRL-Vs need to be typed depends on how you enter the
741abbreviation. This also applies to mappings. Let's use an example here.
742
743Suppose you want to abbreviate "esc" to enter an <Esc> character. When you
744type the ":ab" command in Vim, you have to enter this: (here ^V is a CTRL-V
745and ^[ is <Esc>)
746
747You type: ab esc ^V^V^V^V^V^[
748
749 All keyboard input is subjected to ^V quote interpretation, so
750 the first, third, and fifth ^V characters simply allow the second,
751 and fourth ^Vs, and the ^[, to be entered into the command-line.
752
753You see: ab esc ^V^V^[
754
755 The command-line contains two actual ^Vs before the ^[. This is
756 how it should appear in your .exrc file, if you choose to go that
757 route. The first ^V is there to quote the second ^V; the :ab
758 command uses ^V as its own quote character, so you can include quoted
Bram Moolenaar81695252004-12-29 20:58:21 +0000759 whitespace or the | character in the abbreviation. The :ab command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 doesn't do anything special with the ^[ character, so it doesn't need
761 to be quoted. (Although quoting isn't harmful; that's why typing 7
762 [but not 8!] ^Vs works.)
763
764Stored as: esc ^V^[
765
766 After parsing, the abbreviation's short form ("esc") and long form
767 (the two characters "^V^[") are stored in the abbreviation table.
768 If you give the :ab command with no arguments, this is how the
769 abbreviation will be displayed.
770
771 Later, when the abbreviation is expanded because the user typed in
772 the word "esc", the long form is subjected to the same type of
773 ^V interpretation as keyboard input. So the ^V protects the ^[
Bram Moolenaar81695252004-12-29 20:58:21 +0000774 character from being interpreted as the "exit Insert mode" character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 Instead, the ^[ is inserted into the text.
776
777Expands to: ^[
778
779[example given by Steve Kirkendall]
780
781==============================================================================
7823. Local mappings and functions *script-local*
783
784When using several Vim script files, there is the danger that mappings and
785functions used in one script use the same name as in other scripts. To avoid
786this, they can be made local to the script.
787
788 *<SID>* *<SNR>* *E81*
789The string "<SID>" can be used in a mapping or menu. This requires that the
790'<' flag is not present in 'cpoptions'.
791 When executing the map command, Vim will replace "<SID>" with the special
792key code <SNR>, followed by a number that's unique for the script, and an
793underscore. Example: >
794 :map <SID>Add
795could define a mapping "<SNR>23_Add".
796
797When defining a function in a script, "s:" can be prepended to the name to
798make it local to the script. But when a mapping is executed from outside of
799the script, it doesn't know in which script the function was defined. To
800avoid this problem, use "<SID>" instead of "s:". The same translation is done
801as for mappings. This makes it possible to define a call to the function in
Bram Moolenaar81695252004-12-29 20:58:21 +0000802a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
804When a local function is executed, it runs in the context of the script it was
805defined in. This means that new functions and mappings it defines can also
806use "s:" or "<SID>" and it will use the same unique number as when the
807function itself was defined. Also, the "s:var" local script variables can be
808used.
809
810When executing an autocommand or a user command, it will run in the context of
811the script it was defined in. This makes it possible that the command calls a
812local function or uses a local mapping.
813
814Otherwise, using "<SID>" outside of a script context is an error.
815
816If you need to get the script number to use in a complicated script, you can
817use this trick: >
818 :map <SID>xx <SID>xx
819 :let s:sid = maparg("<SID>xx")
820 :unmap <SID>xx
821And remove the trailing "xx".
822
823The "<SNR>" will be shown when listing functions and mappings. This is useful
824to find out what they are defined to.
825
826The |:scriptnames| command can be used to see which scripts have been sourced
827and what their <SNR> number is.
828
829This is all {not in Vi} and {not available when compiled without the +eval
830feature}.
831
832==============================================================================
8334. User-defined commands *user-commands*
834
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000835It is possible to define your own Ex commands. A user-defined command can act
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836just like a built-in command (it can have a range or arguments, arguments can
837be completed as filenames or buffer names, etc), except that when the command
838is executed, it is transformed into a normal ex command and then executed.
839
840For starters: See section |40.2| in the user manual.
841
842 *E183* *user-cmd-ambiguous*
843All user defined commands must start with an uppercase letter, to avoid
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000844confusion with builtin commands. (There are a few builtin commands, notably
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845:Next, :Print and :X, which do start with an uppercase letter. The builtin
846will always take precedence in these cases). The other characters of the user
847command can be uppercase letters, lowercase letters or digits. When using
848digits, note that other commands that take a numeric argument may become
849ambiguous. For example, the command ":Cc2" could be the user command ":Cc2"
850without an argument, or the command ":Cc" with argument "2". It is advised to
851put a space between the command name and the argument to avoid these problems.
852
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000853When using a user-defined command, the command can be abbreviated. However, if
854an abbreviation is not unique, an error will be issued. Furthermore, a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855built-in command will always take precedence.
856
857Example: >
858 :command Rename ...
859 :command Renumber ...
860 :Rena " Means "Rename"
861 :Renu " Means "Renumber"
862 :Ren " Error - ambiguous
863 :command Paste ...
864 :P " The built-in :Print
865
866It is recommended that full names for user-defined commands are used in
867scripts.
868
869:com[mand] *:com* *:command*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000870 List all user-defined commands. When listing commands,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 the characters in the first two columns are
872 ! Command has the -bang attribute
873 " Command has the -register attribute
874 b Command is local to current buffer
875 (see below for details on attributes)
876
877:com[mand] {cmd} List the user-defined commands that start with {cmd}
878
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000879 *:command-verbose*
880When 'verbose' is non-zero, listing a command will also display where it was
881last defined. Example: >
882
883 :verbose command TOhtml
884 Name Args Range Complete Definition
885 TOhtml 0 % :call Convert2HTML(<line1>, <line2>)
886 Last set from /usr/share/vim/vim-7.0/plugin/tohtml.vim
887<
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888See |:verbose-cmd| for more information.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 *E174* *E182*
891:com[mand][!] [{attr}...] {cmd} {rep}
892 Define a user command. The name of the command is
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000893 {cmd} and its replacement text is {rep}. The command's
894 attributes (see below) are {attr}. If the command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 already exists, an error is reported, unless a ! is
896 specified, in which case the command is redefined.
897
898:delc[ommand] {cmd} *:delc* *:delcommand* *E184*
899 Delete the user-defined command {cmd}.
900
901:comc[lear] *:comc* *:comclear*
902 Delete all user-defined commands.
903
904Command attributes
905
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000906User-defined commands are treated by Vim just like any other ex commands. They
907can have arguments, or have a range specified. Arguments are subject to
908completion as filenames, buffers, etc. Exactly how this works depends upon the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909command's attributes, which are specified when the command is defined.
910
911There are a number of attributes, split into four categories: argument
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000912handling, completion behavior, range handling, and special cases. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913attributes are described below, by category.
914
915Argument handling *E175* *E176*
916
917By default, a user defined command will take no arguments (and an error is
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000918reported if any are supplied). However, it is possible to specify that the
919command can take arguments, using the -nargs attribute. Valid cases are:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921 -nargs=0 No arguments are allowed (the default)
922 -nargs=1 Exactly one argument is required
923 -nargs=* Any number of arguments are allowed (0, 1, or many)
924 -nargs=? 0 or 1 arguments are allowed
925 -nargs=+ Arguments must be supplied, but any number are allowed
926
927Arguments are considered to be separated by (unescaped) spaces or Tabs in this
928context.
929
930Note that arguments are used as text, not as expressions. Specifically,
931"s:var" will use the script-local variable in the script where the command was
932defined, not where it is invoked! Example:
933 script1.vim: >
934 :let s:error = "None"
935 :command -nargs=1 Error echoerr <args>
936< script2.vim: >
937 :source script1.vim
938 :let s:error = "Wrong!"
939 :Error s:error
940Executing script2.vim will result in "None" to be echoed. Not what you
941intended! Calling a function may be an alternative.
942
943Completion behavior *:command-completion*
944 *E179* *E180* *E181*
945By default, the arguments of user defined commands do not undergo completion.
946However, by specifying one or the other of the following attributes, argument
947completion can be enabled:
948
949 -complete=augroup autocmd groups
950 -complete=buffer buffer names
951 -complete=command Ex command (and arguments)
952 -complete=dir directory names
953 -complete=environment environment variable names
954 -complete=event autocommand events
955 -complete=expression Vim expression
956 -complete=file file and directory names
957 -complete=function function name
958 -complete=help help subjects
959 -complete=highlight highlight groups
960 -complete=mapping mapping name
961 -complete=menu menus
962 -complete=option options
963 -complete=tag tags
964 -complete=tag_listfiles tags, file names are shown when CTRL-D is hit
965 -complete=var user variables
966 -complete=custom,{func} custom completion, defined via {func}
Bram Moolenaara466c992005-07-09 21:03:22 +0000967 -complete=customlist,{func} custom completion, defined via {func}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
Bram Moolenaara5792f52005-11-23 21:25:05 +0000969
970Custom completion *:command-completion-custom*
971 *:command-completion-customlist*
972 *E467* *E468*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973It is possible to define customized completion schemes via the "custom,{func}"
Bram Moolenaara466c992005-07-09 21:03:22 +0000974or the "customlist,{func}" completion argument. The {func} part should be a
975function with the following prototype >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 :function {func}(ArgLead, CmdLine, CursorPos)
978
Bram Moolenaara466c992005-07-09 21:03:22 +0000979The function need not use all these arguments. The function should provide the
980completion candidates as the return value.
981
982For the "custom" argument, the function should return the completion
983candidates one per line in a newline separated string.
984
985For the "customlist" argument, the function should return the completion
Bram Moolenaara5792f52005-11-23 21:25:05 +0000986candidates as a Vim List. Non-string items in the list are ignored.
Bram Moolenaara466c992005-07-09 21:03:22 +0000987
988The function arguments are:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 ArgLead the leading portion of the argument currently being
990 completed on
991 CmdLine the entire command line
Bram Moolenaara5792f52005-11-23 21:25:05 +0000992 CursorPos the cursor position in it (byte index)
Bram Moolenaara466c992005-07-09 21:03:22 +0000993The function may use these for determining context. For the "custom"
994argument, it is not necessary to filter candidates against the (implicit
995pattern in) ArgLead. Vim will do filter the candidates with its regexp engine
996after function return, and this is probably more efficient in most cases. For
997the "customlist" argument, Vim will not filter the returned completion
998candidates and the user supplied function should filter the candidates.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000The following example lists user names to a Finger command >
1001 :com -complete=custom,ListUsers -nargs=1 Finger !finger <args>
1002 :fun ListUsers(A,L,P)
1003 : return system("cut -d: -f1 /etc/passwd")
1004 :endfun
1005
Bram Moolenaara466c992005-07-09 21:03:22 +00001006The following example completes filenames from the directories specified in
1007the 'path' option: >
1008 :com -nargs=1 -bang -complete=customlist,EditFileComplete
1009 \ EditFile edit<bang> <args>
1010 :fun EditFileComplete(A,L,P)
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00001011 : return split(globpath(&path, a:ArgLead), "\n")
Bram Moolenaara466c992005-07-09 21:03:22 +00001012 :endfun
1013<
Bram Moolenaara5792f52005-11-23 21:25:05 +00001014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015Range handling *E177* *E178*
1016
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017By default, user-defined commands do not accept a line number range. However,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018it is possible to specify that the command does take a range (the -range
1019attribute), or that it takes an arbitrary count value, either in the line
1020number position (-range=N, like the |:split| command) or as a "count"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001021argument (-count=N, like the |:Next| command). Possible attributes are:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 -range Range allowed, default is current line
1024 -range=% Range allowed, default is whole file (1,$)
1025 -range=N A count (default N) which is specified in the line
1026 number position (like |:split|)
1027 -count=N A count (default N) which is specified either in the line
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00001028 number position, or as an initial argument (like |:Next|).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 Specifying -count (without a default) acts like -count=0
1030
1031Note that -range=N and -count=N are mutually exclusive - only one should be
1032specified.
1033
1034Special cases
1035
1036There are some special cases as well:
1037
1038 -bang The command can take a ! modifier (like :q or :w)
1039 -bar The command can be followed by a "|" and another command.
1040 A "|" inside the command argument is not allowed then.
1041 Also checks for a " to start a comment.
1042 -register The first argument to the command can be an optional
1043 register name (like :del, :put, :yank).
1044 -buffer The command will only be available in the current buffer.
1045
1046In the cases of the -count and -register attributes, if the optional argument
1047is supplied, it is removed from the argument list and is available to the
1048replacement text separately.
1049
1050Replacement text
1051
1052The replacement text for a user defined command is scanned for special escape
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001053sequences, using <...> notation. Escape sequences are replaced with values
1054from the entered command line, and all other text is copied unchanged. The
1055resulting string is executed as an Ex command. If the initial < of an escape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056sequence is preceded by a backslash, the sequence is copied unchanged.
1057
1058The valid escape sequences are
1059
1060 *<line1>*
1061 <line1> The starting line of the command range.
1062 *<line2>*
1063 <line2> The final line of the command range.
1064 *<count>*
1065 <count> Any count supplied (as described for the '-range'
1066 and '-count' attributes).
1067 *<bang>*
1068 <bang> (See the '-bang' attribute) Expands to a ! if the
1069 command was executed with a ! modifier, otherwise
1070 expands to nothing.
1071 *<reg>* *<register>*
1072 <reg> (See the '-register' attribute) The optional register,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001073 if specified. Otherwise, expands to nothing. <register>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 is a synonym for this.
1075 *<args>*
1076 <args> The command arguments, exactly as supplied (but as
1077 noted above, any count or register can consume some
1078 of the arguments, which are then not part of <args>).
1079 <lt> A single '<' (Less-Than) character. This is needed if you
1080 want to get a literal copy of one of these escape sequences
1081 into the expansion - for example, to get <bang>, use
1082 <lt>bang>.
1083
1084 *<q-args>*
1085If the first two characters of an escape sequence are "q-" (for example,
1086<q-args>) then the value is quoted in such a way as to make it a valid value
1087for use in an expression. This uses the argument as one single value.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001088When there is no argument <q-args> is an empty string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090To allow commands to pass their arguments on to a user-defined function, there
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001091is a special form <f-args> ("function args"). This splits the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092arguments at spaces and Tabs, quotes each argument individually, and the
1093<f-args> sequence is replaced by the comma-separated list of quoted arguments.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001094See the Mycmd example below. If no arguments are given <f-args> is removed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096Examples >
1097
1098 " Delete everything after here to the end
1099 :com Ddel +,$d
1100
1101 " Rename the current buffer
1102 :com -nargs=1 -bang -complete=file Ren f <args>|w<bang>
1103
1104 " Replace a range with the contents of a file
1105 " (Enter this all as one line)
1106 :com -range -nargs=1 -complete=file
1107 Replace <line1>-pu_|<line1>,<line2>d|r <args>|<line1>d
1108
1109 " Count the number of lines in the range
Bram Moolenaar81695252004-12-29 20:58:21 +00001110 :com! -range -nargs=0 Lines echo <line2> - <line1> + 1 "lines"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
1112 " Call a user function (example of <f-args>)
1113 :com -nargs=* Mycmd call Myfunc(<f-args>)
1114
1115When executed as: >
1116 :Mycmd arg1 arg2
1117This will invoke: >
1118 :call Myfunc("arg1","arg2")
1119
1120 :" A more substantial example
1121 :function Allargs(command)
1122 : let i = 0
1123 : while i < argc()
1124 : if filereadable(argv(i))
1125 : execute "e " . argv(i)
1126 : execute a:command
1127 : endif
1128 : let i = i + 1
1129 : endwhile
1130 :endfunction
1131 :command -nargs=+ -complete=command Allargs call Allargs(<q-args>)
1132
1133The command Allargs takes any Vim command(s) as argument and executes it on all
1134files in the argument list. Usage example (note use of the "e" flag to ignore
1135errors and the "update" command to write modified buffers): >
1136 :Allargs %s/foo/bar/ge|update
1137This will invoke: >
1138 :call Allargs("%s/foo/bar/ge|update")
1139<
1140When defining an user command in a script, it will be able to call functions
1141local to the script and use mappings local to the script. When the user
1142invokes the user command, it will run in the context of the script it was
1143defined in. This matters if |<SID>| is used in a command.
1144
1145 vim:tw=78:ts=8:ft=help:norl: