example.
When defining several autocommands for a certain group, use the ":augroup"
command. For example, let's define autocommands for C programs: >
:augroup cprograms
: autocmd BufReadPost *.c,*.h :set sw=4 sts=4
: autocmd BufReadPost *.cpp :set sw=3 sts=3
:augroup END
This will do the same as: >
:autocmd cprograms BufReadPost *.c,*.h :set sw=4 sts=4
:autocmd cprograms BufReadPost *.cpp :set sw=3 sts=3
To delete all autocommands in the "cprograms" group: >
:autocmd! cprograms
NESTING
Generally, commands executed as the result of an autocommand event will not
trigger any new events. If you read a file in response to a FileChangedShell
event, it will not trigger the autocommands that would set the syntax, for
example. To make the events triggered, add the "++nested" flag: >
:autocmd FileChangedShell * ++nested edit
EXECUTING AUTOCOMMANDS
It is possible to trigger an autocommand by pretending an event has occurred.
This is useful to have one autocommand trigger another one. Example: >
:autocmd BufReadPost *.new execute "doautocmd BufReadPost " . expand("<afile>:r")
This defines an autocommand that is triggered when a new file has been edited.
The file name must end in ".new". The ":execute" command uses expression
evaluation to form a new command and execute it. When editing the file
"tryout.c.new" the executed command will be: >
:doautocmd BufReadPost tryout.c
The expand() function takes the "<afile>" argument, which stands for the file
name the autocommand was executed for, and takes the root of the file name
with ":r".
":doautocmd" executes on the current buffer. The ":doautoall" command works
like "doautocmd" except it executes on all the buffers.
USING NORMAL MODE COMMANDS
The commands executed by an autocommand are Command-line commands. If you
want to use a Normal mode command, the ":normal" command can be used.
Example: >
:autocmd BufReadPost *.log normal G
This will make the cursor jump to the last line of `*.log` files when you start
to edit it.
Using the ":normal" command is a bit tricky. First of all, make sure its
argument is a complete command, including all the arguments. When you use "i"
to go to Insert mode, there must also be a <Esc> to leave Insert mode again.
If you use a "/" to start a search pattern, there must be a <CR> to execute
it.
The ":normal" command uses all the text after it as commands. Thus there
can be no | and another command following. To work around this, put the
":normal" command inside an ":execute" command. This also makes it possible
to pass unprintable characters in a convenient way. Example: >
:autocmd BufReadPost *.chg execute "normal ONew entry:\<Esc>" |
\ 1read !date
This also shows the use of a backslash to break a long command into more
lines. This can be used in Vim scripts (not at the command line).
When you want the autocommand do something complicated, which involves jumping
around in the file and then returning to the original position, you may want
to restore the view on the file. See |restore-position| for an example.
IGNORING EVENTS
At times, you will not want to trigger an autocommand. The 'eventignore'
option contains a list of events that will be totally ignored. For example,
the following causes events for entering and leaving a window to be ignored: >
:set eventignore=WinEnter,WinLeave
To ignore all events, use the following command: >
:set eventignore=all
To set it back to the normal behavior, make 'eventignore' empty:
>
:set eventignore=
<
==============================================================================
Next chapter: |usr_41.txt| Write a Vim script
Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: