home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim53os2.zip / vim-5.3 / doc / map.txt < prev    next >
Text File  |  1998-08-30  |  30KB  |  723 lines

  1. *map.txt*       For Vim version 5.3.  Last modification: 1998 Aug 24
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Key mapping and abbreviations.
  8.  
  9. 1. Key mapping            |key-mapping|
  10. 2. Abbreviations        |abbreviations|
  11. 3. User-defined commands    |user-commands|
  12.  
  13. ==============================================================================
  14. 1. Key mapping                        *key-mapping*
  15.  
  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.
  19.  
  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.
  31.  
  32.  
  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}
  44.  
  45.  
  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
  60.  
  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.  
  71. :map
  72. :nm[ap]
  73. :vm[ap]
  74. :om[ap]
  75. :map!
  76. :im[ap]
  77. :cm[ap]
  78.             List all key mappings for the modes where the map
  79.             command applies.  Note that ":map" and ":map!" are
  80.             used most often, because they include the other modes.
  81.  
  82. :map     {lhs}                        *:map_l*
  83. :nm[ap]  {lhs}                        *:nmap_l*
  84. :vm[ap]  {lhs}                        *:vmap_l*
  85. :om[ap]  {lhs}                        *:omap_l*
  86. :map!    {lhs}                        *:map_l!*
  87. :im[ap]  {lhs}                        *:imap_l*
  88. :cm[ap]  {lhs}                        *:cmap_l*
  89.             List the key mappings for the key sequences starting
  90.             with {lhs} in the modes where the map command applies.
  91.             {not in Vi}
  92.  
  93. These commands are used to map a key or key sequence to a string of
  94. characters.  You can use this to put command sequences under function keys,
  95. translate one key into another, etc.  See |:mkexrc| for how to save and
  96. restore the current mappings.
  97.  
  98. There are five sets of mappings
  99. - For Normal mode: When typing commands.
  100. - For Visual mode: When typing commands while the Visual area is highlighted.
  101. - For Operator-pending mode: When an operator is pending (after "d", "y", "c",
  102.   etc.).
  103. - For Insert mode. These are also used in Replace mode.
  104. - For Command-line mode: When entering a ":" or "/" command.
  105.  
  106.                         *map-overview* *map-modes*
  107. Overview of which map command works in which mode:
  108.  
  109.     commands:                      modes:                  ~
  110.                       Normal     Visual  Operator-pending ~
  111. :map   :noremap   :unmap   :mapclear         X        X       X
  112. :nmap  :nnoremap  :nunmap  :nmapclear         X        .       .
  113. :vmap  :vnoremap  :vunmap  :vmapclear         .        X       .
  114. :omap  :onoremap  :ounmap  :omapclear         .        .       X
  115.  
  116.                       Insert Command-line        ~
  117. :map!  :noremap!  :unmap!  :mapclear!         X           X
  118. :imap  :inoremap  :iunmap  :imapclear         X           .
  119. :cmap  :cnoremap  :cunmap  :cmapclear         .           X
  120.  
  121. The original Vi did not have separate mappings for
  122. Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
  123. Therefore the ":map" and ":map!" commands enter and display mappings for
  124. several modes.  In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
  125. ":imap" commands to enter mappings for each mode separately.
  126.  
  127. To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
  128. first define it for all three modes, then unmap it for Operator-pending mode:
  129.     :map    xx something-difficult
  130.     :ounmap xx
  131. Likewise for a mapping for Visual and Operator-pending mode or Normal and
  132. Operator-pending mode.
  133.  
  134.                             *map-listing*
  135. When listing mappings the characters in the first two columns are:
  136.  
  137.     CHAR        MODE    ~
  138.     <Space>        Normal, Visual and Operator-pending
  139.      n        Normal
  140.      v        Visual
  141.      o        Operator-pending
  142.      !        Insert and Command-line
  143.      i        Insert
  144.      c        Command-line
  145.  
  146. A "*" just before the {rhs} indicates that it is not remappable.
  147.  
  148. Everything from the first non-blank after {lhs} up to the end of the line
  149. (or '|') is considered to be part of {rhs}.  This allows the {rhs} to end
  150. with a space.
  151.  
  152. Note: When using mappings for Visual mode, you can use the "'<" mark, which
  153. is the start of the last selected Visual area in the current buffer |'<|.
  154.  
  155.                             *map_backslash*
  156. Note that only CTRL-V is mentioned here as a special character for mappings
  157. and abbreviations.  When 'cpoptions' does not contain 'B', a backslash can
  158. also be used like CTRL-V.  The <> notation can be fully used then |<>|.  But
  159. you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
  160. follows.
  161.  
  162. To map a backslash, or use a backslash literally in the {rhs}, the special
  163. sequence "<Bslash>" can be used.  This avoids the need to double backslashes
  164. when using nested mappings.
  165.  
  166.                             *map_CTRL_C*
  167. It's not possible to use a CTRL-C in the {lhs}.  You just can't map CTRL-C.
  168. The reason is that CTRL-C must always be available to break a running command.
  169.  
  170.                             *map_space_in_lhs*
  171. To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
  172. each space).
  173.                             *map_space_in_rhs*
  174. If you want a {rhs} that starts with a space, use "<Space>".  To be fully Vi
  175. compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
  176. single CTRL-V (you have to type CTRL-V two times).
  177.                             *map_empty_rhs*
  178. You can create an empty {rhs} by typing nothing after a single CTRL-V (you
  179. have to type CTRL-V two times).  Unfortunately, you cannot do this in a vimrc
  180. file.
  181.                             *<Nop>*
  182. A easier way to get a mapping that doesn't produce anything, is to use "<Nop>"
  183. for the {rhs}.  This only works when the |<>| notation is enabled.  For
  184. example, to make sure that function key 8 does nothing at all:
  185. >    :map  <F8>  <Nop>
  186. >    :map! <F8>  <Nop>
  187.  
  188.                             *map-comments*
  189. It is not possible to put a comment after these commands, because the '"'
  190. character is considered to be part of the {lhs} or {rhs}.
  191.  
  192.                             *map_bar*
  193. Since the '|' character is used to separate a map command from the next
  194. command, you will have to do something special to include  a '|' in {rhs}.
  195. There are three methods:
  196.    use         works when               example    ~
  197.    <Bar>     '<' is not in 'cpoptions'       :map _l :!ls <Bar> more^M
  198.    \|         'b' is not in 'cpoptions'       :map _l :!ls \| more^M
  199.    ^V|         always, in Vim and Vi       :map _l :!ls ^V| more^M
  200.  
  201. (here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
  202. cannot use the <> notation "<C-V>" here).
  203.  
  204. All three work when you use the default setting for 'cpoptions'.
  205.  
  206. When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
  207. ending in a '\' and then another command.  This is Vi compatible, but
  208. unlogical when compared to other commands.
  209.  
  210.                             *map_return*
  211. When you have a mapping that contains an Ex command, you need to put a line
  212. terminator after it to have it executed.  The use of <CR> is recommended for
  213. this (see |<>|).  Example:
  214. >  :map  _ls  :!ls -l %<CR>:echo "the end"<CR>
  215.  
  216. To avoid mapping of the characters you type in insert or Command-line mode,
  217. type a CTRL-V first.  The mapping in Insert mode is disabled if the 'paste'
  218. option is on.
  219.  
  220. Note that when an error is enountered (that causes an error message) the rest
  221. of the mapping is not executed.  This is Vi-compatible.
  222.  
  223. Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
  224. and CTRL-X is not mapped.  This was done to be able to use all the named
  225. registers and marks, even when the command with the same name has been
  226. mapped.
  227.  
  228.                             *map-which-keys*
  229. If you are going to map something, you will need to choose which key(s) to use
  230. for the {lhs}.  You will have to avoid keys that are used for Vim commands,
  231. otherwise you would not be able to use those commands anymore.  Here are a few
  232. suggestions:
  233. - Function keys <F2>, <F3>, etc..  Also the shifted function keys.  Note that
  234.   <F1> is already used for the help command.
  235. - Meta-keys (with the ALT key pressed).
  236. - Use the "_" character and then any other character.  The "_" command does
  237.   exist in Vim (see |_|), but you probably never use it.
  238.  
  239.                             *map-examples*
  240. A few examples (given as you type them, for "<CR>" you type four characters;
  241. the '<' flag must not be present in 'cpoptions' for this to work).
  242.  
  243. >  :map <F3>  o#include
  244. >  :map <M-g> /foo<CR>cwbar<Esc>
  245. >  :map _x    d/END/e<CR>
  246. >  :map! qq   quadrillion questions
  247.  
  248.                             *map-typing*
  249. Vim will compare what you type with the start of a mapped sequence.  If there
  250. is an incomplete match, it will get more characters until there either is a
  251. complete match or until there is no match at all.  Example: If you map! "qq",
  252. the first 'q' will not appear on the screen until you type another
  253. character.  This is because Vim cannot know if the next character will be a
  254. 'q' or not.  If the 'timeout' option is on (which is the default) Vim will
  255. only wait for one second (or as long as specified with the 'timeoutlen'
  256. option).  After that it assumes that the 'q' is to be interpreted as such.  If
  257. type slowly, or your system is slow, reset the 'timeout' option.  Then you
  258. might want to set the 'ttimeout' option.
  259.  
  260.                         *recursive_mapping*
  261. If you include the {lhs} in the {rhs} you have a recursive mapping.  When
  262. {lhs} is typed, it will be replaced with {rhs}.  When the {lhs} which is
  263. included in {rhs} is encountered it will be replaced with {rhs}, and so on.
  264. This makes it possible to repeat a command an infinite number of times.  The
  265. only problem is that the only way to stop this is by causing an error.  The
  266. macros to solve a maze uses this, look there for an example.  There is one
  267. exception: If the {rhs} starts with {lhs}, the first character is not mapped
  268. again (this is Vi compatible).
  269. For example:
  270. >  :map ab abcd
  271. will execute the "a" command and insert "bcd" in the text.  The "ab" in the
  272. {rhs} will not be mapped again.
  273.  
  274. If you want to exchange the meaning of two keys you should use the :noremap
  275. command.  For example:
  276. >  :noremap k j
  277. >  :noremap j k
  278. This will exchange the cursor up and down commands.
  279.  
  280. With the normal :map command, when the 'remap' option is on, mapping takes
  281. place until the text is found not to be a part of a {lhs}.  For example, if
  282. you use:
  283. >  :map x y
  284. >  :map y x
  285. Vim will replace x with y, and then y with x, etc.  When this has happened
  286. 'maxmapdepth' times (default 1000), Vim will give the error message
  287. "recursive mapping".
  288.  
  289. See the file "index" for keys that are not used and thus can be mapped
  290. without losing any builtin function.  I suggest you use function keys,
  291. and meta-keys.  If you are prepared to lose a command that you hardly ever use
  292. you can make mappings that start with '_' or '-'.  You can also use
  293. ":help <key>^D" to find out if a key is used for some command.  (<key> is the
  294. specific key you want to find out about, ^D is CTRL-D).
  295.  
  296. If you include an undo command inside a mapped sequence, this will bring the
  297. text back in the state before executing the macro.  This is compatible with
  298. the original Vi, as long as there is only one undo command in the mapped
  299. sequence (having two undo commands in a mapped sequence did not make sense
  300. in the original Vi, you would get back the text before the first undo).
  301.  
  302. There are three ways to map a special key:
  303. 1. The Vi-compatible method: Map the key code.  Often this is a sequence that
  304.    starts with <Esc>.  To enter a mapping like this you type ":map " and then
  305.    you have to type CTRL-V before hitting the function key.  Note that when
  306.    the key code for the key is in the termcap (the t_ options), it will
  307.    automatically be translated into the internal code and become the second
  308.    way of mapping (unless the 'k' flag is included in 'cpoptions').
  309. 2. The second method is to use the internal code for the function key.  To
  310.    enter such a mapping type CTRL-K and then hit the function key, or use
  311.    the form "#1", "#2", .. "#9", "#0", "<Up>", "<S-Down>", "<S-F7>", etc.
  312.    (see table of keys |key-notation|, all keys from <Up> can be used).  The
  313.    first ten function keys can be defined in two ways: Just the number, like
  314.    "#2", and with "<F>", like "<F2>".  Both stand for function key 2.  "#0"
  315.    refers to function key 10, defined with option 't_f10', which may be
  316.    function key zero on some keyboards.  The <> form cannot be used when
  317.    'cpoptions' includes the '<' flag.
  318. 3. Use the termcap entry, with the form <t_xx>, where "xx" is the name of the
  319.    termcap entry.  Any string entry can be used.  For example:
  320. >    :map <t_F3> G
  321.    Maps function key 13 to "G".  This does not work if 'cpoptions' includes
  322.    the '<' flag.
  323.  
  324. The advantage of the second and third method is that the mapping will work on
  325. different terminals without modification (the function key will be
  326. translated into the same internal code or the actual key code, no matter what
  327. terminal you are using.  The termcap must be correct for this to work, and you
  328. must use the same mappings).
  329.  
  330. DETAIL: Vim first checks if a sequence from the keyboard is mapped.  If it
  331. isn't the terminal key codes are tried (see |terminal-options|).  If a
  332. terminal code is found it is replaced with the internal code.  Then the check
  333. for a mapping is done again (so you can map an internal code to something
  334. else).  What is written into the script file depends on what is recognized.
  335. If the terminal key code was recognized as a mapping the key code itself is
  336. written to the script file.  If it was recognized as a terminal code the
  337. internal code is written to the script file.
  338.  
  339. ==============================================================================
  340. 2. Abbreviations                    *abbreviations*
  341.  
  342. Abbreviations are used in insert mode, Replace mode and Command-line mode.
  343. If you enter a word that is an abbreviation, it is replaced with the word it
  344. stands for.  This can be used to save typing for often used long words.
  345.  
  346. There are three types of abbreviations:
  347.  
  348. full-id      The "full-id" type consists entirely of keyword characters (letters
  349.       and characters from 'iskeyword' option).  This is the most common
  350.       abbreviation.
  351.  
  352.       Examples: "foo", "g3", "-1"
  353.  
  354. end-id      The "end-id" type ends in a keyword character, but all the other
  355.       characters are not keyword characters.
  356.  
  357.       Examples: "#i", "..f", "$/7"
  358.  
  359. non-id      The "non-id" type ends in a non-keyword character, the other
  360.       characters may be of any type, excluding space and Tab.  {this type
  361.       is not supported by Vi}
  362.  
  363.       Examples: "def#", "4/7$"
  364.  
  365. Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"
  366.  
  367. An abbreviation is only recognized when you type a non-keyword character.
  368. This can also be the <Esc> that ends insert mode or the <CR> that ends a
  369. command.  The non-keyword character which ends the abbreviation is inserted
  370. after the expanded abbreviation.  An exception to this is the character <C-]>,
  371. which is used to expand an abbreviation without inserting any extra
  372. characters.
  373.  
  374. Example:
  375. >   :ab hh    hello
  376.         "hh<Space>" is expanded to "hello<Space>"
  377.         "hh<C-]>" is expanded to "hello"
  378.  
  379. The characters before the cursor must match the abbreviation.  Each type has
  380. an additional rule:
  381.  
  382. full-id      In front of the match is a non-keyword character, or this is where
  383.       the line or insertion starts.  Exception: When the abbreviation is
  384.       only one character, it is not recognized if there is a non-keyword
  385.       character in front of it, other than a space or a <Tab>.
  386.  
  387. end-id      In front of the match is a keyword character, or a space or a <Tab>,
  388.       or this is where the line or insertion starts.
  389.  
  390. non-id      In front of the match is a space, <Tab> or the start of the line or
  391.       the insertion.
  392.  
  393. Examples: (<CURSOR> is where you type a non-keyword character)
  394. >  :ab foo   four old otters
  395.         " foo<CURSOR>"      is expanded to " four old otters"
  396.         " foobar<CURSOR>" is not expanded
  397.         "barfoo<CURSOR>"  is not expanded
  398.  
  399. >  :ab #i #include
  400.         "#i<CURSOR>"      is expanded to "#include"
  401.         ">#i<CURSOR>"      is not expanded
  402.  
  403. >  :ab ;; <endofline>"
  404.         "test;;"      is not expanded
  405.         "test ;;"      is expanded to "test <endofline>"
  406.  
  407. To avoid the abbreviation in insert mode: Type part of the abbreviation, exit
  408. insert mode with <Esc>, re-enter insert mode with "a" and type the rest.  Or
  409. type CTRL-V before the character after the abbreviation.
  410. To avoid the abbreviation in Command-line mode: Type CTRL-V twice somewhere in
  411. the abbreviation to avoid it to be replaced.  A CTRL-V in front of a normal
  412. character is mostly ignored otherwise.
  413.  
  414. There are no default abbreviations.
  415.  
  416. Abbreviations are never recursive.  You can use ":ab f f-o-o" without any
  417. problem.  But abbreviations can be mapped.  {some versions of Vi support
  418. recursive abbreviations, for no apparent reason}
  419.  
  420. Abbreviations are disabled if the 'paste' option is on.
  421.  
  422.                         *:ab* *:abbreviate*
  423. :ab[breviate]        list all abbreviations.  The character in the first
  424.             column indicates the mode where the abbreviation is
  425.             used: 'i' for insert mode, 'c' for Command-line
  426.             mode, '!' for both.
  427.  
  428. :ab[breviate] <lhs>    list the abbreviations that start with <lhs>
  429.  
  430. :ab[breviate] <lhs> <rhs>
  431.             add abbreviation for <lhs> to <rhs>.  If <lhs> already
  432.             existed it is replaced with the new <rhs>.  <rhs> may
  433.             contain spaces.
  434.  
  435.                         *:una* *:unabbreviate*
  436. :una[bbreviate] <lhs>    remove abbreviation for <lhs> from the list
  437.  
  438.                         *:norea* *:noreabbrev*
  439. :norea[bbrev] [lhs] [rhs]
  440.             same as ":ab", but no remapping for this <rhs> {not
  441.             in Vi}
  442.  
  443.                         *:ca* *:cabbrev*
  444. :ca[bbrev] [lhs] [rhs]    same as ":ab", but for Command-line mode only.  {not
  445.             in Vi}
  446.  
  447.                         *:cuna* *:cunabbrev*
  448. :cuna[bbrev] <lhs>    same as ":una", but for Command-line mode only.  {not
  449.             in Vi}
  450.  
  451.                         *:cnorea* *:cnoreabbrev*
  452. :cnorea[bbrev] [lhs] [rhs]
  453.             same as ":ab", but for Command-line mode only and no
  454.             remapping for this <rhs> {not in Vi}
  455.  
  456.                         *:ia* *:iabbrev*
  457. :ia[bbrev] [lhs] [rhs]    same as ":ab", but for Insert mode only.  {not in Vi}
  458.  
  459.                         *:iuna* *:iunabbrev*
  460. :iuna[bbrev] <lhs>    same as ":una", but for insert mode only.  {not in
  461.             Vi}
  462.  
  463.                         *:inorea* *:inoreabbrev*
  464. :inorea[bbrev] [lhs] [rhs]
  465.             same as ":ab", but for Insert mode only and no
  466.             remapping for this <rhs> {not in Vi}
  467.  
  468.                             *:abc* *:abclear*
  469. :abc[lear]        Remove all abbreviations.  {not in Vi}
  470.  
  471.                             *:iabc* *:iabclear*
  472. :iabc[lear]        Remove all abbreviations for Insert mode.  {not in Vi}
  473.  
  474.                             *:cabc* *:cabclear*
  475. :cabc[lear]        Remove all abbreviations for Command-line mode.  {not
  476.             in Vi}
  477.  
  478.                             *using_CTRL-V*
  479. It is possible to use special characters in the rhs of an abbreviation.
  480. CTRL-V has to be used to avoid the special meaning of most non printable
  481. characters.  How many CTRL-Vs need to be typed depends on how you enter the
  482. abbreviation.  This also applies to mappings.  Let's use an example here.
  483.  
  484. Suppose you want to abbreviate "esc" to enter an <Esc> character.  When you
  485. type the ":ab" command in Vim, you have to enter this: (here ^V is a CTRL-V
  486. and ^[ is <Esc>)
  487.  
  488. You type:   ab esc ^V^V^V^V^V^[
  489.  
  490.     All keyboard input is subjected to ^V quote interpretation, so
  491.     the first, third, and fifth ^V  characters simply allow the second,
  492.     and fourth ^Vs, and the ^[, to be entered into the command-line.
  493.  
  494. You see:    ab esc ^V^V^[
  495.  
  496.     The command-line contains two actual ^Vs before the ^[.  This is
  497.     how it should appear in your .exrc file, if you choose to go that
  498.     route.  The first ^V is there to quote the second ^V; the :ab
  499.     command uses ^V as its own quote character, so you can include quoted
  500.     whitespace or the | character in the abbreviation.   The :ab command
  501.     doesn't do anything special with the ^[ character, so it doesn't need
  502.     to be quoted.  (Although quoting isn't harmful; that's why typing 7
  503.     [but not 8!] ^Vs works.)
  504.  
  505. Stored as:  esc     ^V^[
  506.  
  507.     After parsing, the abbreviation's short form ("esc") and long form
  508.     (the two characters "^V^[") are stored in the abbreviation table.
  509.     If you give the :ab command with no arguments, this is how the
  510.     abbreviation will be displayed.
  511.  
  512.     Later, when the abbreviation is expanded because the user typed in
  513.     the word "esc", the long form is subjected to the same type of
  514.     ^V interpretation as keyboard input.  So the ^V protects the ^[
  515.     character from being interpreted as the "exit input-mode" character.
  516.     Instead, the ^[ is inserted into the text.
  517.  
  518. Expands to: ^[
  519.  
  520. [example given by Steve Kirkendall]
  521.  
  522. ==============================================================================
  523. 3. User-defined commands                *user-commands*
  524.  
  525. It is possible to define your own ex commands. A user-defined command can act
  526. just like a built-in command (it can have a range or arguments, arguments can
  527. be completed as filenames or buffer names, etc), except that when the command
  528. is executed, it is transformed into a normal ex command and then executed.
  529.  
  530. All user defined commands must start with an uppercase letter, to avoid
  531. confusion with builtin commands. (There are a few builtin commands, notably
  532. :Next and :Print, which do start with an uppercase letter. The builtin will
  533. always take precedence in these cases).
  534.  
  535. When using a user-defined command, the command can be abbreviated. However, if
  536. an abbreviation is not unique, an error will be issued. Furthermore, a
  537. built-in command will always take precedence.
  538.  
  539. Example:
  540. >    :command Rename ...
  541. >    :command Renumber ...
  542. >    :Rena                " Means "Rename"
  543. >    :Renu                " Means "Renumber"
  544. >    :Ren                " Error - ambiguous
  545. >    :command Paste ...
  546. >    :P                " The built-in :Print
  547.  
  548. It is recommended that full names for user-defined commands are used in
  549. scripts.
  550.  
  551. :com[mand]                        *:com* *:command*
  552.             List all user-defined commands. When listing commands,
  553.             the characters in the first two columns are
  554.                 !    Command has the -bang attribute
  555.                 "    Command has the -register attribute
  556.             (see below for details on attributes)
  557.  
  558. :com[mand] {cmd}    List the user-defined commands that start with {cmd}
  559.  
  560. :com[mand][!] [{attr}...] {cmd} {rep}
  561.             Define a user command.  The name of the command is
  562.             {cmd} and its replacement text is {rep}. The command's
  563.             attributes (see below) are {attr}. If the command
  564.             already exists, an error is reported, unless a ! is
  565.             specified, when the command is redefined.
  566.  
  567. :delc[ommand] {cmd}                    *:delc* *:delcommand*
  568.             Delete the user-defined command {cmd}.
  569.  
  570. :comc[lear]                        *:comc* *:comclear*
  571.             Delete all user-defined commands.
  572.  
  573. Command attributes
  574.  
  575. User-defined commands are treated by Vim just like any other ex commands. They
  576. can have arguments, or have a range specified. Arguments are subject to
  577. completion as filenames, buffers, etc. Exactly how this works depends upon the
  578. command's attributes, which are specified when the command is defined.
  579.  
  580. There are a number of attributes, split into four categories: argument
  581. handling, completion behaviour, range handling, and special cases. The
  582. attributes are described below, by category.
  583.  
  584. Argument handling
  585.  
  586. By default, a user defined command will take no arguments (and an error is
  587. reported if any are supplied). However, it is possible to specify that the
  588. command can take arguments, using the -nargs attribute. Valid cases are:
  589.  
  590.     -nargs=0    No arguments are allowed (the default)
  591.     -nargs=1    Exactly one argument is required
  592.     -nargs=*    Any number of arguments are allowed (0, 1, or many)
  593.     -nargs=?    0 or 1 arguments are allowed
  594.     -nargs=+    Arguments must be supplied, but any number are allowed
  595.  
  596. Arguments are considered to be separated by (unescaped) spaces in this
  597. context.
  598.  
  599. Completion behaviour
  600.  
  601. By default, the arguments of user defined commands do not undergo completion.
  602. However, by specifying one or the other of the following attributes, argument
  603. completion can be enabled:
  604.  
  605.     -complete=augroup    autocmd groups
  606.     -complete=buffer    buffer names
  607.     -complete=command    Ex command (and arguments)
  608.     -complete=dir        directory names
  609.     -complete=event        autocommand events
  610.     -complete=file        file and directory names
  611.     -complete=help        help subjects
  612.     -complete=highlight    highlight groups
  613.     -complete=menu        menus
  614.     -complete=option    options
  615.     -complete=tag        tags
  616.     -complete=var        user variables
  617.  
  618. Range handling
  619.  
  620. By default, user-defined commands do not accept a line number range. However,
  621. it is possible to specify that the command does take a range (the -range
  622. attribute), or that it takes an arbitrary count value, either in the line
  623. number position (-range=N, like the |:split| command) or as a "count"
  624. argument (-count=N, like the |:Next| command). Possible attributes are:
  625.  
  626.     -range        Range allowed, default is current line
  627.     -range=%    Range allowed, default is whole file (1,$)
  628.     -range=N    A count (default N) which is specified in the line
  629.             number position (like |:split|)
  630.     -count=N    A count (default N) which is specified either in the line
  631.             number position, or as an initial argument (like |:Next|)
  632.             Specifying -count (without a default) acts like -count=0
  633.  
  634. Note that -range=N and -count=N are mutually exclusive - only one should be
  635. specified.
  636.  
  637. Special cases
  638.  
  639. There are some special cases as well:
  640.  
  641.     -bang        The command can take a ! modifier (like :q or :w)
  642.     -register   The first argument to the command can be an optional
  643.             register name (like :del, :put, :yank).
  644.  
  645. In the cases of the -count and -register attributes, if the optional argument
  646. is supplied, it is removed from the argument list and is available to the
  647. replacement text separately.
  648.  
  649. Replacement text
  650.  
  651. The replacement text for a user defined command is scanned for special escape
  652. sequences, using <...> notation. Escape sequences are replaced with values
  653. from the entered command line, and all other text is copied unchanged. The
  654. resulting string is executed as an Ex command. If the initial < of an escape
  655. sequence is preceded by a backslash, the sequence is copied unchanged.
  656.  
  657. The valid escape sequences are
  658.  
  659.     <line1>    The starting line of the command range.
  660.     <line2>    The final line of the command range.
  661.     <count>    Any count supplied (as described for the '-range'
  662.         and '-count' attributes).
  663.     <bang>    (See the '-bang' attribute) Expands to a ! if the
  664.         command was executed with a ! modifier, otherwise
  665.         expands to nothing.
  666.     <reg>    (See the '-register' attribute) The optional register,
  667.         if specified. Otherwise, expands to nothing. <register>
  668.         is a synonym for this.
  669.     <args>    The command arguments, exactly as supplied (but as
  670.         noted above, any count or register can consume some
  671.         of the arguments, which are then not part of <args>).
  672.     <lt>    A single '<' (Less-Then) character.  This is needed if you
  673.         want to get a literal copy of one of these escape sequences
  674.         into the expansion - for example, to get <bang>, use
  675.         <lt>bang>.
  676.  
  677. If the first two characters of an escape sequence are "q-" (for example,
  678. <q-args>) then the value is quoted in such a way as to make it a valid value
  679. for use in an expression.
  680.  
  681. To allow commands to pass their arguments on to a user-defined function, there
  682. is a special form <f-args> ("function args"). This splits the command
  683. arguments at spaces, quotes each argument individually, and the <f-args>
  684. sequence is replaced by the comma-separated list of quoted arguments.  See the
  685. Mycmd example below.
  686.  
  687. Examples
  688.  
  689. >   " Delete everything after here to the end
  690. >   :com Ddel +,$d
  691.  
  692. >   " Rename the current buffer
  693. >   :com -nargs=1 -bang -complete=file Ren f <args>|w<bang>
  694.  
  695. >   " Replace a range with the contents of a file
  696. >   " (Enter this all as one line)
  697. >   :com -range -nargs=1 -complete=file
  698. >     Replace <line1>-pu_|<line1>,<line2>d|r <args>|<line1>d
  699.  
  700. >   " Call a user function (example of <q-args>)
  701. >   :com -nargs=* Mycmd call Myfunc(<q-args>)
  702.  
  703. >   " A more substantial example
  704. >   :function Allbuf(command)
  705. >   :    let i = 1
  706. >   :    while i <= bufnr("$")
  707. >   :       if bufexists(i)
  708. >   :          execute 'buffer ' . i
  709. >   :          execute a:command
  710. >   :      endif
  711. >   :      let i = i + 1
  712. >   :   endwhile
  713. >   :endfunction
  714. >   :command -nargs=+ -complete=command Allbuf call Allbuf(<q-args>)
  715.  
  716. The command Allbuf takes any Vim command(s) as argument and executes it on all
  717. buffers in the buffer list.  Usage example (note use of the "e" flag to ignore
  718. errors and the "update" command to write modified buffers):
  719.  
  720. >   :Allbuf %s/foo/bar/ge|update
  721.  
  722.  vim:tw=78:ts=8:sw=8:
  723.