: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 * | augroup END
augroup mine | au BufRead * set tw=70 | augroup END
Instead you can put the group name into the command:
au! mine BufRead *
au mine BufRead * set tw=70
Or use :execute
:
augroup mine | exe "au! BufRead *" | augroup END
augroup mine | exe "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.
:autocmd
adds to the list of autocommands regardless of whether they are
already present. When your .vimrc file is sourced twice, the autocommands
will appear twice. To avoid this, define your autocommands in a group, so
that you can easily clear them:
augroup vimrc
autocmd! " Remove all vimrc autocommands
au BufNewFile,BufRead *.html so <sfile>:h/html.vim
augroup END
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.
You can specify a comma-separated list of event names. No white space can be
used in this list. The command applies to all the events in the list.
For READING FILES there are four kinds of events possible:
BufNewFile starting to edit a non-existent file
BufReadPre BufReadPost starting to edit an existing file
FilterReadPre FilterReadPost read the temp file with filter output
FileReadPre FileReadPost any other file read
Vim uses only one of these four kinds when reading a file. The "Pre" and
"Post" events are both triggered, before and after reading the file.
Note that the autocommands for the *ReadPre events and all the Filter events
are not allowed to change the current buffer (you will get an error message if
this happens). This is to prevent the file to be read into the wrong buffer.
Note that the 'modified' flag is reset AFTER executing the BufReadPost
and BufNewFile autocommands. But when the 'modified' option was set by the
autocommands, this doesn't happen.
You can use the 'eventignore' option to ignore a number of events or all
events.
autocommand-events {event}
Vim recognizes the following events. Vim ignores the case of event names
(e.g., you can use "BUFread" or "bufread" instead of "BufRead").
First an overview by function with a short explanation. Then the list
alphabetically with full explanations autocmd-events-abc.
Name triggered by
Reading
BufNewFile starting to edit a file that doesn't exist
BufReadPre starting to edit a new buffer, before reading the file
BufRead starting to edit a new buffer, after reading the file
BufReadPost starting to edit a new buffer, after reading the file
BufReadCmd before starting to edit a new buffer Cmd-event
FileReadPre before reading a file with a ":read" command
FileReadPost after reading a file with a ":read" command
FileReadCmd before reading a file with a ":read" command Cmd-event
FilterReadPre before reading a file from a filter command
FilterReadPost after reading a file from a filter command
StdinReadPre before reading from stdin into the buffer
StdinReadPost After reading from the stdin into the buffer
Writing
BufWrite starting to write the whole buffer to a file
BufWritePre starting to write the whole buffer to a file
BufWritePost after writing the whole buffer to a file
BufWriteCmd before writing the whole buffer to a file Cmd-event
FileWritePre starting to write part of a buffer to a file
FileWritePost after writing part of a buffer to a file
FileWriteCmd before writing part of a buffer to a file Cmd-event
FileAppendPre starting to append to a file
FileAppendPost after appending to a file
FileAppendCmd before appending to a file Cmd-event
FilterWritePre starting to write a file for a filter command or diff
FilterWritePost after writing a file for a filter command or diff
Buffers
BufAdd just after adding a buffer to the buffer list
BufCreate just after adding a buffer to the buffer list
BufDelete before deleting a buffer from the buffer list
BufWipeout before completely deleting a buffer
BufFilePre before changing the name of the current buffer
BufFilePost after changing the name of the current buffer
BufEnter after entering a buffer
BufLeave before leaving to another buffer
BufWinEnter after a buffer is displayed in a window
BufWinLeave before a buffer is removed from a window
BufUnload before unloading a buffer
BufHidden just after a buffer has become hidden
BufNew just after creating a new buffer
SwapExists detected an existing swap file
TermOpen when a terminal job starts
TermClose when a terminal job ends
Options
FileType when the 'filetype' option has been set
Syntax when the 'syntax' option has been set
OptionSet after setting any option
Startup and exit
VimEnter after doing all the startup stuff
GUIEnter after starting the GUI successfully
GUIFailed after starting the GUI failed
TermResponse after the terminal response to t_RV is received
QuitPre when using :quit
, before deciding whether to quit
VimLeavePre before exiting Vim, before writing the shada file
VimLeave before exiting Vim, after writing the shada file
Various
DirChanged after the current-directory was changed
FileChangedShell Vim notices that a file changed since editing started
FileChangedShellPost after handling a file changed since editing started
FileChangedRO before making the first change to a read-only file
ShellCmdPost after executing a shell command
ShellFilterPost after filtering with a shell command
CmdUndefined a user command is used but it isn't defined
FuncUndefined a user function is used but it isn't defined
SpellFileMissing a spell file is used but it can't be found
SourcePre before sourcing a Vim script
SourceCmd before sourcing a Vim script Cmd-event
VimResized after the Vim window size changed
FocusGained Vim got input focus
FocusLost Vim lost input focus
CursorHold the user doesn't press a key for a while
CursorHoldI the user doesn't press a key for a while in Insert mode
CursorMoved the cursor was moved in Normal mode
CursorMovedI the cursor was moved in Insert mode
WinNew after creating a new window
WinEnter after entering another window
WinLeave before leaving a window
TabEnter after entering another tab page
TabLeave before leaving a tab page
TabNew when creating a new tab page
TabNewEntered after entering a new tab page
TabClosed after closing a tab page
CmdlineEnter after entering cmdline mode
CmdlineLeave before leaving cmdline mode
CmdwinEnter after entering the command-line window
CmdwinLeave before leaving the command-line window
InsertEnter starting Insert mode
InsertChange when typing <Insert> while in Insert or Replace mode
InsertLeave when leaving Insert mode
InsertCharPre when a character was typed in Insert mode, before
inserting it
TextYankPost when some text is yanked or deleted
TextChanged after a change was made to the text in Normal mode
TextChangedI after a change was made to the text in Insert mode
ColorScheme after loading a color scheme
RemoteReply a reply from a server Vim was received
QuickFixCmdPre before a quickfix command is run
QuickFixCmdPost after a quickfix command is run
SessionLoadPost after loading a session file
MenuPopup just before showing the popup menu
CompleteDone after Insert mode completion is done
User to be used in combination with ":doautocmd"
BufCreate BufAdd
BufAdd or BufCreate Just after creating a new buffer which is
added to the buffer list, or adding a buffer
to the buffer list.
Also used just after a buffer in the buffer
list has been renamed.
The BufCreate event is for historic reasons.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being created "<afile>".
BufDelete
BufDelete Before deleting a buffer from the buffer list.
The BufUnload may be called first (if the
buffer was loaded).
Also used just before a buffer in the buffer
list is renamed.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being deleted "<afile>" and "<abuf>".
Don't change to another buffer, it will cause
problems.
BufEnter
BufEnter After entering a buffer. Useful for setting
options for a file type. Also executed when
starting to edit a buffer, after the
BufReadPost autocommands.
BufFilePost
BufFilePost After changing the name of the current buffer
with the ":file" or ":saveas" command.
BufFilePre
BufFilePre Before changing the name of the current buffer
with the ":file" or ":saveas" command.
BufHidden
BufHidden Just after a buffer has become hidden. That
is, when there are no longer windows that show
the buffer, but the buffer is not unloaded or
deleted. Not used for ":qa" or ":q" when
exiting Vim.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being unloaded "<afile>".
BufLeave
BufLeave Before leaving to another buffer. Also when
leaving or closing the current window and the
new current window is not for the same buffer.
Not used for ":qa" or ":q" when exiting Vim.
BufNew
BufNew Just after creating a new buffer. Also used
just after a buffer has been renamed. When
the buffer is added to the buffer list BufAdd
will be triggered too.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being created "<afile>".
BufNewFile
BufNewFile When starting to edit a file that doesn't
exist. Can be used to read in a skeleton
file.
BufRead BufReadPost
BufRead or BufReadPost When starting to edit a new buffer, after
reading the file into the buffer, before
executing the modelines. See BufWinEnter
for when you need to do something after
processing the modelines.
This does NOT work for ":r file". Not used
when the file doesn't exist. Also used after
successfully recovering a file.
Also triggered for the filetypedetect group
when executing ":filetype detect" and when
writing an unnamed buffer in a way that the
buffer gets a name.
BufReadCmd
BufReadCmd Before starting to edit a new buffer. Should
read the file into the buffer. Cmd-event
BufReadPre E200 E201
BufReadPre When starting to edit a new buffer, before
reading the file into the buffer. Not used
if the file doesn't exist.
BufUnload
BufUnload Before unloading a buffer. This is when the
text in the buffer is going to be freed. This
may be after a BufWritePost and before a
BufDelete. Also used for all buffers that are
loaded when Vim is going to exit.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being unloaded "<afile>".
Don't change to another buffer or window, it
will cause problems!
When exiting and v:dying is 2 or more this
event is not triggered.
BufWinEnter
BufWinEnter After a buffer is displayed in a window. This
can be when the buffer is loaded (after
processing the modelines) or when a hidden
buffer is displayed in a window (and is no
longer hidden).
Does not happen for :split without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.
BufWinLeave
BufWinLeave Before a buffer is removed from a window.
Not when it's still visible in another window.
Also triggered when exiting. It's triggered
before BufUnload or BufHidden.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being unloaded "<afile>".
When exiting and v:dying is 2 or more this
event is not triggered.
BufWipeout
BufWipeout Before completely deleting a buffer. The
BufUnload and BufDelete events may be called
first (if the buffer was loaded and was in the
buffer list). Also used just before a buffer
is renamed (also when it's not in the buffer
list).
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer being deleted "<afile>".
Don't change to another buffer, it will cause
problems.
BufWrite BufWritePre
BufWrite or BufWritePre Before writing the whole buffer to a file.
BufWriteCmd
BufWriteCmd Before writing the whole buffer to a file.
Should do the writing of the file and reset
'modified' if successful, unless '+' is in
'cpo' and writing to another file cpo-+.
The buffer contents should not be changed.
When the command resets 'modified' the undo
information is adjusted to mark older undo
states as 'modified', like :write does.
Cmd-event
BufWritePost
BufWritePost After writing the whole buffer to a file
(should undo the commands for BufWritePre).
CmdUndefined
CmdUndefined When a user command is used but it isn't
defined. Useful for defining a command only
when it's used. The pattern is matched
against the command name. Both <amatch> and
<afile> are set to the name of the command.
NOTE: Autocompletion won't work until the
command is defined. An alternative is to
always define the user command and have it
invoke an autoloaded function. See autoload.
CmdlineEnter
CmdlineEnter After moving the cursor to the command line,
where the user can type a command or search
string.
<afile> is set to a single character,
indicating the type of command-line.
cmdline-char
Sets these v:event keys:
cmdlevel
cmdtype
CmdlineLeave
CmdlineLeave Before leaving the command line.
<afile> is set to a single character,
indicating the type of command-line.
cmdline-char
Sets these v:event keys:
abort (mutable)
cmdlevel
cmdtype
Note: abort
can only be changed from false
to true. An autocmd cannot execute an already
aborted cmdline by changing it to false.
CmdwinEnter
CmdwinEnter After entering the command-line window.
Useful for setting options specifically for
this special type of window. This is
triggered _instead_ of BufEnter and WinEnter.
<afile> is set to a single character,
indicating the type of command-line.
cmdwin-char
CmdwinLeave
CmdwinLeave Before leaving the command-line window.
Useful to clean up any global setting done
with CmdwinEnter. This is triggered _instead_
of BufLeave and WinLeave.
<afile> is set to a single character,
indicating the type of command-line.
cmdwin-char
ColorScheme
ColorScheme After loading a color scheme. :colorscheme
The pattern is matched against the
colorscheme name. <afile> can be used for the
name of the actual file where this option was
set, and <amatch> for the new colorscheme
name.
CursorMoved
CursorMoved After the cursor was moved in Normal or Visual
mode. Also when the text of the cursor line
has been changed, e.g., with "x", "rx" or "p".
Not triggered when there is typeahead or when
an operator is pending.
For an example see match-parens.
Careful: This is triggered very often, don't
do anything that the user does not expect or
that is slow.
CursorMovedI
CursorMovedI After the cursor was moved in Insert mode.
Not triggered when the popup menu is visible.
Otherwise the same as CursorMoved.
DirChanged
DirChanged After the current-directory was changed.
Sets these v:event keys:
cwd: current working directory
scope: "global", "tab", "window"
Recursion is ignored.
FileAppendCmd
FileAppendCmd Before appending to a file. Should do the
appending to the file. Use the '[ and ']
marks for the range of lines.Cmd-event
FileAppendPost
FileAppendPost After appending to a file.
FileAppendPre
FileAppendPre Before appending to a file. Use the '[ and ']
marks for the range of lines.
FileChangedRO
FileChangedRO Before making the first change to a read-only
file. Can be used to check-out the file from
a source control system. Not triggered when
the change was caused by an autocommand.
This event is triggered when making the first
change in a buffer or the first change after
'readonly' was set, just before the change is
applied to the text.
WARNING: If the autocommand moves the cursor
the effect of the change is undefined.
E788
It is not allowed to change to another buffer
here. You can reload the buffer but not edit
another one.
E881
If the number of lines changes saving for undo
may fail and the change will be aborted.
FileChangedShell
FileChangedShell When Vim notices that the modification time of
a file has changed since editing started.
Also when the file attributes of the file
change or when the size of the file changes.
timestamp
Mostly triggered after executing a shell
command, but also with a :checktime command
or when gvim regains input focus.
This autocommand is triggered for each changed
file. It is not used when 'autoread' is set
and the buffer was not changed. If a
FileChangedShell autocommand is present the
warning message and prompt is not given.
The v:fcs_reason variable is set to indicate
what happened and v:fcs_choice can be used
to tell Vim what to do next.
NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer that was changed, which is in "<afile>".
NOTE: The commands must not change the current
buffer, jump to another buffer or delete a
buffer. E246 E811
NOTE: This event never nests, to avoid an
endless loop. This means that while executing
commands for the FileChangedShell event no
other FileChangedShell event will be
triggered.
FileChangedShellPost
FileChangedShellPost After handling a file that was changed outside
of Vim. Can be used to update the statusline.
FileReadCmd
FileReadCmd Before reading a file with a ":read" command.
Should do the reading of the file. Cmd-event
FileReadPost
FileReadPost After reading a file with a ":read" command.
Note that Vim sets the '[ and '] marks to the
first and last line of the read. This can be
used to operate on the lines just read.
FileReadPre
FileReadPre Before reading a file with a ":read" command.
FileType
FileType When the 'filetype' option has been set. The
pattern is matched against the filetype.
<afile> can be used for the name of the file
where this option was set, and <amatch> for
the new value of 'filetype'. Navigating to
another window or buffer is not allowed.
See filetypes.
FileWriteCmd
FileWriteCmd Before writing to a file, when not writing the
whole buffer. Should do the writing to the
file. Should not change the buffer. Use the
'[ and '] marks for the range of lines.
Cmd-event
FileWritePost
FileWritePost After writing to a file, when not writing the
whole buffer.
FileWritePre
FileWritePre Before writing to a file, when not writing the
whole buffer. Use the '[ and '] marks for the
range of lines.
FilterReadPost
FilterReadPost After reading a file from a filter command.
Vim checks the pattern against the name of
the current buffer as with FilterReadPre.
Not triggered when 'shelltemp' is off.
FilterReadPre E135
FilterReadPre Before reading a file from a filter command.
Vim checks the pattern against the name of
the current buffer, not the name of the
temporary file that is the output of the
filter command.
Not triggered when 'shelltemp' is off.
FilterWritePost
FilterWritePost After writing a file for a filter command or
making a diff.
Vim checks the pattern against the name of
the current buffer as with FilterWritePre.
Not triggered when 'shelltemp' is off.
FilterWritePre
FilterWritePre Before writing a file for a filter command or
making a diff.
Vim checks the pattern against the name of
the current buffer, not the name of the
temporary file that is the output of the
filter command.
Not triggered when 'shelltemp' is off.
FocusGained
FocusGained When Vim got input focus. Only for the GUI
version and a few console versions where this
can be detected.
FocusLost
FocusLost When Vim lost input focus. Only for the GUI
version and a few console versions where this
can be detected. May also happen when a
dialog pops up.
FuncUndefined
FuncUndefined When a user function is used but it isn't
defined. Useful for defining a function only
when it's used. The pattern is matched
against the function name. Both <amatch> and
<afile> are set to the name of the function.
NOTE: When writing Vim scripts a better
alternative is to use an autoloaded function.
See autoload-functions.
GUIEnter
GUIEnter After starting the GUI successfully, and after
opening the window. It is triggered before
VimEnter when using gvim. Can be used to
position the window from a gvimrc file:
autocmd GUIEnter * winpos 100 50
GUIFailed
GUIFailed After starting the GUI failed. Vim may
continue to run in the terminal, if possible
(only on Unix and alikes, when connecting the
X server fails). You may want to quit Vim:
autocmd GUIFailed * qall
InsertChange
InsertChange When typing <Insert> while in Insert or
Replace mode. The v:insertmode variable
indicates the new mode.
Be careful not to move the cursor or do
anything else that the user does not expect.
InsertCharPre
InsertCharPre When a character is typed in Insert mode,
before inserting the char.
The v:char variable indicates the char typed
and can be changed during the event to insert
a different character. When v:char is set
to more than one character this text is
inserted literally.
It is not allowed to change the text textlock.
The event is not triggered when 'paste' is
set.
TextYankPost
TextYankPost Just after a yank or deleting command, but not
if the black hole register quote_ is used nor
for setreg(). Pattern must be *.
Sets these v:event keys:
operator
regcontents
regname
regtype
Recursion is ignored.
It is not allowed to change the text textlock.
InsertEnter
InsertEnter Just before starting Insert mode. Also for
Replace mode and Virtual Replace mode. The
v:insertmode variable indicates the mode.
Be careful not to do anything else that the
user does not expect.
The cursor is restored afterwards. If you do
not want that set v:char to a non-empty
string.
InsertLeave
InsertLeave When leaving Insert mode. Also when using
CTRL-O i_CTRL-O. But not for i_CTRL-C.
MenuPopup
MenuPopup Just before showing the popup menu (under the
right mouse button). Useful for adjusting the
menu for what is under the cursor or mouse
pointer.
The pattern is matched against a single
character representing the mode:
n Normal
v Visual
o Operator-pending
i Insert
c Command line
OptionSet
OptionSet After setting an option. The pattern is
matched against the long option name.
The v:option_old variable indicates the
old option value, v:option_new variable
indicates the newly set value, the
v:option_type variable indicates whether
it's global or local scoped and <amatch>
indicates what option has been set.
Usage example: Check for the existence of the
directory in the 'backupdir' and 'undodir'
options, create the directory if it doesn't
exist yet.
Note: It's a bad idea to reset an option
during this autocommand, this may break a
plugin. You can always use :noa
to prevent
triggering this autocommand.
QuickFixCmdPre
QuickFixCmdPre Before a quickfix command is run (:make,
:lmake, :grep, :lgrep, :grepadd,
:lgrepadd, :vimgrep, :lvimgrep,
:vimgrepadd, :lvimgrepadd, :cscope,
:cfile, :cgetfile, :caddfile, :lfile,
:lgetfile, :laddfile, :helpgrep,
:lhelpgrep, :cexpr, :cgetexpr,
:caddexpr, :cbuffer, :cgetbuffer,
:caddbuffer).
The pattern is matched against the command
being run. When :grep is used but 'grepprg'
is set to "internal" it still matches "grep".
This command cannot be used to set the
'makeprg' and 'grepprg' variables.
If this command causes an error, the quickfix
command is not executed.
QuickFixCmdPost
QuickFixCmdPost Like QuickFixCmdPre, but after a quickfix
command is run, before jumping to the first
location. For :cfile and :lfile commands
it is run after error file is read and before
moving to the first error.
See QuickFixCmdPost-example.
QuitPre
QuitPre When using :quit
, :wq
or :qall
, before
deciding whether it closes the current window
or quits Vim. Can be used to close any
non-essential window if the current window is
the last ordinary window.
RemoteReply
RemoteReply When a reply from a Vim that functions as
server was received server2client(). The
pattern is matched against the {serverid}.
<amatch> is equal to the {serverid} from which
the reply was sent, and <afile> is the actual
reply string.
Note that even if an autocommand is defined,
the reply should be read with remote_read()
to consume it.
SessionLoadPost
SessionLoadPost After loading the session file created using
the :mksession command.
ShellCmdPost
ShellCmdPost After executing a shell command with :!cmd,
:make and :grep. Can be used to check for
any changed files.
For non-blocking shell commands, see
job-control.
ShellFilterPost
ShellFilterPost After executing a shell command with
":{range}!cmd", ":w !cmd" or ":r !cmd".
Can be used to check for any changed files.
SourcePre
SourcePre Before sourcing a Vim script. :source
<afile> is the name of the file being sourced.
SourceCmd
SourceCmd When sourcing a Vim script. :source
<afile> is the name of the file being sourced.
The autocommand must source this file.
Cmd-event
SpellFileMissing
SpellFileMissing When trying to load a spell checking file and
it can't be found. The pattern is matched
against the language. <amatch> is the
language, 'encoding' also matters. See
spell-SpellFileMissing.
StdinReadPost
StdinReadPost After reading from the stdin into the buffer,
before executing the modelines. Only used
when the "-" argument was used when Vim was
started --.
StdinReadPre
StdinReadPre Before reading from stdin into the buffer.
Only used when the "-" argument was used when
Vim was started --.
SwapExists
SwapExists Detected an existing swap file when starting
to edit a file. Only when it is possible to
select a way to handle the situation, when Vim
would ask the user what to do.
The v:swapname variable holds the name of
the swap file found, <afile> the file being
edited. v:swapcommand may contain a command
to be executed in the opened file.
The commands should set the v:swapchoice
variable to a string with one character to
tell Vim what should be done next:
'o' open read-only
'e' edit the file anyway
'r' recover
'd' delete the swap file
'q' quit, don't edit the file
'a' abort, like hitting CTRL-C
When set to an empty string the user will be
asked, as if there was no SwapExists autocmd.
E812
It is not allowed to change to another buffer,
change a buffer name or change directory
here.
Syntax
Syntax When the 'syntax' option has been set. The
pattern is matched against the syntax name.
<afile> can be used for the name of the file
where this option was set, and <amatch> for
the new value of 'syntax'.
See :syn-on.
TabEnter
TabEnter Just after entering a tab page. tab-page
After triggering the WinEnter and before
triggering the BufEnter event.
TabLeave
TabLeave Just before leaving a tab page. tab-page
A WinLeave event will have been triggered
first.
TabNew
TabNew When creating a new tab page. tab-page
After WinEnter and before TabEnter.
TabNewEntered
TabNewEntered After entering a new tab page. tab-page
After BufEnter.
TabClosed
TabClosed After closing a tab page. <afile> can be used
for the tab page number.
TermClose
TermClose When a terminal job ends.
TermOpen
TermOpen When a terminal job is starting. Can be
used to configure the terminal buffer.
TermResponse
TermResponse After the response to t_RV is received from
the terminal. The value of v:termresponse
can be used to do things depending on the
terminal version. Note that this event may be
triggered halfway through another event
(especially if file I/O, a shell command, or
anything else that takes time is involved).
TextChanged
TextChanged After a change was made to the text in the
current buffer in Normal mode. That is when
b:changedtick has changed.
Not triggered when there is typeahead or when
an operator is pending.
Careful: This is triggered very often, don't
do anything that the user does not expect or
that is slow.
TextChangedI
TextChangedI After a change was made to the text in the
current buffer in Insert mode.
Not triggered when the popup menu is visible.
Otherwise the same as TextChanged.
User
User Never executed automatically. To be used for
autocommands that are only executed with
":doautocmd".
UserGettingBored
UserGettingBored When the user presses the same key 42 times.
Just kidding! :-)
VimEnter
VimEnter After doing all the startup stuff, including
loading vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.
Just before this event is triggered the
v:vim_did_enter variable is set, so that you
can do:
if v:vim_did_enter
call s:init()
else
au VimEnter * call s:init()
endif
VimLeave
VimLeave Before exiting Vim, just after writing the
.shada file. Executed only once, like
VimLeavePre.
< Use v:dying to detect an abnormal exit.
Use v:exiting to get the exit code.
Not triggered if v:dying is 2 or more.
VimLeavePre
VimLeavePre Before exiting Vim, just before writing the
.shada file. This is executed only once,
if there is a match with the name of what
happens to be the current buffer when exiting.
Mostly useful with a "*" pattern.
autocmd VimLeavePre * call CleanupStuff()
Use v:dying to detect an abnormal exit.
Use v:exiting to get the exit code.
Not triggered if v:dying is 2 or more.
VimResized
VimResized After the Vim window was resized, thus 'lines'
and/or 'columns' changed. Not when starting
up though.
WinEnter
WinEnter After entering another window. Not done for
the first window, when Vim has just started.
Useful for setting the window height.
If the window is for another buffer, Vim
executes the BufEnter autocommands after the
WinEnter autocommands.
Note: When using ":split fname" the WinEnter
event is triggered after the split but before
the file "fname" is loaded.
WinLeave
WinLeave Before leaving a window. If the window to be
entered next is for a different buffer, Vim
executes the BufLeave autocommands before the
WinLeave autocommands (but not for ":new").
Not used for ":qa" or ":q" when exiting Vim.
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 *.gz !gzip <afile>:r
autocmd FileAppendPre *.gz !gunzip <afile>
autocmd FileAppendPre *.gz !mv <afile>:r <afile>
autocmd FileAppendPost *.gz !mv <afile> <afile>:r
autocmd FileAppendPost *.gz !gzip <afile>:r
augroup END
The "gzip" group is used to be able to delete any existing autocommands with
":autocmd!", for when the file is sourced twice.
(":r" is the file name without the extension, see :_%:)
The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
FileAppendPost and VimLeave events do not set or reset the changed flag of the
buffer. When you decompress the buffer with the BufReadPost autocommands, you
can still exit with ":q". When you use ":undo" in BufWritePost to undo the
changes made by BufWritePre commands, you can still do ":q" (this also makes
"ZZ" work). If you do want the buffer to be marked as modified, set the
'modified' option.
To execute Normal mode commands from an autocommand, use the ":normal"
command. Use with care! If the Normal mode command is not finished, the user
needs to type characters (e.g., after ":normal m" you need to type a mark
name).
If you want the buffer to be unmodified after changing it, reset the
'modified' option. This makes it possible to exit the buffer with ":q"
instead of ":q!".
autocmd-nested E218
By default, autocommands do not nest. If you use ":e" or ":w" in an
autocommand, Vim does not execute the BufRead and BufWrite autocommands for
those commands. If you do want this, use the "nested" flag for those commands
in which you want nesting. For example:
autocmd FileChangedShell *.c nested e!
The nesting is limited to 10 levels to get out of recursive loops.
It's possible to use the ":au" command in an autocommand. This can be a
self-modifying command! This can be useful for an autocommand that should
execute only once.
If you want to skip autocommands for one command, use the :noautocmd command
modifier or the 'eventignore' option.
Note: When reading a file (with ":read file" or with a filter command) and the
last line in the file does not have an <EOL>, Vim remembers this. At the next
write (with ":write file" or with a filter command), if the same line is
written again as the last line in a file AND 'binary' is set, Vim does not
supply an <EOL>. This makes a filter command on the just read lines write the
same file as was read, and makes a write command on just filtered lines write
the same file as was read from the filter. For example, another way to write
a compressed file:
autocmd FileWritePre *.gz set bin|'[,']!gzip
autocmd FileWritePost *.gz undo|set nobin
autocommand-pattern
You can specify multiple patterns, separated by commas. Here are some
examples:
autocmd BufRead * set tw=79 nocin ic infercase fo=2croq
autocmd BufRead .letter set tw=72 fo=2tcrq
autocmd BufEnter .letter set dict=/usr/lib/dict/words
autocmd BufLeave .letter set dict=
autocmd BufRead,BufNewFile *.c,*.h set tw=0 cin noic
autocmd BufEnter *.c,*.h abbr FOR for (i = 0; i < 3; ++i)<CR>{<CR>}<Esc>O
autocmd BufLeave *.c,*.h unabbr FOR
For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.):
autocmd BufEnter ?akefile* set include=^s\=include
autocmd BufLeave ?akefile* set include&
To always start editing C files at the first function:
autocmd BufRead *.c,*.h 1;/^{
Without the "1;" above, the search would start from wherever the file was
entered, rather than from the start of the file.