home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / runtime / doc / map.txt < prev    next >
Encoding:
Text File  |  2003-06-01  |  43.5 KB  |  1,070 lines

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