Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / share / vim / vim58 / doc / map.txt
blob3e4f1cdab9509ba0eeedebfbcd02b1be2d9e9194
1 *map.txt*       For Vim version 5.8.  Last change: 2001 Jan 30
4                   VIM REFERENCE MANUAL    by Bram Moolenaar
7 Key mapping and abbreviations.
9 1. Key mapping                  |key-mapping|
10 2. Abbreviations                |abbreviations|
11 3. User-defined commands        |user-commands|
13 ==============================================================================
14 1. Key mapping                          *key-mapping* *mapping* *macro*
16 There are commands to enter new mappings, remove mappings and list mappings.
17 See |map-overview| for the various forms of "map" and their relationships with
18 modes.
20 :map     {lhs} {rhs}                                    *:map*
21 :nm[ap]  {lhs} {rhs}                                    *:nm* *:nmap*
22 :vm[ap]  {lhs} {rhs}                                    *:vm* *:vmap*
23 :om[ap]  {lhs} {rhs}                                    *:om* *:omap*
24 :map!    {lhs} {rhs}                                    *:map!*
25 :im[ap]  {lhs} {rhs}                                    *:im* *:imap*
26 :cm[ap]  {lhs} {rhs}                                    *:cm* *:cmap*
27                         Map the key sequence {lhs} to {rhs} for the modes
28                         where the map command applies.  The result, including
29                         {rhs}, is then further scanned for mappings.  This
30                         allows for nested and recursive use of mappings.
33 :no[remap]   {lhs} {rhs}                                *:no*  *:noremap*
34 :nn[oremap]  {lhs} {rhs}                                *:nn*  *:nnoremap*
35 :vn[oremap]  {lhs} {rhs}                                *:vn*  *:vnoremap*
36 :ono[remap]  {lhs} {rhs}                                *:ono* *:onoremap*
37 :no[remap]!  {lhs} {rhs}                                *:no!* *:noremap!*
38 :ino[remap]  {lhs} {rhs}                                *:ino* *:inoremap*
39 :cno[remap]  {lhs} {rhs}                                *:cno* *:cnoremap*
40                         Map the key sequence {lhs} to {rhs} for the modes
41                         where the map command applies.  Disallow mapping of
42                         {rhs}, to avoid nested and recursive mappings.  Often
43                         used to redefine a command.  {not in Vi}
46 :unm[ap]   {lhs}                                        *:unm*  *:unmap*
47 :nun[map]  {lhs}                                        *:nun*  *:nunmap*
48 :vu[nmap]  {lhs}                                        *:vu*   *:vunmap*
49 :ou[nmap]  {lhs}                                        *:ou*   *:ounmap*
50 :unm[ap]!  {lhs}                                        *:unm!* *:unmap!*
51 :iu[nmap]  {lhs}                                        *:iu*   *:iunmap*
52 :cu[nmap]  {lhs}                                        *:cu*   *:cunmap*
53                         Remove the mapping of {lhs} for the modes where the
54                         map command applies.  The mapping may remain defined
55                         for other modes where it applies.
56                         Note: Trailing spaces are included in the {lhs}.  This
57                         unmap does NOT work:
58 >                               :map @@ foo
59 >                               :unmap @@ | print
61 :mapc[lear]                                             *:mapc*   *:mapclear*
62 :nmapc[lear]                                            *:nmapc*  *:nmapclear*
63 :vmapc[lear]                                            *:vmapc*  *:vmapclear*
64 :omapc[lear]                                            *:omapc*  *:omapclear*
65 :mapc[lear]!                                            *:mapc!*  *:mapclear!*
66 :imapc[lear]                                            *:imapc*  *:imapclear*
67 :cmapc[lear]                                            *:cmapc*  *:cmapclear*
68                         Remove ALL mappings for the modes where the map
69                         command applies.  {not in Vi}
70                         Warning: This also removes the default mappings.
72 :map
73 :nm[ap]
74 :vm[ap]
75 :om[ap]
76 :map!
77 :im[ap]
78 :cm[ap]
79                         List all key mappings for the modes where the map
80                         command applies.  Note that ":map" and ":map!" are
81                         used most often, because they include the other modes.
83 :map     {lhs}                                          *:map_l*
84 :nm[ap]  {lhs}                                          *:nmap_l*
85 :vm[ap]  {lhs}                                          *:vmap_l*
86 :om[ap]  {lhs}                                          *:omap_l*
87 :map!    {lhs}                                          *:map_l!*
88 :im[ap]  {lhs}                                          *:imap_l*
89 :cm[ap]  {lhs}                                          *:cmap_l*
90                         List the key mappings for the key sequences starting
91                         with {lhs} in the modes where the map command applies.
92                         {not in Vi}
94 These commands are used to map a key or key sequence to a string of
95 characters.  You can use this to put command sequences under function keys,
96 translate one key into another, etc.  See |:mkexrc| for how to save and
97 restore the current mappings.
99 There are five sets of mappings
100 - For Normal mode: When typing commands.
101 - For Visual mode: When typing commands while the Visual area is highlighted.
102 - For Operator-pending mode: When an operator is pending (after "d", "y", "c",
103   etc.).
104 - For Insert mode. These are also used in Replace mode.
105 - For Command-line mode: When entering a ":" or "/" command.
107 There are no separate mappings for Select mode.  The same as for Visual mode
108 are used |Select-mode-mapping|.
110                                                 *map-overview* *map-modes*
111 Overview of which map command works in which mode:
113     commands:                                 modes:                          ~
114                                           Normal     Visual  Operator-pending ~
115 :map   :noremap   :unmap   :mapclear         X          X          X
116 :nmap  :nnoremap  :nunmap  :nmapclear        X          .          .
117 :vmap  :vnoremap  :vunmap  :vmapclear        .          X          .
118 :omap  :onoremap  :ounmap  :omapclear        .          .          X
120                                           Insert Command-line           ~
121 :map!  :noremap!  :unmap!  :mapclear!        X         X
122 :imap  :inoremap  :iunmap  :imapclear        X         .
123 :cmap  :cnoremap  :cunmap  :cmapclear        .         X
125 The original Vi did not have separate mappings for
126 Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
127 Therefore the ":map" and ":map!" commands enter and display mappings for
128 several modes.  In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
129 ":imap" commands to enter mappings for each mode separately.
131 To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
132 first define it for all three modes, then unmap it for Operator-pending mode:
133         :map    xx something-difficult
134         :ounmap xx
135 Likewise for a mapping for Visual and Operator-pending mode or Normal and
136 Operator-pending mode.
138                                                         *map-listing*
139 When listing mappings the characters in the first two columns are:
141         CHAR            MODE    ~
142         <Space>         Normal, Visual and Operator-pending
143          n              Normal
144          v              Visual
145          o              Operator-pending
146          !              Insert and Command-line
147          i              Insert
148          c              Command-line
150 A "*" just before the {rhs} indicates that it is not remappable.
152 Everything from the first non-blank after {lhs} up to the end of the line
153 (or '|') is considered to be part of {rhs}.  This allows the {rhs} to end
154 with a space.
156 Note: When using mappings for Visual mode, you can use the "'<" mark, which
157 is the start of the last selected Visual area in the current buffer |'<|.
159                                                         *map_backslash*
160 Note that only CTRL-V is mentioned here as a special character for mappings
161 and abbreviations.  When 'cpoptions' does not contain 'B', a backslash can
162 also be used like CTRL-V.  The <> notation can be fully used then |<>|.  But
163 you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
164 follows.
166 To map a backslash, or use a backslash literally in the {rhs}, the special
167 sequence "<Bslash>" can be used.  This avoids the need to double backslashes
168 when using nested mappings.
170                                                         *map_CTRL_C*
171 It's not possible to use a CTRL-C in the {lhs}.  You just can't map CTRL-C.
172 The reason is that CTRL-C must always be available to break a running command.
174                                                         *map_space_in_lhs*
175 To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
176 each space).
177                                                         *map_space_in_rhs*
178 If you want a {rhs} that starts with a space, use "<Space>".  To be fully Vi
179 compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
180 single CTRL-V (you have to type CTRL-V two times).
181                                                         *map_empty_rhs*
182 You can create an empty {rhs} by typing nothing after a single CTRL-V (you
183 have to type CTRL-V two times).  Unfortunately, you cannot do this in a vimrc
184 file.
185                                                         *<Nop>*
186 A easier way to get a mapping that doesn't produce anything, is to use "<Nop>"
187 for the {rhs}.  This only works when the |<>| notation is enabled.  For
188 example, to make sure that function key 8 does nothing at all:
189 >       :map  <F8>  <Nop>
190 >       :map! <F8>  <Nop>
192                                                         *map-comments*
193 It is not possible to put a comment after these commands, because the '"'
194 character is considered to be part of the {lhs} or {rhs}.
196                                                         *map_bar*
197 Since the '|' character is used to separate a map command from the next
198 command, you will have to do something special to include  a '|' in {rhs}.
199 There are three methods:
200    use       works when                    example      ~
201    <Bar>     '<' is not in 'cpoptions'     :map _l :!ls <Bar> more^M
202    \|        'b' is not in 'cpoptions'     :map _l :!ls \| more^M
203    ^V|       always, in Vim and Vi         :map _l :!ls ^V| more^M
205 (here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
206 cannot use the <> notation "<C-V>" here).
208 All three work when you use the default setting for 'cpoptions'.
210 When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
211 ending in a '\' and then another command.  This is Vi compatible, but
212 illogical when compared to other commands.
214                                                         *map_return*
215 When you have a mapping that contains an Ex command, you need to put a line
216 terminator after it to have it executed.  The use of <CR> is recommended for
217 this (see |<>|).  Example:
218 >  :map  _ls  :!ls -l %<CR>:echo "the end"<CR>
220 To avoid mapping of the characters you type in insert or Command-line mode,
221 type a CTRL-V first.  The mapping in Insert mode is disabled if the 'paste'
222 option is on.
224 Note that when an error is encountered (that causes an error message) the rest
225 of the mapping is not executed.  This is Vi-compatible.
227 Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
228 and CTRL-X is not mapped.  This was done to be able to use all the named
229 registers and marks, even when the command with the same name has been
230 mapped.
232                                                         *map-which-keys*
233 If you are going to map something, you will need to choose which key(s) to use
234 for the {lhs}.  You will have to avoid keys that are used for Vim commands,
235 otherwise you would not be able to use those commands anymore.  Here are a few
236 suggestions:
237 - Function keys <F2>, <F3>, etc..  Also the shifted function keys <S-F1>,
238   <S-F2>, etc.  Note that <F1> is already used for the help command.
239 - Meta-keys (with the ALT key pressed).
240 - Use the '_' or ',' character and then any other character.  The "_" and ","
241   commands do exist in Vim (see |_| and |,|), but you probably never use them.
242 - Use a key that is a synonym for another command.  For example: CTRL-P and
243   CTRL-N.  Use an extra character to allow more mappings.
245 See the file "index" for keys that are not used and thus can be mapped without
246 losing any builtin function.  You can also use ":help {key}^D" to find out if
247 a key is used for some command.  ({key} is the specific key you want to find
248 out about, ^D is CTRL-D).
250                                                         *map-examples*
251 A few examples (given as you type them, for "<CR>" you type four characters;
252 the '<' flag must not be present in 'cpoptions' for this to work).
254 >  :map <F3>  o#include
255 >  :map <M-g> /foo<CR>cwbar<Esc>
256 >  :map _x    d/END/e<CR>
257 >  :map! qq   quadrillion questions
259                                                         *map-typing*
260 Vim will compare what you type with the start of a mapped sequence.  If there
261 is an incomplete match, it will get more characters until there either is a
262 complete match or until there is no match at all.  Example: If you map! "qq",
263 the first 'q' will not appear on the screen until you type another
264 character.  This is because Vim cannot know if the next character will be a
265 'q' or not.  If the 'timeout' option is on (which is the default) Vim will
266 only wait for one second (or as long as specified with the 'timeoutlen'
267 option).  After that it assumes that the 'q' is to be interpreted as such.  If
268 type slowly, or your system is slow, reset the 'timeout' option.  Then you
269 might want to set the 'ttimeout' option.
271                                                         *map-keys-fails*
272 There is one situation where key codes might not be recognized:
273 - Vim can only read part of the key code.  Mostly this is only the first
274   character.  This happens on some Unix versions in an xterm.
275 - The key code is after character(s) that are mapped.  E.g., "<F1><F1>" or
276   "g<F1>".
277 The result is that the key code is not recognized in this situation, and the
278 mapping fails.
279 There are two actions needed to avoid this problem:
280 - Remove the 'K' flag from 'cpoptions'.  This will make Vim wait for the rest
281   of the characters of the function key.
282 - When using <F1> to <F4> the actual key code generated may correspond to
283   <xF1> to <xF4>.  There are mappings from <xF1> to <F1>, <xF2> to <F2>, etc.,
284   but these are not recognized after another half a mapping.  Make sure the
285   key codes for <F1> to <F4> are correct:
286 >       :set <F1>=<type CTRL-V><type F1>
287   Type the <F1> as four characters.  The part after the "=" must be done with
288   the actual keys, not the literal text.
289 Another solution is to use the actual key code in the mapping for the second
290 special key:
291 >       :map <F1><Esc>OP :echo "yes"<CR>
292 Don't type a real <Esc>, Vim will recognize the key code and replace it with
293 <F1> anyway.
295                                                 *recursive_mapping*
296 If you include the {lhs} in the {rhs} you have a recursive mapping.  When
297 {lhs} is typed, it will be replaced with {rhs}.  When the {lhs} which is
298 included in {rhs} is encountered it will be replaced with {rhs}, and so on.
299 This makes it possible to repeat a command an infinite number of times.  The
300 only problem is that the only way to stop this is by causing an error.  The
301 macros to solve a maze uses this, look there for an example.  There is one
302 exception: If the {rhs} starts with {lhs}, the first character is not mapped
303 again (this is Vi compatible).
304 For example:
305 >  :map ab abcd
306 will execute the "a" command and insert "bcd" in the text.  The "ab" in the
307 {rhs} will not be mapped again.
309 If you want to exchange the meaning of two keys you should use the :noremap
310 command.  For example:
311 >  :noremap k j
312 >  :noremap j k
313 This will exchange the cursor up and down commands.
315 With the normal :map command, when the 'remap' option is on, mapping takes
316 place until the text is found not to be a part of a {lhs}.  For example, if
317 you use:
318 >  :map x y
319 >  :map y x
320 Vim will replace x with y, and then y with x, etc.  When this has happened
321 'maxmapdepth' times (default 1000), Vim will give the error message
322 "recursive mapping".
324                                                         *:map-undo*
325 If you include an undo command inside a mapped sequence, this will bring the
326 text back in the state before executing the macro.  This is compatible with
327 the original Vi, as long as there is only one undo command in the mapped
328 sequence (having two undo commands in a mapped sequence did not make sense
329 in the original Vi, you would get back the text before the first undo).
331                                                         *:map-special-keys*
332 There are three ways to map a special key:
333 1. The Vi-compatible method: Map the key code.  Often this is a sequence that
334    starts with <Esc>.  To enter a mapping like this you type ":map " and then
335    you have to type CTRL-V before hitting the function key.  Note that when
336    the key code for the key is in the termcap (the t_ options), it will
337    automatically be translated into the internal code and become the second
338    way of mapping (unless the 'k' flag is included in 'cpoptions').
339 2. The second method is to use the internal code for the function key.  To
340    enter such a mapping type CTRL-K and then hit the function key, or use
341    the form "#1", "#2", .. "#9", "#0", "<Up>", "<S-Down>", "<S-F7>", etc.
342    (see table of keys |key-notation|, all keys from <Up> can be used).  The
343    first ten function keys can be defined in two ways: Just the number, like
344    "#2", and with "<F>", like "<F2>".  Both stand for function key 2.  "#0"
345    refers to function key 10, defined with option 't_f10', which may be
346    function key zero on some keyboards.  The <> form cannot be used when
347    'cpoptions' includes the '<' flag.
348 3. Use the termcap entry, with the form <t_xx>, where "xx" is the name of the
349    termcap entry.  Any string entry can be used.  For example:
350 >    :map <t_F3> G
351    Maps function key 13 to "G".  This does not work if 'cpoptions' includes
352    the '<' flag.
354 The advantage of the second and third method is that the mapping will work on
355 different terminals without modification (the function key will be
356 translated into the same internal code or the actual key code, no matter what
357 terminal you are using.  The termcap must be correct for this to work, and you
358 must use the same mappings).
360 DETAIL: Vim first checks if a sequence from the keyboard is mapped.  If it
361 isn't the terminal key codes are tried (see |terminal-options|).  If a
362 terminal code is found it is replaced with the internal code.  Then the check
363 for a mapping is done again (so you can map an internal code to something
364 else).  What is written into the script file depends on what is recognized.
365 If the terminal key code was recognized as a mapping the key code itself is
366 written to the script file.  If it was recognized as a terminal code the
367 internal code is written to the script file.
369 ==============================================================================
370 2. Abbreviations                        *abbreviations* *Abbreviations*
372 Abbreviations are used in Insert mode, Replace mode and Command-line mode.
373 If you enter a word that is an abbreviation, it is replaced with the word it
374 stands for.  This can be used to save typing for often used long words.  And
375 you can use it to automatically correct obvious spelling errors.
376 Examples:
378         :iab ms MicroSoft
379         :iab tihs this
381 There are three types of abbreviations:
383 full-id   The "full-id" type consists entirely of keyword characters (letters
384           and characters from 'iskeyword' option).  This is the most common
385           abbreviation.
387           Examples: "foo", "g3", "-1"
389 end-id    The "end-id" type ends in a keyword character, but all the other
390           characters are not keyword characters.
392           Examples: "#i", "..f", "$/7"
394 non-id    The "non-id" type ends in a non-keyword character, the other
395           characters may be of any type, excluding space and Tab.  {this type
396           is not supported by Vi}
398           Examples: "def#", "4/7$"
400 Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"
402 An abbreviation is only recognized when you type a non-keyword character.
403 This can also be the <Esc> that ends insert mode or the <CR> that ends a
404 command.  The non-keyword character which ends the abbreviation is inserted
405 after the expanded abbreviation.  An exception to this is the character <C-]>,
406 which is used to expand an abbreviation without inserting any extra
407 characters.
409 Example:
410 >   :ab hh      hello
411             "hh<Space>" is expanded to "hello<Space>"
412             "hh<C-]>" is expanded to "hello"
414 The characters before the cursor must match the abbreviation.  Each type has
415 an additional rule:
417 full-id   In front of the match is a non-keyword character, or this is where
418           the line or insertion starts.  Exception: When the abbreviation is
419           only one character, it is not recognized if there is a non-keyword
420           character in front of it, other than a space or a <Tab>.
422 end-id    In front of the match is a keyword character, or a space or a <Tab>,
423           or this is where the line or insertion starts.
425 non-id    In front of the match is a space, <Tab> or the start of the line or
426           the insertion.
428 Examples: ({CURSOR} is where you type a non-keyword character)
429 >  :ab foo   four old otters
430                 " foo{CURSOR}"    is expanded to " four old otters"
431                 " foobar{CURSOR}" is not expanded
432                 "barfoo{CURSOR}"  is not expanded
434 >  :ab #i #include
435                 "#i{CURSOR}"      is expanded to "#include"
436                 ">#i{CURSOR}"     is not expanded
438 >  :ab ;; <endofline>"
439                 "test;;"          is not expanded
440                 "test ;;"         is expanded to "test <endofline>"
442 To avoid the abbreviation in insert mode: Type part of the abbreviation, exit
443 insert mode with <Esc>, re-enter insert mode with "a" and type the rest.  Or
444 type CTRL-V before the character after the abbreviation.
445 To avoid the abbreviation in Command-line mode: Type CTRL-V twice somewhere in
446 the abbreviation to avoid it to be replaced.  A CTRL-V in front of a normal
447 character is mostly ignored otherwise.
449 It is possible to move the cursor after an abbreviation:
450 >  :iab if if ()<Left>
451 This does not work if 'cpoptions' includes the '<' flag. |<>|
453 There are no default abbreviations.
455 Abbreviations are never recursive.  You can use ":ab f f-o-o" without any
456 problem.  But abbreviations can be mapped.  {some versions of Vi support
457 recursive abbreviations, for no apparent reason}
459 Abbreviations are disabled if the 'paste' option is on.
461                                                 *:ab* *:abbreviate*
462 :ab[breviate]           list all abbreviations.  The character in the first
463                         column indicates the mode where the abbreviation is
464                         used: 'i' for insert mode, 'c' for Command-line
465                         mode, '!' for both.
467 :ab[breviate] {lhs}     list the abbreviations that start with {lhs}
469 :ab[breviate] {lhs} {rhs}
470                         add abbreviation for {lhs} to {rhs}.  If {lhs} already
471                         existed it is replaced with the new {rhs}.  {rhs} may
472                         contain spaces.
474                                                 *:una* *:unabbreviate*
475 :una[bbreviate] {lhs}   remove abbreviation for {lhs} from the list
477                                                 *:norea* *:noreabbrev*
478 :norea[bbrev] [lhs] [rhs]
479                         same as ":ab", but no remapping for this {rhs} {not
480                         in Vi}
482                                                 *:ca* *:cabbrev*
483 :ca[bbrev] [lhs] [rhs]  same as ":ab", but for Command-line mode only.  {not
484                         in Vi}
486                                                 *:cuna* *:cunabbrev*
487 :cuna[bbrev] {lhs}      same as ":una", but for Command-line mode only.  {not
488                         in Vi}
490                                                 *:cnorea* *:cnoreabbrev*
491 :cnorea[bbrev] [lhs] [rhs]
492                         same as ":ab", but for Command-line mode only and no
493                         remapping for this {rhs} {not in Vi}
495                                                 *:ia* *:iabbrev*
496 :ia[bbrev] [lhs] [rhs]  same as ":ab", but for Insert mode only.  {not in Vi}
498                                                 *:iuna* *:iunabbrev*
499 :iuna[bbrev] {lhs}      same as ":una", but for insert mode only.  {not in
500                         Vi}
502                                                 *:inorea* *:inoreabbrev*
503 :inorea[bbrev] [lhs] [rhs]
504                         same as ":ab", but for Insert mode only and no
505                         remapping for this {rhs} {not in Vi}
507                                                         *:abc* *:abclear*
508 :abc[lear]              Remove all abbreviations.  {not in Vi}
510                                                         *:iabc* *:iabclear*
511 :iabc[lear]             Remove all abbreviations for Insert mode.  {not in Vi}
513                                                         *:cabc* *:cabclear*
514 :cabc[lear]             Remove all abbreviations for Command-line mode.  {not
515                         in Vi}
517                                                         *using_CTRL-V*
518 It is possible to use special characters in the rhs of an abbreviation.
519 CTRL-V has to be used to avoid the special meaning of most non printable
520 characters.  How many CTRL-Vs need to be typed depends on how you enter the
521 abbreviation.  This also applies to mappings.  Let's use an example here.
523 Suppose you want to abbreviate "esc" to enter an <Esc> character.  When you
524 type the ":ab" command in Vim, you have to enter this: (here ^V is a CTRL-V
525 and ^[ is <Esc>)
527 You type:   ab esc ^V^V^V^V^V^[
529         All keyboard input is subjected to ^V quote interpretation, so
530         the first, third, and fifth ^V  characters simply allow the second,
531         and fourth ^Vs, and the ^[, to be entered into the command-line.
533 You see:    ab esc ^V^V^[
535         The command-line contains two actual ^Vs before the ^[.  This is
536         how it should appear in your .exrc file, if you choose to go that
537         route.  The first ^V is there to quote the second ^V; the :ab
538         command uses ^V as its own quote character, so you can include quoted
539         whitespace or the | character in the abbreviation.   The :ab command
540         doesn't do anything special with the ^[ character, so it doesn't need
541         to be quoted.  (Although quoting isn't harmful; that's why typing 7
542         [but not 8!] ^Vs works.)
544 Stored as:  esc     ^V^[
546         After parsing, the abbreviation's short form ("esc") and long form
547         (the two characters "^V^[") are stored in the abbreviation table.
548         If you give the :ab command with no arguments, this is how the
549         abbreviation will be displayed.
551         Later, when the abbreviation is expanded because the user typed in
552         the word "esc", the long form is subjected to the same type of
553         ^V interpretation as keyboard input.  So the ^V protects the ^[
554         character from being interpreted as the "exit input-mode" character.
555         Instead, the ^[ is inserted into the text.
557 Expands to: ^[
559 [example given by Steve Kirkendall]
561 ==============================================================================
562 3. User-defined commands                                *user-commands*
564 It is possible to define your own ex commands. A user-defined command can act
565 just like a built-in command (it can have a range or arguments, arguments can
566 be completed as filenames or buffer names, etc), except that when the command
567 is executed, it is transformed into a normal ex command and then executed.
569 All user defined commands must start with an uppercase letter, to avoid
570 confusion with builtin commands. (There are a few builtin commands, notably
571 :Next, :Print and :X, which do start with an uppercase letter.  The builtin
572 will always take precedence in these cases).  The other characters of the user
573 command can be uppercase letters, lowercase letters or digits.  When using
574 digits, note that other commands that take a numeric argument may become
575 ambiguous.  For example, the command ":Cc2" could be the user command ":Cc2"
576 without an argument, or the command ":Cc" with argument "2".  It is advised to
577 put a space between the command name and the argument to avoid these problems.
579 When using a user-defined command, the command can be abbreviated. However, if
580 an abbreviation is not unique, an error will be issued. Furthermore, a
581 built-in command will always take precedence.
583 Example:
584 >       :command Rename ...
585 >       :command Renumber ...
586 >       :Rena                           " Means "Rename"
587 >       :Renu                           " Means "Renumber"
588 >       :Ren                            " Error - ambiguous
589 >       :command Paste ...
590 >       :P                              " The built-in :Print
592 It is recommended that full names for user-defined commands are used in
593 scripts.
595 :com[mand]                                              *:com* *:command*
596                         List all user-defined commands. When listing commands,
597                         the characters in the first two columns are
598                             !   Command has the -bang attribute
599                             "   Command has the -register attribute
600                         (see below for details on attributes)
602 :com[mand] {cmd}        List the user-defined commands that start with {cmd}
604 :com[mand][!] [{attr}...] {cmd} {rep}
605                         Define a user command.  The name of the command is
606                         {cmd} and its replacement text is {rep}. The command's
607                         attributes (see below) are {attr}. If the command
608                         already exists, an error is reported, unless a ! is
609                         specified, in which case the command is redefined.
611 :delc[ommand] {cmd}                                     *:delc* *:delcommand*
612                         Delete the user-defined command {cmd}.
614 :comc[lear]                                             *:comc* *:comclear*
615                         Delete all user-defined commands.
617 Command attributes
619 User-defined commands are treated by Vim just like any other ex commands. They
620 can have arguments, or have a range specified. Arguments are subject to
621 completion as filenames, buffers, etc. Exactly how this works depends upon the
622 command's attributes, which are specified when the command is defined.
624 There are a number of attributes, split into four categories: argument
625 handling, completion behavior, range handling, and special cases. The
626 attributes are described below, by category.
628 Argument handling
630 By default, a user defined command will take no arguments (and an error is
631 reported if any are supplied). However, it is possible to specify that the
632 command can take arguments, using the -nargs attribute. Valid cases are:
634         -nargs=0    No arguments are allowed (the default)
635         -nargs=1    Exactly one argument is required
636         -nargs=*    Any number of arguments are allowed (0, 1, or many)
637         -nargs=?    0 or 1 arguments are allowed
638         -nargs=+    Arguments must be supplied, but any number are allowed
640 Arguments are considered to be separated by (unescaped) spaces in this
641 context.
643 Completion behavior
645 By default, the arguments of user defined commands do not undergo completion.
646 However, by specifying one or the other of the following attributes, argument
647 completion can be enabled:
649         -complete=augroup       autocmd groups
650         -complete=buffer        buffer names
651         -complete=command       Ex command (and arguments)
652         -complete=dir           directory names
653         -complete=event         autocommand events
654         -complete=file          file and directory names
655         -complete=help          help subjects
656         -complete=highlight     highlight groups
657         -complete=menu          menus
658         -complete=option        options
659         -complete=tag           tags
660         -complete=tag_listfiles tags, file names are shown when CTRL-D is hit
661         -complete=var           user variables
663 Range handling
665 By default, user-defined commands do not accept a line number range. However,
666 it is possible to specify that the command does take a range (the -range
667 attribute), or that it takes an arbitrary count value, either in the line
668 number position (-range=N, like the |:split| command) or as a "count"
669 argument (-count=N, like the |:Next| command). Possible attributes are:
671         -range      Range allowed, default is current line
672         -range=%    Range allowed, default is whole file (1,$)
673         -range=N    A count (default N) which is specified in the line
674                     number position (like |:split|)
675         -count=N    A count (default N) which is specified either in the line
676                     number position, or as an initial argument (like |:Next|)
677                     Specifying -count (without a default) acts like -count=0
679 Note that -range=N and -count=N are mutually exclusive - only one should be
680 specified.
682 Special cases
684 There are some special cases as well:
686         -bang       The command can take a ! modifier (like :q or :w)
687         -register   The first argument to the command can be an optional
688                     register name (like :del, :put, :yank).
690 In the cases of the -count and -register attributes, if the optional argument
691 is supplied, it is removed from the argument list and is available to the
692 replacement text separately.
694 Replacement text
696 The replacement text for a user defined command is scanned for special escape
697 sequences, using <...> notation. Escape sequences are replaced with values
698 from the entered command line, and all other text is copied unchanged. The
699 resulting string is executed as an Ex command. If the initial < of an escape
700 sequence is preceded by a backslash, the sequence is copied unchanged.
702 The valid escape sequences are
704                                                 *<line1>*
705         <line1> The starting line of the command range.
706                                                 *<line2>*
707         <line2> The final line of the command range.
708                                                 *<count>*
709         <count> Any count supplied (as described for the '-range'
710                 and '-count' attributes).
711                                                 *<bang>*
712         <bang>  (See the '-bang' attribute) Expands to a ! if the
713                 command was executed with a ! modifier, otherwise
714                 expands to nothing.
715                                                 *<reg>* *<register>*
716         <reg>   (See the '-register' attribute) The optional register,
717                 if specified. Otherwise, expands to nothing. <register>
718                 is a synonym for this.
719                                                 *<args>*
720         <args>  The command arguments, exactly as supplied (but as
721                 noted above, any count or register can consume some
722                 of the arguments, which are then not part of <args>).
723         <lt>    A single '<' (Less-Then) character.  This is needed if you
724                 want to get a literal copy of one of these escape sequences
725                 into the expansion - for example, to get <bang>, use
726                 <lt>bang>.
728 If the first two characters of an escape sequence are "q-" (for example,
729 <q-args>) then the value is quoted in such a way as to make it a valid value
730 for use in an expression.  This uses the argument as one single value.
732 To allow commands to pass their arguments on to a user-defined function, there
733 is a special form <f-args> ("function args"). This splits the command
734 arguments at spaces, quotes each argument individually, and the <f-args>
735 sequence is replaced by the comma-separated list of quoted arguments.  See the
736 Mycmd example below.  When there is no argument, <f-args> also has no
737 argument.
739 Examples
741 >   " Delete everything after here to the end
742 >   :com Ddel +,$d
744 >   " Rename the current buffer
745 >   :com -nargs=1 -bang -complete=file Ren f <args>|w<bang>
747 >   " Replace a range with the contents of a file
748 >   " (Enter this all as one line)
749 >   :com -range -nargs=1 -complete=file
750 >        Replace <line1>-pu_|<line1>,<line2>d|r <args>|<line1>d
752 >   " Count the number of lines in the range
753 >   com! -range -nargs=0 Lines :echo <line2> - <line1> + 1 "lines"
755 >   " Call a user function (example of <f-args>)
756 >   :com -nargs=* Mycmd call Myfunc(<f-args>)
758 When executed as:
759 >       :Mycmd arg1 arg2
760 This will invoke:
761 >       :call Myfunc("arg1","arg2")
763 >   " A more substantial example
764 >   :function Allargs(command)
765 >   :   let i = 0
766 >   :   while i < argc()
767 >   :      if filereadable(argv(i))
768 >   :        execute "e " . argv(i)
769 >   :        execute a:command
770 >   :      endif
771 >   :      let i = i + 1
772 >   :   endwhile
773 >   :endfunction
774 >   :command -nargs=+ -complete=command Allargs call Allargs(<q-args>)
776 The command Allargs takes any Vim command(s) as argument and executes it on all
777 files in the argument list.  Usage example (note use of the "e" flag to ignore
778 errors and the "update" command to write modified buffers):
779 >       :Allargs %s/foo/bar/ge|update
780 This will invoke:
781 >       :call Allargs("%s/foo/bar/ge|update")
783  vim:tw=78:ts=8:sw=8: