argument is not given, Vim executes
the autocommands for all groups. When the [group]
argument is included, Vim executes only the matching
autocommands for that group. Undefined group is an
error.
*<nomodeline>*
After applying the autocommands the modelines are
processed, so that their settings overrule the
settings from autocommands when editing a file. This
is skipped if <nomodeline> is specified. You probably
want to use <nomodeline> for events not used when
loading a buffer, such as |User|.
Modelines are also skipped when no matching
autocommands were executed.
*:doautoa* *:doautoall*
:doautoa[ll] [<nomodeline>] [group] {event} [fname]
Like ":doautocmd", but apply the autocommands to each
loaded buffer. The current buffer is done last.
Note that [fname] is used to select the autocommands,
not the buffers to which they are applied. Example: >
augroup mine
autocmd!
autocmd FileType * echo expand('<amatch>')
augroup END
doautoall mine FileType Loaded-Buffer
< Sourcing this script, you'll see as many
"Loaded-Buffer" echoed as there are loaded buffers.
Careful: Don't use this for autocommands that delete a
buffer, change to another buffer or change the
contents of a buffer; the result is unpredictable.
This command is intended for autocommands that set
options, change highlighting, and things like that.
==============================================================================
10. Using autocommands *autocmd-use*
For WRITING FILES there are four possible sets of events. Vim uses only one
of these sets for a write command:
BufWriteCmd BufWritePre BufWritePost writing the whole buffer
FilterWritePre FilterWritePost writing to filter temp file
FileAppendCmd FileAppendPre FileAppendPost appending to a file
FileWriteCmd FileWritePre FileWritePost any other file write
When there is a matching "*Cmd" autocommand, it is assumed it will do the
writing. No further writing is done and the other events are not triggered.
|Cmd-event|
Note that the "*WritePost" commands should undo any changes to the buffer that
were caused by the "*WritePre" commands; otherwise, writing the file will have
the side effect of changing the buffer.
Before executing the autocommands, the buffer from which the lines are to be
written temporarily becomes the current buffer. Unless the autocommands
change the current buffer or delete the previously current buffer, the
previously current buffer is made the current buffer again.
The "*WritePre" and "*AppendPre" autocommands must not delete the buffer from
which the lines are to be written.
The '[ and '] marks have a special position:
- Before the "*ReadPre" event the '[ mark is set to the line just above where
the new lines will be inserted.
- Before the "*ReadPost" event the '[ mark is set to the first line that was
just read, the '] mark to the last line.
- Before executing the "*WriteCmd", "*WritePre" and "*AppendPre" autocommands the '[
mark is set to the first line that will be written, the '] mark to the last
line.
Careful: '[ and '] change when using commands that change the buffer.
In commands which expect a file name, you can use "<afile>" for the file name
that is being read |:<afile>| (you can also use "%" for the current file
name). "<abuf>" can be used for the buffer number of the currently effective
buffer. This also works for buffers that don't have a name. But it doesn't
work for files without a buffer (e.g., with ":r file").
*gzip-example*
Examples for reading and writing compressed files: >
:augroup gzip
: autocmd!
: autocmd BufReadPre,FileReadPre *.gz set bin
: autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
: autocmd BufReadPost,FileReadPost *.gz set nobin
: autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " .. expand("%:r")
: autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
: autocmd BufWritePost,FileWritePost