<q-mods>)
<
*<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-Than) 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>.
*<q-args>*
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.
When there is no argument <q-args> is an empty string. See the
|q-args-example| below.
*<f-args>*
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 and tabs, quotes each argument individually, and the
<f-args> sequence is replaced by the comma-separated list of quoted arguments.
See the Mycmd example below. If no arguments are given <f-args> is removed.
To embed whitespace into an argument of <f-args>, prepend a backslash.
<f-args> replaces every pair of backslashes (\\) with one backslash. A
backslash followed by a character other than white space or a backslash
remains unmodified. Also see |f-args-example| below. Overview:
command <f-args> ~
XX ab "ab"
XX a\b 'a\b'
XX a\ b 'a b'
XX a\ b 'a ', 'b'
XX a\\b 'a\b'
XX a\\ b 'a\', 'b'
XX a\\\b 'a\\b'
XX a\\\ b 'a\ b'
XX a\\\\b 'a\\b'
XX a\\\\ b 'a\\', 'b'
XX [nothing]
Note that if the "no arguments" situation is to be handled, you have to make
sure that the function can be called without arguments.
Examples for user commands: >
" 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"
< *f-args-example*
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")
< *q-args-example*
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")
<
vim:tw=78:ts=8:noet:ft=help:norl: