"Hello World". However, if you add a double quote, it won't work.
For example: >
:Say he said "hello"
To get special characters turned into a string, properly escaped to use as an
expression, use "<q-args>": >
:command -nargs=+ Say :echo <q-args>
Now the above ":Say" command will result in this to be executed: >
:echo "he said \"hello\""
The <f-args> keyword contains the same information as the <args> keyword,
except in a format suitable for use as function call arguments. For example:
>
:command -nargs=* DoIt :call AFunction(<f-args>)
:DoIt a b c
Executes the following command: >
:call AFunction("a", "b", "c")
LINE RANGE
Some commands take a range as their argument. To tell Vim that you are
defining such a command, you need to specify a -range option. The values for
this option are as follows:
-range Range is allowed; default is the current line.
-range=% Range is allowed; default is the whole file.
-range={count} Range is allowed; the last number in it is used as a
single number whose default is {count}.
When a range is specified, the keywords <line1> and <line2> get the values of
the first and last line in the range. For example, the following command
defines the SaveIt command, which writes out the specified range to the file
"save_file": >
:command -range=% SaveIt :<line1>,<line2>write! save_file
OTHER OPTIONS
Some of the other options and keywords are as follows:
-count={number} The command can take a count whose default is
{number}. The resulting count can be used
through the <count> keyword.
-bang You can use a !. If present, using <bang> will
result in a !.
-register You can specify a register. (The default is
the unnamed register.)
The register specification is available as
<reg> (a.k.a. <register>).
-complete={type} Type of command-line completion used. See
|:command-completion| for the list of possible
values.
-bar The command can be followed by | and another
command, or " and a comment.
-buffer The command is only available for the current
buffer.
Finally, you have the <lt> keyword. It stands for the character <. Use this
to escape the special meaning of the <> items mentioned.
REDEFINING AND DELETING
To redefine the same command use the ! argument: >
:command -nargs=+ Say :echo "<args>"
:command! -nargs=+ Say :echo <q-args>
To delete a user command use ":delcommand". It takes a single argument, which
is the name of the command. Example: >
:delcommand SaveIt
To delete all the user commands: >
:comclear
Careful, this can't be undone!
More details about all this in the reference manual: |user-commands|.
==============================================================================
*40.3* Autocommands
An autocommand is a command that is executed automatically in response to some
event, such as a file being read or written or a buffer change. Through the
use of autocommands you can train Vim to edit compressed files, for example.
That is used in the |gzip| plugin.
Autocommands are very powerful. Use them with care and they will help you
avoid typing many commands. Use them carelessly and they will cause a lot of
trouble.
Suppose you want to replace a datestamp on the end of a file every time it is
written. First you define a function: >
:function DateInsert()
: $delete
: read !date
:endfunction
You want this function to be called each time, just before a buffer is written
to a file. This will make that happen: >
:autocmd BufWritePre * call DateInsert()
"BufWritePre" is the event for which this autocommand is triggered: Just
before (pre) writing a buffer to a file. The "*" is a pattern to match with
the file name. In this case it matches all files.
With this command enabled, when you do a ":write", Vim checks for any
matching BufWritePre autocommands and executes them, and then it
performs the ":write".
The general form of the :autocmd command is as follows: >
:autocmd [group]