Vim documentation: map

main help file

*map.txt*       For Vim version 5.8.  Last change: 2001 Jan 30


		  VIM REFERENCE MANUAL    by Bram Moolenaar


Key mapping and abbreviations.

1. Key mapping			|key-mapping|
2. Abbreviations		|abbreviations|
3. User-defined commands	|user-commands|

==============================================================================

1. Key mapping				*key-mapping* *mapping* *macro*

There are commands to enter new mappings, remove mappings and list mappings.
See |map-overview| for the various forms of "map" and their relationships with
modes.


:map     {lhs} {rhs}					*:map*

:nm[ap]  {lhs} {rhs}					*:nm* *:nmap*

:vm[ap]  {lhs} {rhs}					*:vm* *:vmap*

:om[ap]  {lhs} {rhs}					*:om* *:omap*

:map!    {lhs} {rhs}					*:map!*

:im[ap]  {lhs} {rhs}					*:im* *:imap*

:cm[ap]  {lhs} {rhs}					*:cm* *:cmap*
			Map the key sequence {lhs} to {rhs} for the modes
			where the map command applies.  The result, including
			{rhs}, is then further scanned for mappings.  This
			allows for nested and recursive use of mappings.



:no[remap]   {lhs} {rhs}				*:no*  *:noremap*

:nn[oremap]  {lhs} {rhs}				*:nn*  *:nnoremap*

:vn[oremap]  {lhs} {rhs}				*:vn*  *:vnoremap*

:ono[remap]  {lhs} {rhs}				*:ono* *:onoremap*

:no[remap]!  {lhs} {rhs}				*:no!* *:noremap!*

:ino[remap]  {lhs} {rhs}				*:ino* *:inoremap*

:cno[remap]  {lhs} {rhs}				*:cno* *:cnoremap*
			Map the key sequence {lhs} to {rhs} for the modes
			where the map command applies.  Disallow mapping of
			{rhs}, to avoid nested and recursive mappings.  Often
			used to redefine a command.  {not in Vi}



:unm[ap]   {lhs}					*:unm*  *:unmap*

:nun[map]  {lhs}					*:nun*  *:nunmap*

:vu[nmap]  {lhs}					*:vu*   *:vunmap*

:ou[nmap]  {lhs}					*:ou*   *:ounmap*

:unm[ap]!  {lhs}					*:unm!* *:unmap!*

:iu[nmap]  {lhs}					*:iu*   *:iunmap*

:cu[nmap]  {lhs}					*:cu*   *:cunmap*
			Remove the mapping of {lhs} for the modes where the
			map command applies.  The mapping may remain defined
			for other modes where it applies.
			Note: Trailing spaces are included in the {lhs}.  This
			unmap does NOT work:
				:map @@ foo
				:unmap @@ | print


:mapc[lear]						*:mapc*   *:mapclear*

:nmapc[lear]						*:nmapc*  *:nmapclear*

:vmapc[lear]						*:vmapc*  *:vmapclear*

:omapc[lear]						*:omapc*  *:omapclear*

:mapc[lear]!						*:mapc!*  *:mapclear!*

:imapc[lear]						*:imapc*  *:imapclear*

:cmapc[lear]						*:cmapc*  *:cmapclear*
			Remove ALL mappings for the modes where the map
			command applies.  {not in Vi}
			Warning: This also removes the default mappings.

:map
:nm[ap]
:vm[ap]
:om[ap]
:map!
:im[ap]
:cm[ap]
			List all key mappings for the modes where the map
			command applies.  Note that ":map" and ":map!" are
			used most often, because they include the other modes.


:map     {lhs}						*:map_l*

:nm[ap]  {lhs}						*:nmap_l*

:vm[ap]  {lhs}						*:vmap_l*

:om[ap]  {lhs}						*:omap_l*

:map!    {lhs}						*:map_l!*

:im[ap]  {lhs}						*:imap_l*

:cm[ap]  {lhs}						*:cmap_l*
			List the key mappings for the key sequences starting
			with {lhs} in the modes where the map command applies.
			{not in Vi}

These commands are used to map a key or key sequence to a string of
characters.  You can use this to put command sequences under function keys,
translate one key into another, etc.  See |:mkexrc| for how to save and
restore the current mappings.

There are five sets of mappings
- For Normal mode: When typing commands.
- For Visual mode: When typing commands while the Visual area is highlighted.
- For Operator-pending mode: When an operator is pending (after "d", "y", "c",
  etc.).
- For Insert mode. These are also used in Replace mode.
- For Command-line mode: When entering a ":" or "/" command.

There are no separate mappings for Select mode.  The same as for Visual mode
are used |Select-mode-mapping|.


						*map-overview* *map-modes*
Overview of which map command works in which mode:

    commands:				      modes:			      
					  Normal     Visual  Operator-pending 
:map   :noremap   :unmap   :mapclear	     X		X	   X
:nmap  :nnoremap  :nunmap  :nmapclear	     X		.	   .
:vmap  :vnoremap  :vunmap  :vmapclear	     .		X	   .
:omap  :onoremap  :ounmap  :omapclear	     .		.	   X

					  Insert Command-line		
:map!  :noremap!  :unmap!  :mapclear!	     X	       X
:imap  :inoremap  :iunmap  :imapclear	     X	       .
:cmap  :cnoremap  :cunmap  :cmapclear	     .	       X

The original Vi did not have separate mappings for
Normal/Visual/Operator-pending mode and for Insert/Command-line mode.
Therefore the ":map" and ":map!" commands enter and display mappings for
several modes.  In Vim you can use the ":nmap", ":vmap", ":omap", ":cmap" and
":imap" commands to enter mappings for each mode separately.

To enter a mapping for Normal and Visual mode, but not Operator-pending mode,
first define it for all three modes, then unmap it for Operator-pending mode:
	:map    xx something-difficult
	:ounmap xx
Likewise for a mapping for Visual and Operator-pending mode or Normal and
Operator-pending mode.


							*map-listing*
When listing mappings the characters in the first two columns are:

	CHAR		MODE	
	<Space>		Normal, Visual and Operator-pending
	 n		Normal
	 v		Visual
	 o		Operator-pending
	 !		Insert and Command-line
	 i		Insert
	 c		Command-line

A "*" just before the {rhs} indicates that it is not remappable.

Everything from the first non-blank after {lhs} up to the end of the line
(or '|') is considered to be part of {rhs}.  This allows the {rhs} to end
with a space.

Note: When using mappings for Visual mode, you can use the "'<" mark, which
is the start of the last selected Visual area in the current buffer |'<|.


							*map_backslash*
Note that only CTRL-V is mentioned here as a special character for mappings
and abbreviations.  When 'cpoptions' does not contain 'B', a backslash can
also be used like CTRL-V.  The <> notation can be fully used then |<>|.  But
you cannot use "<C-V>" like CTRL-V to escape the special meaning of what
follows.

To map a backslash, or use a backslash literally in the {rhs}, the special
sequence "<Bslash>" can be used.  This avoids the need to double backslashes
when using nested mappings.


							*map_CTRL_C*
It's not possible to use a CTRL-C in the {lhs}.  You just can't map CTRL-C.
The reason is that CTRL-C must always be available to break a running command.


							*map_space_in_lhs*
To include a space in {lhs} precede it with a CTRL-V (type two CTRL-Vs for
each space).

							*map_space_in_rhs*
If you want a {rhs} that starts with a space, use "<Space>".  To be fully Vi
compatible (but unreadable) don't use the |<>| notation, precede {rhs} with a
single CTRL-V (you have to type CTRL-V two times).

							*map_empty_rhs*
You can create an empty {rhs} by typing nothing after a single CTRL-V (you
have to type CTRL-V two times).  Unfortunately, you cannot do this in a vimrc
file.

							*<Nop>*
A easier way to get a mapping that doesn't produce anything, is to use "<Nop>"
for the {rhs}.  This only works when the |<>| notation is enabled.  For
example, to make sure that function key 8 does nothing at all:
	:map  <F8>  <Nop>
	:map! <F8>  <Nop>


							*map-comments*
It is not possible to put a comment after these commands, because the '"''
character is considered to be part of the {lhs} or {rhs}.


							*map_bar*
Since the '|' character is used to separate a map command from the next
command, you will have to do something special to include  a '|' in {rhs}.
There are three methods:
   use	     works when			   example	
   <Bar>     '<' is not in 'cpoptions'	   :map _l :!ls <Bar> more^M
   \|	     'b' is not in 'cpoptions'	   :map _l :!ls \| more^M
   ^V|	     always, in Vim and Vi	   :map _l :!ls ^V| more^M

(here ^V stands for CTRL-V; to get one CTRL-V you have to type it twice; you
cannot use the <> notation "<C-V>" here).

All three work when you use the default setting for 'cpoptions'.

When 'b' is present in 'cpoptions', "\|" will be recognized as a mapping
ending in a '\' and then another command.  This is Vi compatible, but
illogical when compared to other commands.


							*map_return*
When you have a mapping that contains an Ex command, you need to put a line
terminator after it to have it executed.  The use of <CR> is recommended for
this (see |<>|).  Example:
  :map  _ls  :!ls -l %<CR>:echo "the end"<CR>

To avoid mapping of the characters you type in insert or Command-line mode,
type a CTRL-V first.  The mapping in Insert mode is disabled if the 'paste'
option is on.

Note that when an error is encountered (that causes an error message) the rest
of the mapping is not executed.  This is Vi-compatible.

Note that the second character (argument) of the commands @zZtTfF[]rm'`"v
and CTRL-X is not mapped.  This was done to be able to use all the named
registers and marks, even when the command with the same name has been
mapped.


							*map-which-keys*
If you are going to map something, you will need to choose which key(s) to use
for the {lhs}.  You will have to avoid keys that are used for Vim commands,
otherwise you would not be able to use those commands anymore.  Here are a few
suggestions:
- Function keys <F2>, <F3>, etc..  Also the shifted function keys <S-F1>,
  <S-F2>, etc.  Note that <F1> is already used for the help command.
- Meta-keys (with the ALT key pressed).
- Use the '_' or ',' character and then any other character.  The "_" and ","
  commands do exist in Vim (see |_| and |,|), but you probably never use them.
- Use a key that is a synonym for another command.  For example: CTRL-P and
  CTRL-N.  Use an extra character to allow more mappings.

See the file "index" for keys that are not used and thus can be mapped without
losing any builtin function.  You can also use ":help {key}^D" to find out if
a key is used for some command.  ({key} is the specific key you want to find
out about, ^D is CTRL-D).


							*map-examples*
A few examples (given as you type them, for "<CR>" you type four characters;
the '<' flag must not be present in 'cpoptions' for this to work).

  :map <F3>  o#include
  :map <M-g> /foo<CR>cwbar<Esc>
  :map _x    d/END/e<CR>
  :map! qq   quadrillion questions


							*map-typing*
Vim will compare what you type with the start of a mapped sequence.  If there
is an incomplete match, it will get more characters until there either is a
complete match or until there is no match at all.  Example: If you map! "qq",
the first 'q' will not appear on the screen until you type another
character.  This is because Vim cannot know if the next character will be a
'q' or not.  If the 'timeout' option is on (which is the default) Vim will
only wait for one second (or as long as specified with the 'timeoutlen'
option).  After that it assumes that the 'q' is to be interpreted as such.  If
type slowly, or your system is slow, reset the 'timeout' option.  Then you
might want to set the 'ttimeout' option.


							*map-keys-fails*
There is one situation where key codes might not be recognized:
- Vim can only read part of the key code.  Mostly this is only the first
  character.  This happens on some Unix versions in an xterm.
- The key code is after character(s) that are mapped.  E.g., "<F1><F1>" or
  "g<F1>".
The result is that the key code is not recognized in this situation, and the
mapping fails.
There are two actions needed to avoid this problem:
- Remove the 'K' flag from 'cpoptions'.  This will make Vim wait for the rest
  of the characters of the function key.
- When using <F1> to <F4> the actual key code generated may correspond to
  <xF1> to <xF4>.  There are mappings from <xF1> to <F1>, <xF2> to <F2>, etc.,
  but these are not recognized after another half a mapping.  Make sure the
  key codes for <F1> to <F4> are correct:
	:set <F1>=<type CTRL-V><type F1>
  Type the <F1> as four characters.  The part after the "=" must be done with
  the actual keys, not the literal text.
Another solution is to use the actual key code in the mapping for the second
special key:
	:map <F1><Esc>OP :echo "yes"<CR>
Don't type a real <Esc>, Vim will recognize the key code and replace it with
<F1> anyway.


						*recursive_mapping*
If you include the {lhs} in the {rhs} you have a recursive mapping.  When
{lhs} is typed, it will be replaced with {rhs}.  When the {lhs} which is
included in {rhs} is encountered it will be replaced with {rhs}, and so on.
This makes it possible to repeat a command an infinite number of times.  The
only problem is that the only way to stop this is by causing an error.  The
macros to solve a maze uses this, look there for an example.  There is one
exception: If the {rhs} starts with {lhs}, the first character is not mapped
again (this is Vi compatible).
For example:
  :map ab abcd
will execute the "a" command and insert "bcd" in the text.  The "ab" in the
{rhs} will not be mapped again.

If you want to exchange the meaning of two keys you should use the :noremap
command.  For example:
  :noremap k j
  :noremap j k
This will exchange the cursor up and down commands.

With the normal :map command, when the 'remap' option is on, mapping takes
place until the text is found not to be a part of a {lhs}.  For example, if
you use:
  :map x y
  :map y x
Vim will replace x with y, and then y with x, etc.  When this has happened
'maxmapdepth' times (default 1000), Vim will give the error message
"recursive mapping".


							*:map-undo*
If you include an undo command inside a mapped sequence, this will bring the
text back in the state before executing the macro.  This is compatible with
the original Vi, as long as there is only one undo command in the mapped
sequence (having two undo commands in a mapped sequence did not make sense
in the original Vi, you would get back the text before the first undo).


							*:map-special-keys*
There are three ways to map a special key:
1. The Vi-compatible method: Map the key code.  Often this is a sequence that
   starts with <Esc>.  To enter a mapping like this you type ":map " and then
   you have to type CTRL-V before hitting the function key.  Note that when
   the key code for the key is in the termcap (the t_ options), it will
   automatically be translated into the internal code and become the second
   way of mapping (unless the 'k' flag is included in 'cpoptions').
2. The second method is to use the internal code for the function key.  To
   enter such a mapping type CTRL-K and then hit the function key, or use
   the form "#1", "#2", .. "#9", "#0", "<Up>", "<S-Down>", "<S-F7>", etc.
   (see table of keys |key-notation|, all keys from <Up> can be used).  The
   first ten function keys can be defined in two ways: Just the number, like
   "#2", and with "<F>", like "<F2>".  Both stand for function key 2.  "#0"
   refers to function key 10, defined with option 't_f10', which may be
   function key zero on some keyboards.  The <> form cannot be used when
   'cpoptions' includes the '<' flag.
3. Use the termcap entry, with the form <t_xx>, where "xx" is the name of the
   termcap entry.  Any string entry can be used.  For example:
    :map <t_F3> G
   Maps function key 13 to "G".  This does not work if 'cpoptions' includes
   the '<' flag.

The advantage of the second and third method is that the mapping will work on
different terminals without modification (the function key will be
translated into the same internal code or the actual key code, no matter what
terminal you are using.  The termcap must be correct for this to work, and you
must use the same mappings).

DETAIL: Vim first checks if a sequence from the keyboard is mapped.  If it
isn't the terminal key codes are tried (see |terminal-options|).  If a
terminal code is found it is replaced with the internal code.  Then the check
for a mapping is done again (so you can map an internal code to something
else).  What is written into the script file depends on what is recognized.
If the terminal key code was recognized as a mapping the key code itself is
written to the script file.  If it was recognized as a terminal code the
internal code is written to the script file.

==============================================================================

2. Abbreviations			*abbreviations* *Abbreviations*

Abbreviations are used in Insert mode, Replace mode and Command-line mode.
If you enter a word that is an abbreviation, it is replaced with the word it
stands for.  This can be used to save typing for often used long words.  And
you can use it to automatically correct obvious spelling errors.
Examples:

	:iab ms MicroSoft
	:iab tihs this

There are three types of abbreviations:

full-id	  The "full-id" type consists entirely of keyword characters (letters
	  and characters from 'iskeyword' option).  This is the most common
	  abbreviation.

	  Examples: "foo", "g3", "-1"

end-id	  The "end-id" type ends in a keyword character, but all the other
	  characters are not keyword characters.

	  Examples: "#i", "..f", "$/7"

non-id	  The "non-id" type ends in a non-keyword character, the other
	  characters may be of any type, excluding space and Tab.  {this type
	  is not supported by Vi}

	  Examples: "def#", "4/7$"

Examples of strings that cannot be abbreviations: "a.b", "#def", "a b", "_$r"

An abbreviation is only recognized when you type a non-keyword character.
This can also be the <Esc> that ends insert mode or the <CR> that ends a
command.  The non-keyword character which ends the abbreviation is inserted
after the expanded abbreviation.  An exception to this is the character <C-]>,
which is used to expand an abbreviation without inserting any extra
characters.

Example:
   :ab hh	hello
	    "hh<Space>" is expanded to "hello<Space>"
	    "hh<C-]>" is expanded to "hello"

The characters before the cursor must match the abbreviation.  Each type has
an additional rule:

full-id	  In front of the match is a non-keyword character, or this is where
	  the line or insertion starts.  Exception: When the abbreviation is
	  only one character, it is not recognized if there is a non-keyword
	  character in front of it, other than a space or a <Tab>.

end-id	  In front of the match is a keyword character, or a space or a <Tab>,
	  or this is where the line or insertion starts.

non-id	  In front of the match is a space, <Tab> or the start of the line or
	  the insertion.

Examples: ({CURSOR} is where you type a non-keyword character)
  :ab foo   four old otters
		" foo{CURSOR}"	  is expanded to " four old otters"
		" foobar{CURSOR}" is not expanded
		"barfoo{CURSOR}"  is not expanded

  :ab #i #include
		"#i{CURSOR}"	  is expanded to "#include"
		">#i{CURSOR}"	  is not expanded

  :ab ;; <endofline>"
		"test;;"	  is not expanded
		"test ;;"	  is expanded to "test <endofline>"

To avoid the abbreviation in insert mode: Type part of the abbreviation, exit
insert mode with <Esc>, re-enter insert mode with "a" and type the rest.  Or
type CTRL-V before the character after the abbreviation.
To avoid the abbreviation in Command-line mode: Type CTRL-V twice somewhere in
the abbreviation to avoid it to be replaced.  A CTRL-V in front of a normal
character is mostly ignored otherwise.

It is possible to move the cursor after an abbreviation:
  :iab if if ()<Left>
This does not work if 'cpoptions' includes the '<' flag. |<>|

There are no default abbreviations.

Abbreviations are never recursive.  You can use ":ab f f-o-o" without any
problem.  But abbreviations can be mapped.  {some versions of Vi support
recursive abbreviations, for no apparent reason}

Abbreviations are disabled if the 'paste' option is on.


						*:ab* *:abbreviate*
:ab[breviate]		list all abbreviations.  The character in the first
			column indicates the mode where the abbreviation is
			used: 'i' for insert mode, 'c' for Command-line
			mode, '!' for both.

:ab[breviate] {lhs}	list the abbreviations that start with {lhs}

:ab[breviate] {lhs} {rhs}
			add abbreviation for {lhs} to {rhs}.  If {lhs} already
			existed it is replaced with the new {rhs}.  {rhs} may
			contain spaces.


						*:una* *:unabbreviate*
:una[bbreviate] {lhs}	remove abbreviation for {lhs} from the list


						*:norea* *:noreabbrev*
:norea[bbrev] [lhs] [rhs]
			same as ":ab", but no remapping for this {rhs} {not
			in Vi}


						*:ca* *:cabbrev*
:ca[bbrev] [lhs] [rhs]	same as ":ab", but for Command-line mode only.  {not
			in Vi}


						*:cuna* *:cunabbrev*
:cuna[bbrev] {lhs}	same as ":una", but for Command-line mode only.  {not
			in Vi}


						*:cnorea* *:cnoreabbrev*
:cnorea[bbrev] [lhs] [rhs]
			same as ":ab", but for Command-line mode only and no
			remapping for this {rhs} {not in Vi}


						*:ia* *:iabbrev*
:ia[bbrev] [lhs] [rhs]	same as ":ab", but for Insert mode only.  {not in Vi}


						*:iuna* *:iunabbrev*
:iuna[bbrev] {lhs}	same as ":una", but for insert mode only.  {not in
			Vi}


						*:inorea* *:inoreabbrev*
:inorea[bbrev] [lhs] [rhs]
			same as ":ab", but for Insert mode only and no
			remapping for this {rhs} {not in Vi}


							*:abc* *:abclear*
:abc[lear]		Remove all abbreviations.  {not in Vi}


							*:iabc* *:iabclear*
:iabc[lear]		Remove all abbreviations for Insert mode.  {not in Vi}


							*:cabc* *:cabclear*
:cabc[lear]		Remove all abbreviations for Command-line mode.  {not
			in Vi}


							*using_CTRL-V*
It is possible to use special characters in the rhs of an abbreviation.
CTRL-V has to be used to avoid the special meaning of most non printable
characters.  How many CTRL-Vs need to be typed depends on how you enter the
abbreviation.  This also applies to mappings.  Let's use an example here.

Suppose you want to abbreviate "esc" to enter an <Esc> character.  When you
type the ":ab" command in Vim, you have to enter this: (here ^V is a CTRL-V
and ^[ is <Esc>)

You type:   ab esc ^V^V^V^V^V^[

	All keyboard input is subjected to ^V quote interpretation, so
	the first, third, and fifth ^V  characters simply allow the second,
	and fourth ^Vs, and the ^[, to be entered into the command-line.

You see:    ab esc ^V^V^[

	The command-line contains two actual ^Vs before the ^[.  This is
	how it should appear in your .exrc file, if you choose to go that
	route.  The first ^V is there to quote the second ^V; the :ab
	command uses ^V as its own quote character, so you can include quoted
	whitespace or the | character in the abbreviation.   The :ab command
	doesn't do anything special with the ^[ character, so it doesn't need
	to be quoted.  (Although quoting isn't harmful; that's why typing 7
	[but not 8!] ^Vs works.)

Stored as:  esc     ^V^[

	After parsing, the abbreviation's short form ("esc") and long form
	(the two characters "^V^[") are stored in the abbreviation table.
	If you give the :ab command with no arguments, this is how the
	abbreviation will be displayed.

	Later, when the abbreviation is expanded because the user typed in
	the word "esc", the long form is subjected to the same type of
	^V interpretation as keyboard input.  So the ^V protects the ^[
	character from being interpreted as the "exit input-mode" character.
	Instead, the ^[ is inserted into the text.

Expands to: ^[

[example given by Steve Kirkendall]

==============================================================================

3. User-defined commands				*user-commands*

It is possible to define your own ex commands. A user-defined command can act
just like a built-in command (it can have a range or arguments, arguments can
be completed as filenames or buffer names, etc), except that when the command
is executed, it is transformed into a normal ex command and then executed.

All user defined commands must start with an uppercase letter, to avoid
confusion with builtin commands. (There are a few builtin commands, notably
:Next, :Print and :X, which do start with an uppercase letter.  The builtin
will always take precedence in these cases).  The other characters of the user
command can be uppercase letters, lowercase letters or digits.  When using
digits, note that other commands that take a numeric argument may become
ambiguous.  For example, the command ":Cc2" could be the user command ":Cc2"
without an argument, or the command ":Cc" with argument "2".  It is advised to
put a space between the command name and the argument to avoid these problems.

When using a user-defined command, the command can be abbreviated. However, if
an abbreviation is not unique, an error will be issued. Furthermore, a
built-in command will always take precedence.

Example:
	:command Rename ...
	:command Renumber ...
	:Rena				" Means "Rename"
	:Renu				" Means "Renumber"
	:Ren				" Error - ambiguous
	:command Paste ...
	:P				" The built-in :Print

It is recommended that full names for user-defined commands are used in
scripts.


:com[mand]						*:com* *:command*
			List all user-defined commands. When listing commands,
			the characters in the first two columns are
			    !	Command has the -bang attribute
			    "	Command has the -register attribute
			(see below for details on attributes)

:com[mand] {cmd}	List the user-defined commands that start with {cmd}

:com[mand][!] [{attr}...] {cmd} {rep}
			Define a user command.  The name of the command is
			{cmd} and its replacement text is {rep}. The command's
			attributes (see below) are {attr}. If the command
			already exists, an error is reported, unless a ! is
			specified, in which case the command is redefined.


:delc[ommand] {cmd}					*:delc* *:delcommand*
			Delete the user-defined command {cmd}.


:comc[lear]						*:comc* *:comclear*
			Delete all user-defined commands.

Command attributes

User-defined commands are treated by Vim just like any other ex commands. They
can have arguments, or have a range specified. Arguments are subject to
completion as filenames, buffers, etc. Exactly how this works depends upon the
command's attributes, which are specified when the command is defined.

There are a number of attributes, split into four categories: argument
handling, completion behavior, range handling, and special cases. The
attributes are described below, by category.

Argument handling

By default, a user defined command will take no arguments (and an error is
reported if any are supplied). However, it is possible to specify that the
command can take arguments, using the -nargs attribute. Valid cases are:

	-nargs=0    No arguments are allowed (the default)
	-nargs=1    Exactly one argument is required
	-nargs=*    Any number of arguments are allowed (0, 1, or many)
	-nargs=?    0 or 1 arguments are allowed
	-nargs=+    Arguments must be supplied, but any number are allowed

Arguments are considered to be separated by (unescaped) spaces in this
context.

Completion behavior

By default, the arguments of user defined commands do not undergo completion.
However, by specifying one or the other of the following attributes, argument
completion can be enabled:

	-complete=augroup	autocmd groups
	-complete=buffer	buffer names
	-complete=command	Ex command (and arguments)
	-complete=dir		directory names
	-complete=event		autocommand events
	-complete=file		file and directory names
	-complete=help		help subjects
	-complete=highlight	highlight groups
	-complete=menu		menus
	-complete=option	options
	-complete=tag		tags
	-complete=tag_listfiles	tags, file names are shown when CTRL-D is hit
	-complete=var		user variables

Range handling

By default, user-defined commands do not accept a line number range. However,
it is possible to specify that the command does take a range (the -range
attribute), or that it takes an arbitrary count value, either in the line
number position (-range=N, like the |:split| command) or as a "count"
argument (-count=N, like the |:Next| command). Possible attributes are:

	-range	    Range allowed, default is current line
	-range=%    Range allowed, default is whole file (1,$)
	-range=N    A count (default N) which is specified in the line
		    number position (like |:split|)
	-count=N    A count (default N) which is specified either in the line
		    number position, or as an initial argument (like |:Next|)
		    Specifying -count (without a default) acts like -count=0

Note that -range=N and -count=N are mutually exclusive - only one should be
specified.

Special cases

There are some special cases as well:

	-bang	    The command can take a ! modifier (like :q or :w)
	-register   The first argument to the command can be an optional
		    register name (like :del, :put, :yank).

In the cases of the -count and -register attributes, if the optional argument
is supplied, it is removed from the argument list and is available to the
replacement text separately.

Replacement text

The replacement text for a user defined command is scanned for special escape
sequences, using <...> notation. Escape sequences are replaced with values
from the entered command line, and all other text is copied unchanged. The
resulting string is executed as an Ex command. If the initial < of an escape
sequence is preceded by a backslash, the sequence is copied unchanged.

The valid escape sequences are


						*<line1>*
	<line1>	The starting line of the command range.

						*<line2>*
	<line2>	The final line of the command range.

						*<count>*
	<count>	Any count supplied (as described for the '-range'
		and '-count' attributes).

						*<bang>*
	<bang>	(See the '-bang' attribute) Expands to a ! if the
		command was executed with a ! modifier, otherwise
		expands to nothing.

						*<reg>* *<register>*
	<reg>	(See the '-register' attribute) The optional register,
		if specified. Otherwise, expands to nothing. <register>
		is a synonym for this.

						*<args>*
	<args>	The command arguments, exactly as supplied (but as
		noted above, any count or register can consume some
		of the arguments, which are then not part of <args>).
	<lt>	A single '<' (Less-Then) character.  This is needed if you
		want to get a literal copy of one of these escape sequences
		into the expansion - for example, to get <bang>, use
		<lt>bang>.

If the first two characters of an escape sequence are "q-" (for example,
<q-args>) then the value is quoted in such a way as to make it a valid value
for use in an expression.  This uses the argument as one single value.

To allow commands to pass their arguments on to a user-defined function, there
is a special form <f-args> ("function args"). This splits the command
arguments at spaces, quotes each argument individually, and the <f-args>
sequence is replaced by the comma-separated list of quoted arguments.  See the
Mycmd example below.  When there is no argument, <f-args> also has no
argument.

Examples

   " Delete everything after here to the end
   :com Ddel +,$d

   " Rename the current buffer
   :com -nargs=1 -bang -complete=file Ren f <args>|w<bang>

   " Replace a range with the contents of a file
   " (Enter this all as one line)
   :com -range -nargs=1 -complete=file
	 Replace <line1>-pu_|<line1>,<line2>d|r <args>|<line1>d

   " Count the number of lines in the range
   com! -range -nargs=0 Lines :echo <line2> - <line1> + 1 "lines"

   " Call a user function (example of <f-args>)
   :com -nargs=* Mycmd call Myfunc(<f-args>)

When executed as:
	:Mycmd arg1 arg2
This will invoke:
	:call Myfunc("arg1","arg2")

   " A more substantial example
   :function Allargs(command)
   :	let i = 0
   :	while i < argc()
   :	   if filereadable(argv(i))
   :        execute "e " . argv(i)
   :	     execute a:command
   :      endif
   :      let i = i + 1
   :   endwhile
   :endfunction
   :command -nargs=+ -complete=command Allargs call Allargs(<q-args>)

The command Allargs takes any Vim command(s) as argument and executes it on all
files in the argument list.  Usage example (note use of the "e" flag to ignore
errors and the "update" command to write modified buffers):
	:Allargs %s/foo/bar/ge|update
This will invoke:
	:call Allargs("%s/foo/bar/ge|update")

top - main help file