*autocmd.txt* For Vim version 7.4. Last change: 2016 Sep 03
VIM REFERENCE MANUAL by Bram Moolenaar
Automatic commands *autocommand*
For a basic explanation, see section |40.3| in the user manual.
1. Introduction |autocmd-intro|
2. Defining autocommands |autocmd-define|
3. Removing autocommands |autocmd-remove|
4. Listing autocommands |autocmd-list|
5. Events |autocmd-events|
6. Patterns |autocmd-patterns|
7. Buffer-local autocommands |autocmd-buflocal|
8. Groups |autocmd-groups|
9. Executing autocommands |autocmd-execute|
10. Using autocommands |autocmd-use|
11. Disabling autocommands |autocmd-disable|
{Vi does not have any of these commands}
{only when the |+autocmd| feature has not been disabled at compile time}
==============================================================================
1. Introduction *autocmd-intro*
You can specify commands to be executed automatically when reading or writing
a file, when entering or leaving a buffer or window, and when exiting Vim.
For example, you can create an autocommand to set the 'cindent' option for
files matching *.c. You can also use autocommands to implement advanced
features, such as editing compressed files (see |gzip-example|). The usual
place to put autocommands is in your .vimrc or .exrc file.
*E203* *E204* *E143* *E855*
WARNING: Using autocommands is very powerful, and may lead to unexpected side
effects. Be careful not to destroy your text.
- It's a good idea to do some testing on an expendable copy of a file first.
For example: If you use autocommands to decompress a file when starting to
edit it, make sure that the autocommands for compressing when writing work
correctly.
- Be prepared for an error halfway through (e.g., disk full). Vim will mostly
be able to undo the changes to the buffer, but you may have to clean up the
changes to other files by hand (e.g., compress a file that has been
decompressed).
- If the BufRead* events allow you to edit a compressed file, the FileRead*
events should do the same (this makes recovery possible in some rare cases).
It's a good idea to use the same autocommands for the File* and Buf* events
when possible.
==============================================================================
2. Defining autocommands *autocmd-define*
*:au* *:autocmd*
:au[tocmd] [group] {event} {pat} [nested] {cmd}
Add {cmd} to the list of commands that Vim will
execute automatically on {event} for a file matching
{pat} |autocmd-patterns|.
Vim always adds the {cmd} after existing autocommands,
so that the autocommands execute in the order in which
they were given. See |autocmd-nested| for [nested].
The special pattern <buffer> or <buffer=N> defines a buffer-local autocommand.
See |autocmd-buflocal|.
Note: The ":autocmd" command can only be followed by another command when the
'|' appears before {cmd}. This works: >
:augroup mine | au! BufRead | augroup END
But this sees "augroup" as part of the defined command: >
:augroup mine | au BufRead * set tw=70 | augroup END
Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
arguments are not expanded when the autocommand is defined. These will be
expanded when the Event is recognized, and the {cmd} is executed. The only
exception is that "<sfile>" is expanded when the autocmd is defined. Example:
>
:au BufNewFile,BufRead *.html so <sfile>:h/html.vim
Here Vim expands <sfile> to the name of the file containing this line.
When your .vimrc file is sourced twice, the autocommands will appear twice.
To avoid this, put this command in your .vimrc file, before defining
autocommands: >
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a variable
to ensure that Vim includes the autocommands only once: >
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
When the [group] argument is not given, Vim uses the current group (as defined
with ":augroup"); otherwise, Vim uses the group defined with [group]. Note
that [group] must have been defined before. You cannot define a new group
with ":au group ..."; use ":augroup" for that.
While testing autocommands, you might find the 'verbose' option to be useful: >
:set verbose=9
This setting makes Vim echo the autocommands as it executes them.
When defining an autocommand in a script, it will be able to call functions
local to the script and use mappings local to the script. When the event is
triggered and the command executed, it will run in the context of the script
it was defined in. This matters if |<SID>| is used in a command.
When executing the commands, the message from one command overwrites a
previous message. This is different from when executing the commands
manually. Mostly the screen will not scroll up, thus there is no hit-enter
prompt. When one command outputs two messages this can happen anyway.
============================================================================