Nvim :help
pages, generated
from source
using the tree-sitter-vimdoc parser.
autocommand
*.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 file.E203
E204
E143
E855
E937
E952
WARNING: Using autocommands is very powerful, and may lead to unexpected side
effects. Be careful not to destroy your text.
:au
:autocmd
:au[tocmd] [group] {event}
{aupat}
[++once] [++nested] {cmd}
Add {cmd}
to the list of commands that Vim will
execute automatically on {event}
for a file matching
{aupat}
autocmd-pattern.
Note: A quote character is seen as argument to the
:autocmd and won't start a comment.
Nvim always adds {cmd}
after existing autocommands so
they execute in the order in which they were defined.
See autocmd-nested for [++nested].
autocmd-once
If [++once] is supplied the command is executed once,
then removed ("one shot").<buffer>
or <buffer=N> defines a buffer-local autocommand.
See autocmd-buflocal.:augroup mine | au! BufRead | augroup ENDBut this sees "augroup" as part of the defined command:
:augroup mine | au! BufRead * | augroup END :augroup mine | au BufRead * set tw=70 | augroup ENDInstead you can put the group name into the command:
:au! mine BufRead * :au mine BufRead * set tw=70Or use
:execute
::augroup mine | exe "au! BufRead *" | augroup END :augroup mine | exe "au BufRead * set tw=70" | augroup END
autocmd-expand
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.vimHere 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 " Remove all vimrc autocommands autocmd! au BufNewFile,BufRead *.html so <sfile>:h/html.vim augroup ENDIf 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 ... :endifWhen 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.
:set verbose=9This setting makes Vim echo the autocommands as it executes them.
{event}
{aupat}
[++once] [++nested] {cmd}
Remove all autocommands associated with {event}
and
{aupat}
, and add the command {cmd}
.
See autocmd-once for [++once].
See autocmd-nested for [++nested].{event}
{aupat}
Remove all autocommands associated with {event}
and
{aupat}
.{aupat}
Remove all autocommands associated with {aupat}
for
all events.{event}
Remove ALL autocommands for {event}
.
Warning: You should not do this without a group for
BufRead and other common events, it can break
plugins, syntax highlighting, etc.{event}
{aupat}
Show the autocommands associated with {event}
and
{aupat}
.{aupat}
Show the autocommands associated with {aupat}
for all
events.{event}
Show all autocommands for {event}
.<buffer>
or <buffer=N>. See autocmd-buflocal.:autocmd-verbose
When 'verbose' is non-zero, listing an autocommand will also display where it
was last defined. Example::verbose autocmd BufEnter FileExplorer BufEnter * call s:LocalBrowse(expand("<amatch>")) Last set from /usr/share/vim/vim-7.0/plugin/NetrwPlugin.vim
BufAdd
BufAdd After adding a new buffer or existing unlisted
buffer to the buffer list (except during
startup, see VimEnter), or renaming a listed
buffer.
Before BufEnter.
NOTE: Current buffer "%" is not the target
buffer "<afile>", "<abuf>". <buffer=abuf>
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: Current buffer "%" is not the target
buffer "<afile>", "<abuf>". <buffer=abuf>
Do not change to another buffer.
BufEnter
BufEnter After entering (visiting, switching-to) a new
or existing buffer. Useful for setting
filetype options. Compare BufNew which
does not trigger for existing buffers.
After BufAdd.
After BufReadPost.
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 Before a buffer becomes hidden: when there are
no longer windows that show the buffer, but
the buffer is not unloaded or deleted.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.BufModifiedSet
BufModifiedSet After the 'modified'
value of a buffer has
been changed.
BufNew
BufNew After creating a new buffer (except during
startup, see VimEnter) or renaming an
existing buffer. Unlike BufEnter, visiting
(switching to) an existing buffer will not
trigger this again.
NOTE: Current buffer "%" is not the target
buffer "<afile>", "<abuf>". <buffer=abuf>
See also BufAdd, BufNewFile.
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
processing modelines. See BufWinEnter to do
something after processing modelines.
Also triggered:
:read file
command
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, when the text in
the buffer is going to be freed.
After BufWritePost.
Before BufDelete.
Triggers for all loaded buffers when Vim is
going to exit.
NOTE: Current buffer "%" is not the target
buffer "<afile>", "<abuf>". <buffer=abuf>
Do not switch buffers or windows!
Not triggered when exiting and v:dying is 2 or
more.
BufWinEnter
BufWinEnter After a buffer is displayed in a window. This
may be when the buffer is loaded (after
processing modelines) or when a hidden buffer
is displayed (and is no longer hidden).
BufWinLeave
BufWinLeave Before a buffer is removed from a window.
Not when it's still visible in another window.
Also triggered when exiting.
Before BufUnload, BufHidden.
NOTE: Current buffer "%" is not the target
buffer "<afile>", "<abuf>". <buffer=abuf>
Not triggered when exiting and v:dying is 2 or
more.
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: Current buffer "%" is not the target
buffer "<afile>", "<abuf>". <buffer=abuf>
Do not change to another buffer.
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).
ChanInfo
ChanInfo State of channel changed, for instance the
client of a RPC channel described itself.
Sets these v:event keys:
info
See nvim_get_chan_info() for the format of
the info Dictionary.
ChanOpen
ChanOpen Just after a channel was opened.
Sets these v:event keys:
info
See nvim_get_chan_info() for the format of
the info Dictionary.
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>
expand to the command name.
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.
CmdlineChanged
CmdlineChanged After a change was made to the text inside
command line. Be careful not to mess up the
command line, it may cause Vim to lock up.
<afile>
expands to the cmdline-char.
CmdlineEnter
CmdlineEnter After entering the command-line (including
non-interactive use of ":" in a mapping: use
<Cmd> instead to avoid this).
The pattern is matched against cmdline-char.
<afile>
expands to the cmdline-char.
Sets these v:event keys:
cmdlevel
cmdtype
CmdlineLeave
CmdlineLeave Before leaving the command-line (including
non-interactive use of ":" in a mapping: use
<Cmd> instead to avoid this).
<afile>
expands to the cmdline-char.
Sets these v:event keys:
abort (mutable)
cmdlevel
cmdtype
Note: abort
can only be changed from false
to true: 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.
<afile>
expands 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.
<afile>
expands to a single character,
indicating the type of command-line.
cmdwin-char
ColorScheme
ColorScheme After loading a color scheme. :colorscheme
Not triggered if the color scheme is not
found.
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.ColorSchemePre
ColorSchemePre Before loading a color scheme. :colorscheme
Useful to setup removing things added by a
color scheme, before another one is loaded.CompleteChanged
After each time the Insert mode completion
menu changed. Not fired on popup menu hide,
use CompleteDonePre or CompleteDone for
that.CompleteDonePre
CompleteDonePre After Insert mode completion is done. Either
when something was completed or abandoning
completion. ins-completion
complete_info() can be used, the info is
cleared after triggering CompleteDonePre.
The v:completed_item variable contains
information about the completed item.CompleteDone
CompleteDone After Insert mode completion is done. Either
when something was completed or abandoning
completion. ins-completion
complete_info() cannot be used, the info is
cleared before triggering CompleteDone. Use
CompleteDonePre if you need it.
v:completed_item gives the completed item.CursorHold
CursorHold When the user doesn't press a key for the time
specified with 'updatetime'. Not triggered
until the user has pressed a key (i.e. doesn't
fire every 'updatetime' ms if you leave Vim to
make some coffee. :) See CursorHold-example
for previewing tags.
This event is only triggered in Normal mode.
It is not triggered when waiting for a command
argument to be typed, or a movement after an
operator.
While recording the CursorHold event is not
triggered. q
<CursorHold>
Internally the autocommand is triggered by the
<CursorHold>
key. In an expression mapping
getchar() may see this character.:let &ro = &ro
CursorHoldI
CursorHoldI Like CursorHold, but in Insert mode. Not
triggered when waiting for another key, e.g.
after CTRL-V
, and not in CTRL-X
mode
insert_expand.CursorMoved
CursorMoved After the cursor was moved in Normal or Visual
mode or to another window. Also when the text
of the cursor line has been changed, e.g. with
"x", "rx" or "p".
Not always triggered when there is typeahead,
while executing commands in a script file, or
when an operator is pending.
For an example see match-parens.
Note: Cannot be skipped with :noautocmd.
Careful: This is triggered very often, don't
do anything that the user does not expect or
that is slow.
CursorMovedC
CursorMovedC After the cursor was moved in the command
line. Be careful not to mess up the command
line, it may cause Vim to lock up.
<afile>
expands to the cmdline-char.
CursorMovedI
CursorMovedI After the cursor was moved in Insert mode.
Not triggered when the popup menu is visible.
Otherwise the same as CursorMoved.
DiffUpdated
DiffUpdated After diffs have been updated. Depending on
what kind of diff is being used (internal or
external) this can be triggered on every
change or when doing :diffupdate.
DirChanged
DirChanged After the current-directory was changed.
The pattern can be:
"window" to trigger on :lcd
"tabpage" to trigger on :tcd
"global" to trigger on :cd
"auto" to trigger on 'autochdir'.
Sets these v:event keys:
cwd: current working directory
scope: "global", "tabpage", "window"
changed_window: v:true if we fired the event
switching window (or tab)
<afile>
is set to the new directory name.
Non-recursive (event cannot trigger itself).
DirChangedPre
DirChangedPre When the current-directory is going to be
changed, as with DirChanged.
The pattern is like with DirChanged.
Sets these v:event keys:
directory: new working directory
scope: "global", "tabpage", "window"
changed_window: v:true if we fired the event
switching window (or tab)
<afile>
is set to the new directory name.
Non-recursive (event cannot trigger itself).
ExitPre
ExitPre When using :quit
, :wq
in a way it makes
Vim exit, or using :qall
, just after
QuitPre. Can be used to close any
non-essential window. Exiting may still be
cancelled if there is a modified buffer that
isn't automatically saved, use VimLeavePre
for really exiting.
See also QuitPre, WinClosed.
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 checkout the file from
a source control system. Not triggered when
the change was caused by an autocommand.
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
Cannot switch buffers. 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
Triggered for each changed file, after:
E246
E811
Cannot switch, jump to or delete buffers.
Non-recursive (event cannot trigger itself).
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>
is the name of the file where this
option was set. <amatch>
is the new value of
'filetype'.
Cannot switch windows or buffers.
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 with an external diff (see
DiffUpdated for internal 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 with an external 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 Nvim got focus.
FocusLost
FocusLost Nvim lost focus. Also (potentially) when
a GUI 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.
UIEnter
UIEnter After a UI connects via nvim_ui_attach(), or
after builtin TUI is started, after VimEnter.
Sets these v:event keys:
chan: channel-id of the UI
UILeave
UILeave After a UI disconnects from Nvim, or after
builtin TUI is stopped, after VimLeave.
Sets these v:event keys:
chan: channel-id of the UI
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.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.
InsertLeavePre
InsertLeavePre Just before leaving Insert mode. Also when
using CTRL-O
i_CTRL-O. Be careful not to
change mode or use :normal
, it will likely
cause trouble.
InsertLeave
InsertLeave Just after 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 one or two
characters representing the mode:
n Normal
v Visual
o Operator-pending
i Insert
c Command line
tl Terminal
ModeChanged
ModeChanged After changing the mode. The pattern is
matched against 'old_mode:new_mode'
, for
example match against *:c
to simulate
CmdlineEnter.
The following values of v:event are set:
old_mode The mode before it changed.
new_mode The new mode as also returned
by mode() called with a
non-zero argument.
When ModeChanged is triggered, old_mode will
have the value of new_mode when the event was
last triggered.
This will be triggered on every minor mode
change.
Usage example to use relative line numbers
when entering visual mode::au ModeChanged [vV\x16]*:* let &l:rnu = mode() =~# '^[vV\x16]' :au ModeChanged *:[vV\x16]* let &l:rnu = mode() =~# '^[vV\x16]' :au WinEnter,WinLeave * let &l:rnu = mode() =~# '^[vV\x16]'
OptionSet
OptionSet After setting an option (except during
startup). The autocmd-pattern is matched
against the long option name. <amatch>
indicates what option has been set.QuickFixCmdPre
QuickFixCmdPre Before a quickfix command is run (:make,
:lmake, :grep, :lgrep, :grepadd,
:lgrepadd, :vimgrep, :lvimgrep,
:vimgrepadd, :lvimgrepadd,
: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 the 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. For :wq
the buffer is written
before QuitPre is triggered. Can be used to
close any non-essential window if the current
window is the last ordinary window.
See also ExitPre, WinClosed.
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.
SearchWrapped
SearchWrapped After making a search with n or N if the
search wraps around the document back to
the start/finish respectively.
RecordingEnter
RecordingEnter When a macro starts recording.
The pattern is the current file name, and
reg_recording() is the current register that
is used.
RecordingLeave
RecordingLeave When a macro stops recording.
The pattern is the current file name, and
reg_recording() is the recorded
register.
reg_recorded() is only updated after this
event.
Sets these v:event keys:
regcontents
regname
SafeState
SafeState When nothing is pending, going to wait for the
user to type a character.
This will not be triggered when:
mode()
to find out what state
Vim is in. That may be:
state()
, e.g. whether the
screen was scrolled for messages.
SessionLoadPost
SessionLoadPost After loading the session file created using
the :mksession command.
SessionWritePost
SessionWritePost After writing a session file by calling
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.
Signal
Signal After Nvim receives a signal. The pattern is
matched against the signal name. Only
"SIGUSR1" and "SIGWINCH" are supported. Example:autocmd Signal SIGUSR1 call some#func()
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 Vimscript/Lua file. :source
<afile>
is the name of the file being sourced.
SourcePost
SourcePost After sourcing a Vimscript/Lua file. :source
<afile>
is the name of the file being sourced.
Not triggered when sourcing was interrupted.
Also triggered after a SourceCmd autocommand
was triggered.
SourceCmd
SourceCmd When sourcing a Vimscript/Lua file. :source
<afile>
is the name of the file being sourced.
The autocommand must source that 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 During startup, after reading from stdin into
the buffer, before executing modelines. --
StdinReadPre
StdinReadPre During startup, before reading from stdin into
the buffer. --
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
Cannot change to another buffer, change
the buffer name or change directory.
Syntax
Syntax When the 'syntax' option has been set. The
pattern is matched against the syntax name.
<afile>
expands to the name of the file where
this option was set. <amatch>
expands to the
new value of 'syntax'.
See :syn-on.
TabEnter
TabEnter Just after entering a tab page. tab-page
After WinEnter.
Before BufEnter.
TabLeave
TabLeave Just before leaving a tab page. tab-page
After WinLeave.
TabNew
TabNew When creating a new tab page. tab-page
After WinEnter.
Before TabEnter.
TabNewEntered
TabNewEntered After entering a new tab page. tab-page
After BufEnter.
TabClosed
TabClosed After closing a tab page. <afile>
expands to
the tab page number.
TermOpen
TermOpen When a terminal job is starting. Can be
used to configure the terminal buffer.
TermEnter
TermEnter After entering Terminal-mode.
After TermOpen.
TermLeave
TermLeave After leaving Terminal-mode.
After TermClose.
TermClose
TermClose When a terminal job ends.
Sets these v:event keys:
status
TermRequest
TermRequest When a :terminal child process emits an OSC
or DCS sequence. Sets v:termrequest. The
event-data is the request string.
TermResponse
TermResponse When Nvim receives an OSC or DCS response from
the host terminal. Sets v:termresponse. The
event-data is the response string. May be
triggered during another event (file I/O,
a shell command, or anything else that takes
time). Example:-- Query the terminal palette for the RGB value of color 1
-- (red) using OSC 4
vim.api.nvim_create_autocmd('TermResponse', {
once = true,
callback = function(args)
local resp = args.data
local r, g, b = resp:match("\027%]4;1;rgb:(%w+)/(%w+)/(%w+)")
end,
})
io.stdout:write("\027]4;1;?\027\\")
TextChanged
TextChanged After a change was made to the text in the
current buffer in Normal mode. That is after
b:changedtick has changed (also when that
happened before the TextChanged autocommand
was defined).
Not triggered when there is typeahead or when
an operator is pending.
Note: Cannot be skipped with :noautocmd
.
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.
TextChangedP
TextChangedP After a change was made to the text in the
current buffer in Insert mode, only when the
popup menu is visible. Otherwise the same as
TextChanged.
TextChangedT
TextChangedT After a change was made to the text in the
current buffer in Terminal-mode. Otherwise
the same as TextChanged.
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:
inclusive
operator
regcontents
regname
regtype
visual
The inclusive
flag combined with the '[
and '] marks can be used to calculate the
precise region of the operation.User
User Not executed automatically. Use :doautocmd
to trigger this, typically for "custom events"
in a plugin. Example::autocmd User MyPlugin echom 'got MyPlugin event' :doautocmd User MyPlugin
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()
VimResized
VimResized After the Vim window was resized, thus 'lines'
and/or 'columns' changed. Not when starting
up though.
VimResume
VimResume After Nvim resumes from suspend state.
VimSuspend
VimSuspend Before Nvim enters suspend state.
WinClosed
WinClosed When closing a window, just before it is
removed from the window layout. The pattern
is matched against the window-ID. Both
<amatch>
and <afile>
are set to the window-ID.
After WinLeave.
Non-recursive (event cannot trigger itself).
See also ExitPre, QuitPre.
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: For split and tabpage commands the
WinEnter event is triggered after the split
or tab command but before the file 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.
Before WinClosed.
WinNew
WinNew When a new window was created. Not done for
the first window, when Vim has just started.
Before WinEnter.WinScrolled
WinScrolled After any window in the current tab page
scrolled the text (horizontally or vertically)
or changed width or height. See
win-scrolled-resized.<amatch>
and <afile>
are set to the
window-ID.WinResized
WinResized After a window in the current tab page changed
width or height.
See win-scrolled-resized.{aupat}
argument of :autocmd
can be a comma-separated list. This works as
if the command was given with each pattern separately. Thus this command::autocmd BufRead *.txt,*.info set etIs equivalent to:
:autocmd BufRead *.txt set et :autocmd BufRead *.info set etThe file pattern
{aupat}
is tested for a match against the file name in one of
two ways:
1. When there is no '/' in the pattern, Vim checks for a match against only
the tail part of the file name (without its leading directory path).
2. When there is a '/' in the pattern, Vim checks for a match against both the
short file name (as you typed it) and the full file name (after expanding
it to a full path and resolving symbolic links).<buffer>
or <buffer=N> is used for buffer-local
autocommands autocmd-buflocal. This pattern is not matched against the name
of a buffer.:autocmd BufRead *.txt set etSet the 'et' option for all text files.
:autocmd BufRead /vim/src/*.c set cindentSet the 'cindent' option for C files in the /vim/src directory.
:autocmd BufRead /tmp/*.c set ts=5If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and you start editing "/tmp/test.c", this autocommand will match.
:autocmd BufRead */doc/*.txt set tw=78This autocommand will for example be executed for "/tmp/doc/xx.txt" and "/usr/home/piet/doc/yy.txt". The number of directories does not matter here.
:e $ROOTDIR/main.$EXTThe argument is first expanded to:
/usr/root/main.pyBefore it's matched with the pattern of the autocommand. Careful with this when using events like FileReadCmd, the value of
<amatch>
may not be what you
expect.:autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtabAnd ~ can be used for the home directory (if $HOME is defined):
:autocmd BufWritePost ~/.config/nvim/init.vim so <afile> :autocmd BufRead ~archive/* set readonlyThe environment variable is expanded when the autocommand is defined, not when the autocommand is executed. This is different from the command!
file-pattern
The pattern is interpreted like mostly used in file names:
* matches any sequence of characters; Unusual: includes path
separators
? matches any single character
\? matches a '?'
. matches a '.'
~ matches a '~'
, separates patterns
\, matches a ','
{ } like \( \) in a pattern
, inside { }: like \| in a pattern
\} literal }
\{ literal {
\\\{n,m\} like \{n,m} in a pattern
\ special meaning like in a pattern
[ch] matches 'c' or 'h'
[^ch] match any character but 'c' and 'h'autocmd-changes
Matching with the pattern is done when an event is triggered. Changing the
buffer name in one of the autocommands, or even deleting the buffer, does not
change which autocommands will be executed. Example:au BufEnter *.foo bdel au BufEnter *.foo set modifiedThis will delete the current buffer and then set 'modified' in what has become the current buffer instead. Vim doesn't take into account that "*.foo" doesn't match with that buffer name. It matches "*.foo" with the name of the buffer at the moment the event was triggered.
<buffer>
current buffer
<buffer=99> buffer number 99
<buffer=abuf> using <abuf>
(only when executing autocommands)
<abuf>:au CursorHold <buffer> echo 'hold' :au CursorHold <buffer=33> echo 'hold' :au BufNewFile * au CursorHold <buffer=abuf> echo 'hold'All the commands for autocommands also work with buffer-local autocommands, simply use the special string instead of the pattern. Examples:
:au! * <buffer> " remove buffer-local autocommands for " current buffer :au! * <buffer=33> " remove buffer-local autocommands for " buffer #33 :bufdo :au! CursorHold <buffer> " remove autocmd for given event for all " buffers :au * <buffer> " list buffer-local autocommands for " current bufferNote that when an autocommand is defined for the current buffer, it is stored with the buffer number. Thus it uses the form "<buffer=12>", where 12 is the number of the current buffer. You will see this when listing autocommands, for example.
:if exists("#CursorHold#<buffer=12>") | ... | endif :if exists("#CursorHold#<buffer>") | ... | endif " for current bufferWhen a buffer is wiped out its buffer-local autocommands are also gone, of course. Note that when deleting a buffer, e.g., with ":bdel", it is only unlisted, the autocommands are still present. In order to see the removal of buffer-local autocommands:
:set verbose=6It is not possible to define buffer-local autocommands for a non-existent buffer.
:aug
:augroup
:aug[roup] {name}
Define the autocmd group name for the
following ":autocmd" commands. The name "end"
or "END" selects the default group.
To avoid confusion, the name should be
different from existing {event}
names, as this
most likely will not do what you intended.:augroup-delete
E367
W19
E936
:aug[roup]! {name}
Delete the autocmd group {name}
. Don't use
this if there is still an autocommand using
this group! You will get a warning if doing
it anyway. When the group is the current
group you will get error E936.{name}
".
2. Delete any old autocommands with ":au!".
3. Define the autocommands.
4. Go back to the default group with "augroup END".:augroup uncompress : au! : au BufEnter *.gz %!gunzip :augroup ENDThis prevents having the autocommands defined twice (e.g., after sourcing the vimrc file again).
FileExplorer
There is one group that is recognized by Vim: FileExplorer. If this group
exists Vim assumes that editing a directory is possible and will trigger a
plugin that lists the files in that directory. This is used by the netrw
plugin. This allows you to do:browse edit
:do
:doau
:doaut
:doautocmd
E217
:do[autocmd] [<nomodeline>
] [group] {event}
[fname]
Apply the autocommands matching [fname] (default:
current file name) for {event}
to the current buffer.
You can use this when the current file name does not
match the right pattern, after changing settings, or
to execute autocommands for a certain event.
It's possible to use this inside an autocommand too,
so you can base the autocommands for one extension on
another extension. Example::au BufEnter *.cpp so ~/.config/nvim/init_cpp.vim :au BufEnter *.cpp doau BufEnter x.c
<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.augroup mine autocmd! autocmd FileType * echo expand('<amatch>') augroup END doautoall mine FileType Loaded-Buffer
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 ENDThe "gzip" group is used to be able to delete any existing autocommands with ":autocmd!", for when the file is sourced twice.
autocmd-nested
E218
By default, autocommands do not nest. For example, 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.
<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 FORFor 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.
skeleton
template
To read a skeleton (template) file when opening a new file::autocmd BufNewFile *.c 0r ~/vim/skeleton.c :autocmd BufNewFile *.h 0r ~/vim/skeleton.h :autocmd BufNewFile *.java 0r ~/vim/skeleton.javaTo insert the current date and time in a "*.html" file when writing it:
:autocmd BufWritePre,FileWritePre *.html ks|call LastMod()|'s :fun LastMod() : if line("$") > 20 : let l = 20 : else : let l = line("$") : endif : exe "1," .. l .. "g/Last modified: /s/Last modified: .*/Last modified: " .. : \ strftime("%Y %b %d") :endfunYou need to have a line "Last modified: <date time>" in the first 20 lines of the file for this to work. Vim replaces <date time> (and anything in the same line after it) with the current date and time. Explanation: ks mark current position with mark 's' call LastMod() call the LastMod() function to do the work 's return the cursor to the old position The LastMod() function checks if the file is shorter than 20 lines, and then uses the ":g" command to find lines that contain "Last modified: ". For those lines the ":s" command is executed to replace the existing date with the current one. The ":execute" command is used to be able to use an expression for the ":g" and ":s" commands. The date is obtained with the strftime() function. You can change its argument to get another date string.
<Tab>
, CTRL-D
, etc.) where appropriate.autocmd-searchpat
Autocommands do not change the current search patterns. Vim saves the current
search patterns before executing autocommands then restores them after the
autocommands finish. This means that autocommands do not affect the strings
highlighted with the 'hlsearch' option. Within autocommands, you can still
use search patterns normally, e.g., with the "n" command.
If you want an autocommand to set the search pattern, such that it is used
after the autocommand finishes, use the ":let @/ =" command.
The search-highlighting cannot be switched off with ":nohlsearch" in an
autocommand. Use the 'h' flag in the 'shada' option to disable search-
highlighting when starting Vim.Cmd-event
When using one of the "*Cmd" events, the matching autocommands are expected to
do the file reading, writing or sourcing. This can be used when working with
a special kind of file, for example on a remote system.
CAREFUL: If you use these events in a wrong way, it may have the effect of
making it impossible to read or write the matching files! Make sure you test
your autocommands properly. Best is to use a pattern that will never match a
normal file name, for example "ftp://*".:noautocmd
:noa
To disable autocommands for just one command use the ":noautocmd" command
modifier. This will set 'eventignore' to "all" for the duration of the
following command. Example::noautocmd w fname.gzThis will write the file without triggering the autocommands defined by the gzip plugin.