2017-03-21 09:08:19 -07:00
|
|
|
*autocmd.txt* Nvim
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
VIM REFERENCE MANUAL by Bram Moolenaar
|
|
|
|
|
|
|
|
|
2018-10-29 01:40:20 -07:00
|
|
|
Automatic commands *autocmd* *autocommand*
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
For a basic explanation, see section |40.3| in the user manual.
|
|
|
|
|
2017-10-20 17:33:58 -07:00
|
|
|
Type |gO| to see the table of contents.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
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
|
2023-06-21 06:40:40 -07:00
|
|
|
files matching `*.c`. You can also use autocommands to implement advanced
|
2014-07-10 21:05:51 -07:00
|
|
|
features, such as editing compressed files (see |gzip-example|). The usual
|
2015-10-17 07:25:53 -07:00
|
|
|
place to put autocommands is in your vimrc file.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2018-10-28 06:05:39 -07:00
|
|
|
*E203* *E204* *E143* *E855* *E937* *E952*
|
2014-07-10 21:05:51 -07:00
|
|
|
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*
|
2022-01-17 03:58:36 -07:00
|
|
|
:au[tocmd] [group] {event} {aupat} [++once] [++nested] {cmd}
|
2014-07-10 21:05:51 -07:00
|
|
|
Add {cmd} to the list of commands that Vim will
|
|
|
|
execute automatically on {event} for a file matching
|
2022-01-17 03:58:36 -07:00
|
|
|
{aupat} |autocmd-pattern|.
|
2018-10-28 22:39:10 -07:00
|
|
|
Note: A quote character is seen as argument to the
|
|
|
|
:autocmd and won't start a comment.
|
2019-03-09 20:32:58 -07:00
|
|
|
Nvim always adds {cmd} after existing autocommands so
|
|
|
|
they execute in the order in which they were defined.
|
2019-03-13 14:13:25 -07:00
|
|
|
See |autocmd-nested| for [++nested].
|
2019-03-09 20:32:58 -07:00
|
|
|
*autocmd-once*
|
2019-03-13 14:13:25 -07:00
|
|
|
If [++once] is supplied the command is executed once,
|
2019-03-09 20:32:58 -07:00
|
|
|
then removed ("one shot").
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
The special pattern <buffer> or <buffer=N> defines a buffer-local autocommand.
|
|
|
|
See |autocmd-buflocal|.
|
|
|
|
|
2017-01-04 21:06:09 -07:00
|
|
|
Note: The ":autocmd" command can only be followed by another command when the
|
2022-03-27 19:47:34 -07:00
|
|
|
"|" appears where the pattern is expected. This works: >
|
2017-01-04 21:06:09 -07:00
|
|
|
:augroup mine | au! BufRead | augroup END
|
|
|
|
But this sees "augroup" as part of the defined command: >
|
2017-11-07 12:51:49 -07:00
|
|
|
:augroup mine | au! BufRead * | augroup END
|
2017-01-04 21:06:09 -07:00
|
|
|
:augroup mine | au BufRead * set tw=70 | augroup END
|
2017-11-07 12:51:49 -07:00
|
|
|
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
|
2017-01-04 21:06:09 -07:00
|
|
|
|
2022-04-19 06:14:17 -07:00
|
|
|
< *autocmd-expand*
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
|
|
|
|
2017-04-28 14:40:49 -07:00
|
|
|
`: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: >
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2017-04-28 14:40:49 -07:00
|
|
|
augroup vimrc
|
2018-10-28 22:39:10 -07:00
|
|
|
" Remove all vimrc autocommands
|
|
|
|
autocmd!
|
2017-04-28 14:40:49 -07:00
|
|
|
au BufNewFile,BufRead *.html so <sfile>:h/html.vim
|
|
|
|
augroup END
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
==============================================================================
|
2023-10-04 06:31:25 -07:00
|
|
|
3. Removing autocommands *autocmd!* *autocmd-remove*
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2022-01-17 03:58:36 -07:00
|
|
|
:au[tocmd]! [group] {event} {aupat} [++once] [++nested] {cmd}
|
2014-07-10 21:05:51 -07:00
|
|
|
Remove all autocommands associated with {event} and
|
2022-01-17 03:58:36 -07:00
|
|
|
{aupat}, and add the command {cmd}.
|
2019-03-13 14:13:25 -07:00
|
|
|
See |autocmd-once| for [++once].
|
|
|
|
See |autocmd-nested| for [++nested].
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2022-01-17 03:58:36 -07:00
|
|
|
:au[tocmd]! [group] {event} {aupat}
|
2014-07-10 21:05:51 -07:00
|
|
|
Remove all autocommands associated with {event} and
|
2022-01-17 03:58:36 -07:00
|
|
|
{aupat}.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2022-01-17 03:58:36 -07:00
|
|
|
:au[tocmd]! [group] * {aupat}
|
|
|
|
Remove all autocommands associated with {aupat} for
|
|
|
|
all events.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
:au[tocmd]! [group] {event}
|
|
|
|
Remove ALL autocommands for {event}.
|
2017-04-28 14:40:49 -07:00
|
|
|
Warning: You should not do this without a group for
|
|
|
|
|BufRead| and other common events, it can break
|
|
|
|
plugins, syntax highlighting, etc.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
:au[tocmd]! [group] Remove ALL autocommands.
|
2018-10-28 22:39:10 -07:00
|
|
|
Note: a quote will be seen as argument to the :autocmd
|
|
|
|
and won't start a comment.
|
2017-04-28 14:40:49 -07:00
|
|
|
Warning: You should normally not do this without a
|
|
|
|
group, it breaks plugins, syntax highlighting, etc.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
When the [group] argument is not given, Vim uses the current group (as defined
|
|
|
|
with ":augroup"); otherwise, Vim uses the group defined with [group].
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
4. Listing autocommands *autocmd-list*
|
|
|
|
|
2022-01-17 03:58:36 -07:00
|
|
|
:au[tocmd] [group] {event} {aupat}
|
2014-07-10 21:05:51 -07:00
|
|
|
Show the autocommands associated with {event} and
|
2022-01-17 03:58:36 -07:00
|
|
|
{aupat}.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2022-01-17 03:58:36 -07:00
|
|
|
:au[tocmd] [group] * {aupat}
|
|
|
|
Show the autocommands associated with {aupat} for all
|
2014-07-10 21:05:51 -07:00
|
|
|
events.
|
|
|
|
|
|
|
|
:au[tocmd] [group] {event}
|
|
|
|
Show all autocommands for {event}.
|
|
|
|
|
|
|
|
:au[tocmd] [group] Show all autocommands.
|
|
|
|
|
|
|
|
If you provide the [group] argument, Vim lists only the autocommands for
|
|
|
|
[group]; otherwise, Vim lists the autocommands for ALL groups. Note that this
|
|
|
|
argument behavior differs from that for defining and removing autocommands.
|
|
|
|
|
|
|
|
In order to list buffer-local autocommands, use a pattern in the form <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
|
|
|
|
<
|
|
|
|
See |:verbose-cmd| for more information.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
5. Events *autocmd-events* *E215* *E216*
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2023-06-21 06:40:40 -07:00
|
|
|
Note that the autocommands for the "*ReadPre" events and all the Filter events
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
2020-08-31 00:51:35 -07:00
|
|
|
|
|
|
|
*events* *{event}*
|
|
|
|
Nvim recognizes the following events. Names are case-insensitive.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2020-01-13 00:41:55 -07:00
|
|
|
*BufAdd*
|
2023-06-23 03:16:55 -07:00
|
|
|
BufAdd After adding a new buffer or existing unlisted
|
|
|
|
buffer to the buffer list (except during
|
|
|
|
startup, see |VimEnter|), or renaming a listed
|
|
|
|
buffer.
|
2020-01-13 00:41:55 -07:00
|
|
|
Before |BufEnter|.
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>", "<abuf>". |<buffer=abuf>|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>", "<abuf>". |<buffer=abuf>|
|
2020-01-13 00:41:55 -07:00
|
|
|
Do not change to another buffer.
|
2014-07-10 21:05:51 -07:00
|
|
|
*BufEnter*
|
2023-06-23 03:16:55 -07:00
|
|
|
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.
|
2020-01-13 00:41:55 -07:00
|
|
|
After |BufAdd|.
|
|
|
|
After |BufReadPost|.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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*
|
2020-01-18 20:38:48 -07:00
|
|
|
BufHidden Before a buffer becomes hidden: 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.
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>", "<abuf>". |<buffer=abuf>|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2020-01-18 20:38:48 -07:00
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
Not used for ":qa" or ":q" when exiting Vim.
|
2020-11-07 21:22:54 -07:00
|
|
|
*BufModifiedSet*
|
|
|
|
BufModifiedSet After the `'modified'` value of a buffer has
|
|
|
|
been changed.
|
2014-07-10 21:05:51 -07:00
|
|
|
*BufNew*
|
2023-06-23 03:16:55 -07:00
|
|
|
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|.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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
|
2020-01-18 20:38:48 -07:00
|
|
|
processing modelines. See |BufWinEnter| to do
|
|
|
|
something after processing modelines.
|
|
|
|
Also triggered:
|
2022-04-19 06:14:17 -07:00
|
|
|
- when writing an unnamed buffer in a way that
|
|
|
|
the buffer gets a name
|
2020-01-18 20:38:48 -07:00
|
|
|
- after successfully recovering a file
|
|
|
|
- for the "filetypedetect" group when
|
|
|
|
executing ":filetype detect"
|
|
|
|
Not triggered:
|
2022-04-19 06:14:17 -07:00
|
|
|
- for the `:read file` command
|
|
|
|
- when the file doesn't exist
|
2014-07-10 21:05:51 -07:00
|
|
|
*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*
|
2023-07-25 05:07:13 -07:00
|
|
|
BufUnload Before unloading a buffer, when the text in
|
2020-01-18 20:38:48 -07:00
|
|
|
the buffer is going to be freed.
|
|
|
|
After BufWritePost.
|
|
|
|
Before BufDelete.
|
|
|
|
Triggers for all loaded buffers when Vim is
|
|
|
|
going to exit.
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>", "<abuf>". |<buffer=abuf>|
|
2020-01-18 20:38:48 -07:00
|
|
|
Do not switch buffers or windows!
|
2020-01-13 00:41:55 -07:00
|
|
|
Not triggered when exiting and v:dying is 2 or
|
|
|
|
more.
|
2014-07-10 21:05:51 -07:00
|
|
|
*BufWinEnter*
|
|
|
|
BufWinEnter After a buffer is displayed in a window. This
|
2020-01-18 20:38:48 -07:00
|
|
|
may be when the buffer is loaded (after
|
|
|
|
processing modelines) or when a hidden buffer
|
|
|
|
is displayed (and is no longer hidden).
|
|
|
|
|
|
|
|
Not triggered for |:split| without arguments,
|
|
|
|
since the buffer does not change, or :split
|
|
|
|
with a file already open in a window.
|
|
|
|
Triggered for ":split" with the name of the
|
|
|
|
current buffer, since it reloads that buffer.
|
2014-07-10 21:05:51 -07:00
|
|
|
*BufWinLeave*
|
|
|
|
BufWinLeave Before a buffer is removed from a window.
|
|
|
|
Not when it's still visible in another window.
|
2020-01-18 20:38:48 -07:00
|
|
|
Also triggered when exiting.
|
|
|
|
Before BufUnload, BufHidden.
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>", "<abuf>". |<buffer=abuf>|
|
2020-01-13 00:41:55 -07:00
|
|
|
Not triggered when exiting and v:dying is 2 or
|
|
|
|
more.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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).
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>", "<abuf>". |<buffer=abuf>|
|
2020-01-13 00:41:55 -07:00
|
|
|
Do not change to another buffer.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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).
|
2017-05-13 22:43:07 -07:00
|
|
|
*ChanInfo*
|
|
|
|
ChanInfo State of channel changed, for instance the
|
|
|
|
client of a RPC channel described itself.
|
|
|
|
Sets these |v:event| keys:
|
|
|
|
info
|
2018-08-25 07:38:24 -07:00
|
|
|
See |nvim_get_chan_info()| for the format of
|
|
|
|
the info Dictionary.
|
2017-05-13 22:43:07 -07:00
|
|
|
*ChanOpen*
|
|
|
|
ChanOpen Just after a channel was opened.
|
|
|
|
Sets these |v:event| keys:
|
|
|
|
info
|
2018-08-25 07:38:24 -07:00
|
|
|
See |nvim_get_chan_info()| for the format of
|
|
|
|
the info Dictionary.
|
2014-10-10 18:51:25 -07:00
|
|
|
*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
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expand to the command name.
|
2014-10-10 18:51:25 -07:00
|
|
|
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|.
|
2018-01-31 08:17:05 -07:00
|
|
|
*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.
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expands to the |cmdline-char|.
|
2017-11-22 14:35:20 -07:00
|
|
|
*CmdlineEnter*
|
2018-11-15 18:00:04 -07:00
|
|
|
CmdlineEnter After entering the command-line (including
|
|
|
|
non-interactive use of ":" in a mapping: use
|
|
|
|
|<Cmd>| instead to avoid this).
|
2023-02-21 08:50:29 -07:00
|
|
|
The pattern is matched against |cmdline-char|.
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expands to the |cmdline-char|.
|
2017-11-22 14:35:20 -07:00
|
|
|
Sets these |v:event| keys:
|
|
|
|
cmdlevel
|
|
|
|
cmdtype
|
|
|
|
*CmdlineLeave*
|
2018-11-15 18:00:04 -07:00
|
|
|
CmdlineLeave Before leaving the command-line (including
|
|
|
|
non-interactive use of ":" in a mapping: use
|
|
|
|
|<Cmd>| instead to avoid this).
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expands to the |cmdline-char|.
|
2017-11-22 14:35:20 -07:00
|
|
|
Sets these |v:event| keys:
|
|
|
|
abort (mutable)
|
|
|
|
cmdlevel
|
|
|
|
cmdtype
|
|
|
|
Note: `abort` can only be changed from false
|
2020-01-18 20:38:48 -07:00
|
|
|
to true: cannot execute an already aborted
|
|
|
|
cmdline by changing it to false.
|
2014-07-10 21:05:51 -07:00
|
|
|
*CmdwinEnter*
|
|
|
|
CmdwinEnter After entering the command-line window.
|
|
|
|
Useful for setting options specifically for
|
2019-11-29 10:51:25 -07:00
|
|
|
this special type of window.
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expands to a single character,
|
2014-07-10 21:05:51 -07:00
|
|
|
indicating the type of command-line.
|
|
|
|
|cmdwin-char|
|
|
|
|
*CmdwinLeave*
|
|
|
|
CmdwinLeave Before leaving the command-line window.
|
|
|
|
Useful to clean up any global setting done
|
2019-11-29 10:51:25 -07:00
|
|
|
with CmdwinEnter.
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expands to a single character,
|
2014-07-10 21:05:51 -07:00
|
|
|
indicating the type of command-line.
|
|
|
|
|cmdwin-char|
|
|
|
|
*ColorScheme*
|
|
|
|
ColorScheme After loading a color scheme. |:colorscheme|
|
2022-09-05 23:57:53 -07:00
|
|
|
Not triggered if the color scheme is not
|
|
|
|
found.
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
|
|
|
|
2018-10-29 00:31:05 -07:00
|
|
|
*ColorSchemePre*
|
|
|
|
ColorSchemePre Before loading a color scheme. |:colorscheme|
|
|
|
|
Useful to setup removing things added by a
|
|
|
|
color scheme, before another one is loaded.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2023-02-21 09:07:26 -07:00
|
|
|
CompleteChanged *CompleteChanged*
|
2019-07-28 17:36:46 -07:00
|
|
|
After each time the Insert mode completion
|
|
|
|
menu changed. Not fired on popup menu hide,
|
2020-01-26 19:34:32 -07:00
|
|
|
use |CompleteDonePre| or |CompleteDone| for
|
|
|
|
that.
|
2019-03-30 17:15:41 -07:00
|
|
|
|
|
|
|
Sets these |v:event| keys:
|
2019-08-01 13:59:21 -07:00
|
|
|
completed_item See |complete-items|.
|
2019-07-28 17:36:46 -07:00
|
|
|
height nr of items visible
|
|
|
|
width screen cells
|
|
|
|
row top screen row
|
|
|
|
col leftmost screen column
|
|
|
|
size total nr of items
|
|
|
|
scrollbar TRUE if visible
|
2019-03-30 17:15:41 -07:00
|
|
|
|
2020-01-13 01:19:20 -07:00
|
|
|
Non-recursive (event cannot trigger itself).
|
|
|
|
Cannot change the text. |textlock|
|
2019-03-30 17:15:41 -07:00
|
|
|
|
2022-08-09 21:47:38 -07:00
|
|
|
The size and position of the popup are also
|
|
|
|
available by calling |pum_getpos()|.
|
2019-12-16 12:08:55 -07:00
|
|
|
|
2020-01-26 19:34:32 -07:00
|
|
|
*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.
|
|
|
|
|
2019-12-28 22:44:11 -07:00
|
|
|
*CompleteDone*
|
|
|
|
CompleteDone After Insert mode completion is done. Either
|
|
|
|
when something was completed or abandoning
|
|
|
|
completion. |ins-completion|
|
2020-01-26 19:34:32 -07:00
|
|
|
|complete_info()| cannot be used, the info is
|
|
|
|
cleared before triggering CompleteDone. Use
|
|
|
|
CompleteDonePre if you need it.
|
2020-01-18 20:38:48 -07:00
|
|
|
|v:completed_item| gives the completed item.
|
2019-12-28 22:44:11 -07:00
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
*CursorHold*
|
|
|
|
CursorHold When the user doesn't press a key for the time
|
2021-04-30 22:39:00 -07:00
|
|
|
specified with 'updatetime'. Not triggered
|
2014-07-10 21:05:51 -07:00
|
|
|
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|
|
2015-01-20 07:17:50 -07:00
|
|
|
*<CursorHold>*
|
|
|
|
Internally the autocommand is triggered by the
|
|
|
|
<CursorHold> key. In an expression mapping
|
|
|
|
|getchar()| may see this character.
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
Note: Interactive commands cannot be used for
|
|
|
|
this event. There is no hit-enter prompt,
|
|
|
|
the screen is updated directly (when needed).
|
|
|
|
Note: In the future there will probably be
|
|
|
|
another option to set the time.
|
|
|
|
Hint: to force an update of the status lines
|
|
|
|
use: >
|
|
|
|
:let &ro = &ro
|
2020-08-31 00:51:35 -07:00
|
|
|
<
|
2014-07-10 21:05:51 -07:00
|
|
|
*CursorHoldI*
|
2020-01-18 20:38:48 -07:00
|
|
|
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
|
2017-01-01 18:48:26 -07:00
|
|
|
|insert_expand|.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
*CursorMoved*
|
|
|
|
CursorMoved After the cursor was moved in Normal or Visual
|
2019-03-27 03:11:52 -07:00
|
|
|
mode or to another window. Also when the text
|
|
|
|
of the cursor line has been changed, e.g. with
|
|
|
|
"x", "rx" or "p".
|
2022-03-30 04:12:12 -07:00
|
|
|
Not always triggered when there is typeahead,
|
|
|
|
while executing commands in a script file, or
|
2023-06-03 18:09:22 -07:00
|
|
|
when an operator is pending.
|
2014-07-10 21:05:51 -07:00
|
|
|
For an example see |match-parens|.
|
2020-01-18 20:38:48 -07:00
|
|
|
Note: Cannot be skipped with |:noautocmd|.
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
2018-12-09 10:26:12 -07:00
|
|
|
*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|.
|
2017-01-13 03:03:43 -07:00
|
|
|
*DirChanged*
|
2017-01-17 02:47:20 -07:00
|
|
|
DirChanged After the |current-directory| was changed.
|
2021-10-17 07:04:53 -07:00
|
|
|
The pattern can be:
|
2021-10-17 07:04:53 -07:00
|
|
|
"window" to trigger on `:lcd`
|
|
|
|
"tabpage" to trigger on `:tcd`
|
|
|
|
"global" to trigger on `:cd`
|
|
|
|
"auto" to trigger on 'autochdir'.
|
2017-01-17 02:47:20 -07:00
|
|
|
Sets these |v:event| keys:
|
2022-02-10 21:44:47 -07:00
|
|
|
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"
|
2020-11-07 10:02:06 -07:00
|
|
|
changed_window: v:true if we fired the event
|
|
|
|
switching window (or tab)
|
2021-10-17 07:04:53 -07:00
|
|
|
<afile> is set to the new directory name.
|
2020-01-13 01:19:20 -07:00
|
|
|
Non-recursive (event cannot trigger itself).
|
2022-04-19 06:14:17 -07:00
|
|
|
*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|.
|
2014-07-10 21:05:51 -07:00
|
|
|
*FileAppendCmd*
|
|
|
|
FileAppendCmd Before appending to a file. Should do the
|
|
|
|
appending to the file. Use the '[ and ']
|
2020-01-18 20:38:48 -07:00
|
|
|
marks for the range of lines. |Cmd-event|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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
|
2020-01-18 20:38:48 -07:00
|
|
|
file. Can be used to checkout the file from
|
2014-07-10 21:05:51 -07:00
|
|
|
a source control system. Not triggered when
|
|
|
|
the change was caused by an autocommand.
|
2020-01-18 20:38:48 -07:00
|
|
|
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.
|
2014-07-10 21:05:51 -07:00
|
|
|
WARNING: If the autocommand moves the cursor
|
|
|
|
the effect of the change is undefined.
|
|
|
|
*E788*
|
2020-01-18 20:38:48 -07:00
|
|
|
Cannot switch buffers. You can reload the
|
|
|
|
buffer but not edit another one.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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|
|
2020-01-18 20:38:48 -07:00
|
|
|
Triggered for each changed file, after:
|
|
|
|
- executing a shell command
|
|
|
|
- |:checktime|
|
2023-07-25 05:07:13 -07:00
|
|
|
- |FocusGained|
|
2022-10-06 06:16:00 -07:00
|
|
|
|
2020-01-18 20:38:48 -07:00
|
|
|
Not used when 'autoread' is set and the buffer
|
|
|
|
was not changed. If a FileChangedShell
|
|
|
|
autocommand exists the warning message and
|
|
|
|
prompt is not given.
|
|
|
|
|v:fcs_reason| indicates what happened. Set
|
|
|
|
|v:fcs_choice| to control what happens next.
|
2023-06-23 03:16:55 -07:00
|
|
|
NOTE: Current buffer "%" is not the target
|
|
|
|
buffer "<afile>" and "<abuf>". |<buffer=abuf>|
|
2020-01-18 20:38:48 -07:00
|
|
|
*E246* *E811*
|
|
|
|
Cannot switch, jump to or delete buffers.
|
|
|
|
Non-recursive (event cannot trigger itself).
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> is the name of the file where this
|
|
|
|
option was set. <amatch> is the new value of
|
|
|
|
'filetype'.
|
|
|
|
Cannot switch windows or buffers.
|
2014-07-10 21:05:51 -07:00
|
|
|
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
|
2019-07-28 17:59:32 -07:00
|
|
|
making a diff with an external diff (see
|
2021-10-16 08:07:40 -07:00
|
|
|
|DiffUpdated| for internal diff).
|
2014-07-10 21:05:51 -07:00
|
|
|
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
|
2019-07-28 17:59:32 -07:00
|
|
|
making a diff with an external diff.
|
2014-07-10 21:05:51 -07:00
|
|
|
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*
|
2018-06-27 15:48:17 -07:00
|
|
|
FocusGained Nvim got focus.
|
2014-07-10 21:05:51 -07:00
|
|
|
*FocusLost*
|
2018-06-27 15:48:17 -07:00
|
|
|
FocusLost Nvim lost focus. Also (potentially) when
|
|
|
|
a GUI dialog pops up.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2014-10-10 18:51:25 -07:00
|
|
|
NOTE: When writing Vim scripts a better
|
|
|
|
alternative is to use an autoloaded function.
|
2014-07-10 21:05:51 -07:00
|
|
|
See |autoload-functions|.
|
2019-09-12 16:08:22 -07:00
|
|
|
*UIEnter*
|
2021-12-27 02:31:32 -07:00
|
|
|
UIEnter After a UI connects via |nvim_ui_attach()|, or
|
|
|
|
after builtin TUI is started, after |VimEnter|.
|
2019-09-11 18:44:20 -07:00
|
|
|
Sets these |v:event| keys:
|
2023-01-14 14:24:19 -07:00
|
|
|
chan: |channel-id| of the UI
|
2019-09-12 16:08:22 -07:00
|
|
|
*UILeave*
|
2021-12-27 02:31:32 -07:00
|
|
|
UILeave After a UI disconnects from Nvim, or after
|
|
|
|
builtin TUI is stopped, after |VimLeave|.
|
2019-09-11 18:44:20 -07:00
|
|
|
Sets these |v:event| keys:
|
2023-01-14 14:24:19 -07:00
|
|
|
chan: |channel-id| of the UI
|
2017-06-21 08:59:52 -07:00
|
|
|
*InsertChange*
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
2019-03-12 19:24:09 -07:00
|
|
|
|
|
|
|
Cannot change the text. |textlock|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2020-10-21 20:25:21 -07:00
|
|
|
*InsertLeavePre*
|
|
|
|
InsertLeavePre Just before leaving Insert mode. Also when
|
2021-05-01 11:23:09 -07:00
|
|
|
using CTRL-O |i_CTRL-O|. Be careful not to
|
2020-10-21 20:25:21 -07:00
|
|
|
change mode or use `:normal`, it will likely
|
|
|
|
cause trouble.
|
2014-07-10 21:05:51 -07:00
|
|
|
*InsertLeave*
|
2020-10-21 20:25:21 -07:00
|
|
|
InsertLeave Just after leaving Insert mode. Also when
|
|
|
|
using CTRL-O |i_CTRL-O|. But not for |i_CTRL-C|.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2022-06-30 02:17:27 -07:00
|
|
|
The pattern is matched against one or two
|
|
|
|
characters representing the mode:
|
2014-07-10 21:05:51 -07:00
|
|
|
n Normal
|
|
|
|
v Visual
|
|
|
|
o Operator-pending
|
|
|
|
i Insert
|
|
|
|
c Command line
|
2022-06-30 02:17:27 -07:00
|
|
|
tl Terminal
|
2021-09-29 07:36:48 -07:00
|
|
|
*ModeChanged*
|
|
|
|
ModeChanged After changing the mode. The pattern is
|
|
|
|
matched against `'old_mode:new_mode'`, for
|
2021-10-22 11:36:35 -07:00
|
|
|
example match against `*:c` to simulate
|
|
|
|
|CmdlineEnter|.
|
2021-09-29 07:36:48 -07:00
|
|
|
The following values of |v:event| are set:
|
|
|
|
old_mode The mode before it changed.
|
|
|
|
new_mode The new mode as also returned
|
2021-10-22 11:36:35 -07:00
|
|
|
by |mode()| called with a
|
|
|
|
non-zero argument.
|
2021-09-29 07:36:48 -07:00
|
|
|
When ModeChanged is triggered, old_mode will
|
|
|
|
have the value of new_mode when the event was
|
|
|
|
last triggered.
|
2021-10-22 11:36:35 -07:00
|
|
|
This will be triggered on every minor mode
|
|
|
|
change.
|
2021-09-29 07:36:48 -07:00
|
|
|
Usage example to use relative line numbers
|
|
|
|
when entering visual mode: >
|
2021-10-22 11:36:35 -07:00
|
|
|
: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]'
|
2021-09-29 07:36:48 -07:00
|
|
|
< *OptionSet*
|
2019-02-04 05:21:35 -07:00
|
|
|
OptionSet After setting an option (except during
|
|
|
|
|startup|). The |autocmd-pattern| is matched
|
2019-08-01 21:56:33 -07:00
|
|
|
against the long option name. |<amatch>|
|
|
|
|
indicates what option has been set.
|
|
|
|
|
2019-02-04 05:21:35 -07:00
|
|
|
|v:option_type| indicates whether it's global
|
2019-08-01 21:56:33 -07:00
|
|
|
or local scoped.
|
|
|
|
|v:option_command| indicates what type of
|
|
|
|
set/let command was used (follow the tag to
|
|
|
|
see the table).
|
|
|
|
|v:option_new| indicates the newly set value.
|
|
|
|
|v:option_oldlocal| has the old local value.
|
|
|
|
|v:option_oldglobal| has the old global value.
|
|
|
|
|v:option_old| indicates the old option value.
|
|
|
|
|
|
|
|
|v:option_oldlocal| is only set when |:set|
|
|
|
|
or |:setlocal| or a |modeline| was used to set
|
|
|
|
the option. Similarly |v:option_oldglobal| is
|
|
|
|
only set when |:set| or |:setglobal| was used.
|
|
|
|
|
2023-06-11 04:40:22 -07:00
|
|
|
This does not set |<abuf>|, you could use
|
|
|
|
|bufnr()|.
|
|
|
|
|
2023-10-14 09:19:11 -07:00
|
|
|
Note that when setting a |global-local| option
|
|
|
|
with |:set|, then |v:option_old| is the old
|
|
|
|
global value. However, for all options that
|
|
|
|
are not global-local it is the old local
|
2019-08-01 21:56:33 -07:00
|
|
|
value.
|
|
|
|
|
2016-04-18 09:37:38 -07:00
|
|
|
Usage example: Check for the existence of the
|
|
|
|
directory in the 'backupdir' and 'undodir'
|
|
|
|
options, create the directory if it doesn't
|
|
|
|
exist yet.
|
|
|
|
|
2019-02-04 05:21:35 -07:00
|
|
|
Note: Do not reset the same option during this
|
|
|
|
autocommand, that may break plugins. You can
|
|
|
|
always use |:noautocmd| to prevent triggering
|
|
|
|
OptionSet.
|
2015-11-27 07:56:52 -07:00
|
|
|
|
2020-01-13 01:19:20 -07:00
|
|
|
Non-recursive: |:set| in the autocommand does
|
|
|
|
not trigger OptionSet again.
|
2018-10-29 15:58:13 -07:00
|
|
|
|
2024-01-18 01:14:48 -07:00
|
|
|
Not triggered on startup.
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
*QuickFixCmdPre*
|
|
|
|
QuickFixCmdPre Before a quickfix command is run (|:make|,
|
|
|
|
|:lmake|, |:grep|, |:lgrep|, |:grepadd|,
|
|
|
|
|:lgrepadd|, |:vimgrep|, |:lvimgrep|,
|
2022-10-08 07:48:07 -07:00
|
|
|
|:vimgrepadd|, |:lvimgrepadd|,
|
2014-07-10 21:05:51 -07:00
|
|
|
|:cfile|, |:cgetfile|, |:caddfile|, |:lfile|,
|
|
|
|
|:lgetfile|, |:laddfile|, |:helpgrep|,
|
2017-04-28 14:09:56 -07:00
|
|
|
|:lhelpgrep|, |:cexpr|, |:cgetexpr|,
|
|
|
|
|:caddexpr|, |:cbuffer|, |:cgetbuffer|,
|
|
|
|
|:caddbuffer|).
|
2014-07-10 21:05:51 -07:00
|
|
|
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
|
2022-11-24 03:23:50 -07:00
|
|
|
it is run after the error file is read and
|
|
|
|
before moving to the first error.
|
2014-07-10 21:05:51 -07:00
|
|
|
See |QuickFixCmdPost-example|.
|
|
|
|
*QuitPre*
|
|
|
|
QuitPre When using `:quit`, `:wq` or `:qall`, before
|
|
|
|
deciding whether it closes the current window
|
2021-09-08 07:24:12 -07:00
|
|
|
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.
|
2020-01-13 01:19:20 -07:00
|
|
|
See also |ExitPre|, |WinClosed|.
|
2014-07-10 21:05:51 -07:00
|
|
|
*RemoteReply*
|
|
|
|
RemoteReply When a reply from a Vim that functions as
|
2022-09-25 16:58:27 -07:00
|
|
|
server was received server2client(). The
|
2014-07-10 21:05:51 -07:00
|
|
|
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,
|
2022-09-25 16:58:27 -07:00
|
|
|
the reply should be read with remote_read()
|
2014-07-10 21:05:51 -07:00
|
|
|
to consume it.
|
2021-12-10 12:28:25 -07:00
|
|
|
*SearchWrapped*
|
|
|
|
SearchWrapped After making a search with |n| or |N| if the
|
|
|
|
search wraps around the document back to
|
|
|
|
the start/finish respectively.
|
2021-08-17 06:56:37 -07:00
|
|
|
*RecordingEnter*
|
2021-10-01 00:40:22 -07:00
|
|
|
RecordingEnter When a macro starts recording.
|
2021-08-17 06:56:37 -07:00
|
|
|
The pattern is the current file name, and
|
|
|
|
|reg_recording()| is the current register that
|
|
|
|
is used.
|
2021-10-01 00:40:22 -07:00
|
|
|
*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.
|
2022-01-04 18:55:29 -07:00
|
|
|
Sets these |v:event| keys:
|
|
|
|
regcontents
|
|
|
|
regname
|
2023-08-20 19:45:25 -07:00
|
|
|
*SafeState*
|
|
|
|
SafeState When nothing is pending, going to wait for the
|
|
|
|
user to type a character.
|
|
|
|
This will not be triggered when:
|
|
|
|
- an operator is pending
|
|
|
|
- a register was entered with "r
|
|
|
|
- halfway executing a command
|
|
|
|
- executing a mapping
|
|
|
|
- there is typeahead
|
|
|
|
- Insert mode completion is active
|
|
|
|
- Command line completion is active
|
|
|
|
You can use `mode()` to find out what state
|
|
|
|
Vim is in. That may be:
|
2024-01-23 15:07:42 -07:00
|
|
|
- Visual mode
|
2023-08-20 19:45:25 -07:00
|
|
|
- Normal mode
|
|
|
|
- Insert mode
|
|
|
|
- Command-line mode
|
|
|
|
Depending on what you want to do, you may also
|
|
|
|
check more with `state()`, e.g. whether the
|
|
|
|
screen was scrolled for messages.
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
*SessionLoadPost*
|
|
|
|
SessionLoadPost After loading the session file created using
|
2024-03-26 14:41:41 -07:00
|
|
|
the |:mksession| command.
|
|
|
|
*SessionWritePost*
|
|
|
|
SessionWritePost After writing a session file by calling
|
2014-07-10 21:05:51 -07:00
|
|
|
the |:mksession| command.
|
|
|
|
*ShellCmdPost*
|
|
|
|
ShellCmdPost After executing a shell command with |:!cmd|,
|
2014-10-05 10:32:24 -07:00
|
|
|
|:make| and |:grep|. Can be used to check for
|
|
|
|
any changed files.
|
|
|
|
For non-blocking shell commands, see
|
2015-04-10 06:22:46 -07:00
|
|
|
|job-control|.
|
2019-02-03 18:39:05 -07:00
|
|
|
*Signal*
|
2019-02-04 05:21:35 -07:00
|
|
|
Signal After Nvim receives a signal. The pattern is
|
|
|
|
matched against the signal name. Only
|
2022-04-10 15:56:08 -07:00
|
|
|
"SIGUSR1" and "SIGWINCH" are supported. Example: >
|
2019-02-04 05:21:35 -07:00
|
|
|
autocmd Signal SIGUSR1 call some#func()
|
|
|
|
< *ShellFilterPost*
|
2014-07-10 21:05:51 -07:00
|
|
|
ShellFilterPost After executing a shell command with
|
|
|
|
":{range}!cmd", ":w !cmd" or ":r !cmd".
|
|
|
|
Can be used to check for any changed files.
|
|
|
|
*SourcePre*
|
2024-04-30 04:30:21 -07:00
|
|
|
SourcePre Before sourcing a Vimscript/Lua file. |:source|
|
2014-07-10 21:05:51 -07:00
|
|
|
<afile> is the name of the file being sourced.
|
2019-05-22 20:44:57 -07:00
|
|
|
*SourcePost*
|
2024-04-30 04:30:21 -07:00
|
|
|
SourcePost After sourcing a Vimscript/Lua file. |:source|
|
2019-05-22 20:44:57 -07:00
|
|
|
<afile> is the name of the file being sourced.
|
|
|
|
Not triggered when sourcing was interrupted.
|
|
|
|
Also triggered after a SourceCmd autocommand
|
|
|
|
was triggered.
|
2014-07-10 21:05:51 -07:00
|
|
|
*SourceCmd*
|
2024-04-30 04:30:21 -07:00
|
|
|
SourceCmd When sourcing a Vimscript/Lua file. |:source|
|
2014-07-10 21:05:51 -07:00
|
|
|
<afile> is the name of the file being sourced.
|
2024-04-30 04:30:21 -07:00
|
|
|
The autocommand must source that file.
|
2014-07-10 21:05:51 -07:00
|
|
|
|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*
|
2017-12-27 11:30:23 -07:00
|
|
|
StdinReadPost During startup, after reading from stdin into
|
|
|
|
the buffer, before executing modelines. |--|
|
2014-07-10 21:05:51 -07:00
|
|
|
*StdinReadPre*
|
2017-12-27 11:30:23 -07:00
|
|
|
StdinReadPre During startup, before reading from stdin into
|
|
|
|
the buffer. |--|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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*
|
2020-01-18 20:38:48 -07:00
|
|
|
Cannot change to another buffer, change
|
|
|
|
the buffer name or change directory.
|
2014-07-10 21:05:51 -07:00
|
|
|
*Syntax*
|
|
|
|
Syntax When the 'syntax' option has been set. The
|
|
|
|
pattern is matched against the syntax name.
|
2020-01-18 20:38:48 -07:00
|
|
|
<afile> expands to the name of the file where
|
|
|
|
this option was set. <amatch> expands to the
|
|
|
|
new value of 'syntax'.
|
2014-07-10 21:05:51 -07:00
|
|
|
See |:syn-on|.
|
|
|
|
*TabEnter*
|
|
|
|
TabEnter Just after entering a tab page. |tab-page|
|
2020-01-18 20:38:48 -07:00
|
|
|
After WinEnter.
|
|
|
|
Before BufEnter.
|
2014-07-10 21:05:51 -07:00
|
|
|
*TabLeave*
|
|
|
|
TabLeave Just before leaving a tab page. |tab-page|
|
2020-01-18 20:38:48 -07:00
|
|
|
After WinLeave.
|
2017-11-14 02:47:49 -07:00
|
|
|
*TabNew*
|
2014-12-21 22:05:22 -07:00
|
|
|
TabNew When creating a new tab page. |tab-page|
|
2020-01-18 20:38:48 -07:00
|
|
|
After WinEnter.
|
|
|
|
Before TabEnter.
|
2017-11-14 02:47:49 -07:00
|
|
|
*TabNewEntered*
|
2014-12-23 07:56:10 -07:00
|
|
|
TabNewEntered After entering a new tab page. |tab-page|
|
|
|
|
After BufEnter.
|
2017-11-14 02:47:49 -07:00
|
|
|
*TabClosed*
|
2020-01-13 01:19:20 -07:00
|
|
|
TabClosed After closing a tab page. <afile> expands to
|
|
|
|
the tab page number.
|
2017-11-14 02:47:49 -07:00
|
|
|
*TermOpen*
|
|
|
|
TermOpen When a |terminal| job is starting. Can be
|
|
|
|
used to configure the terminal buffer.
|
2019-09-14 15:54:19 -07:00
|
|
|
*TermEnter*
|
|
|
|
TermEnter After entering |Terminal-mode|.
|
|
|
|
After TermOpen.
|
|
|
|
*TermLeave*
|
|
|
|
TermLeave After leaving |Terminal-mode|.
|
|
|
|
After TermClose.
|
|
|
|
*TermClose*
|
|
|
|
TermClose When a |terminal| job ends.
|
2021-08-20 10:45:28 -07:00
|
|
|
Sets these |v:event| keys:
|
|
|
|
status
|
2024-01-09 07:27:56 -07:00
|
|
|
*TermRequest*
|
2024-04-30 04:30:21 -07:00
|
|
|
TermRequest When a |:terminal| child process emits an OSC
|
|
|
|
or DCS sequence. Sets |v:termrequest|. The
|
|
|
|
|event-data| is the request string.
|
2014-07-10 21:05:51 -07:00
|
|
|
*TermResponse*
|
2023-11-16 10:12:42 -07:00
|
|
|
TermResponse When Nvim receives an OSC or DCS response from
|
2024-04-30 04:30:21 -07:00
|
|
|
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: >lua
|
2023-11-06 11:42:40 -07:00
|
|
|
|
|
|
|
-- 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
|
2023-12-05 17:04:21 -07:00
|
|
|
local r, g, b = resp:match("\027%]4;1;rgb:(%w+)/(%w+)/(%w+)")
|
2023-11-06 11:42:40 -07:00
|
|
|
end,
|
|
|
|
})
|
2023-12-05 17:04:21 -07:00
|
|
|
io.stdout:write("\027]4;1;?\027\\")
|
2023-11-06 11:42:40 -07:00
|
|
|
<
|
2014-07-10 21:05:51 -07:00
|
|
|
*TextChanged*
|
|
|
|
TextChanged After a change was made to the text in the
|
2019-07-28 18:03:53 -07:00
|
|
|
current buffer in Normal mode. That is after
|
|
|
|
|b:changedtick| has changed (also when that
|
|
|
|
happened before the TextChanged autocommand
|
|
|
|
was defined).
|
2014-07-10 21:05:51 -07:00
|
|
|
Not triggered when there is typeahead or when
|
|
|
|
an operator is pending.
|
2019-08-01 21:26:19 -07:00
|
|
|
Note: Cannot be skipped with `:noautocmd`.
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
2018-02-11 06:37:14 -07:00
|
|
|
*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.
|
2022-11-06 20:02:29 -07:00
|
|
|
*TextChangedT*
|
|
|
|
TextChangedT After a change was made to the text in the
|
|
|
|
current buffer in |Terminal-mode|. Otherwise
|
|
|
|
the same as TextChanged.
|
2021-10-16 08:07:40 -07:00
|
|
|
*TextYankPost*
|
|
|
|
TextYankPost Just after a |yank| or |deleting| command, but not
|
|
|
|
if the black hole register |quote_| is used nor
|
2023-06-21 06:40:40 -07:00
|
|
|
for |setreg()|. Pattern must be "*".
|
2021-10-16 08:07:40 -07:00
|
|
|
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.
|
|
|
|
|
|
|
|
Non-recursive (event cannot trigger itself).
|
|
|
|
Cannot change the text. |textlock|
|
2014-07-10 21:05:51 -07:00
|
|
|
*User*
|
2019-08-04 18:33:45 -07:00
|
|
|
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*
|
2014-07-10 21:05:51 -07:00
|
|
|
UserGettingBored When the user presses the same key 42 times.
|
|
|
|
Just kidding! :-)
|
|
|
|
*VimEnter*
|
|
|
|
VimEnter After doing all the startup stuff, including
|
2015-10-17 07:25:53 -07:00
|
|
|
loading vimrc files, executing the "-c cmd"
|
2014-07-10 21:05:51 -07:00
|
|
|
arguments, creating all windows and loading
|
|
|
|
the buffers in them.
|
2016-11-14 08:19:06 -07:00
|
|
|
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
|
2020-08-31 00:51:35 -07:00
|
|
|
au VimEnter * call s:init()
|
2016-11-14 08:19:06 -07:00
|
|
|
endif
|
|
|
|
< *VimLeave*
|
2014-07-10 21:05:51 -07:00
|
|
|
VimLeave Before exiting Vim, just after writing the
|
2015-07-05 04:08:50 -07:00
|
|
|
.shada file. Executed only once, like
|
2014-07-10 21:05:51 -07:00
|
|
|
VimLeavePre.
|
2020-08-31 00:51:35 -07:00
|
|
|
Use |v:dying| to detect an abnormal exit.
|
2017-03-14 16:12:21 -07:00
|
|
|
Use |v:exiting| to get the exit code.
|
|
|
|
Not triggered if |v:dying| is 2 or more.
|
2014-07-10 21:05:51 -07:00
|
|
|
*VimLeavePre*
|
|
|
|
VimLeavePre Before exiting Vim, just before writing the
|
2015-07-05 04:08:50 -07:00
|
|
|
.shada file. This is executed only once,
|
2014-07-10 21:05:51 -07:00
|
|
|
if there is a match with the name of what
|
|
|
|
happens to be the current buffer when exiting.
|
|
|
|
Mostly useful with a "*" pattern. >
|
2020-08-31 00:51:35 -07:00
|
|
|
:autocmd VimLeavePre * call CleanupStuff()
|
2017-03-14 16:12:21 -07:00
|
|
|
< 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.
|
2014-07-10 21:05:51 -07:00
|
|
|
*VimResized*
|
|
|
|
VimResized After the Vim window was resized, thus 'lines'
|
|
|
|
and/or 'columns' changed. Not when starting
|
|
|
|
up though.
|
2018-04-14 06:51:22 -07:00
|
|
|
*VimResume*
|
|
|
|
VimResume After Nvim resumes from |suspend| state.
|
|
|
|
*VimSuspend*
|
|
|
|
VimSuspend Before Nvim enters |suspend| state.
|
2018-09-29 11:40:53 -07:00
|
|
|
*WinClosed*
|
2022-11-24 03:23:50 -07:00
|
|
|
WinClosed When closing a window, just before it is
|
|
|
|
removed from the window layout. The pattern
|
|
|
|
is matched against the |window-ID|. Both
|
2021-11-17 19:11:09 -07:00
|
|
|
<amatch> and <afile> are set to the |window-ID|.
|
2020-01-18 20:38:48 -07:00
|
|
|
After WinLeave.
|
2020-01-13 01:19:20 -07:00
|
|
|
Non-recursive (event cannot trigger itself).
|
|
|
|
See also |ExitPre|, |QuitPre|.
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2018-10-29 00:27:28 -07:00
|
|
|
Note: For split and tabpage commands the
|
|
|
|
WinEnter event is triggered after the split
|
|
|
|
or tab command but before the file is loaded.
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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.
|
2022-03-09 23:34:55 -07:00
|
|
|
Before WinClosed.
|
2017-01-08 08:12:52 -07:00
|
|
|
*WinNew*
|
|
|
|
WinNew When a new window was created. Not done for
|
2017-04-28 14:19:10 -07:00
|
|
|
the first window, when Vim has just started.
|
2020-01-18 20:38:48 -07:00
|
|
|
Before WinEnter.
|
2020-10-28 01:49:49 -07:00
|
|
|
|
2022-04-11 01:24:15 -07:00
|
|
|
*WinScrolled*
|
2022-11-22 18:54:48 -07:00
|
|
|
WinScrolled After any window in the current tab page
|
|
|
|
scrolled the text (horizontally or vertically)
|
|
|
|
or changed width or height. See
|
|
|
|
|win-scrolled-resized|.
|
2022-11-19 17:38:46 -07:00
|
|
|
|
|
|
|
The pattern is matched against the |window-ID|
|
|
|
|
of the first window that scrolled or resized.
|
|
|
|
Both <amatch> and <afile> are set to the
|
|
|
|
|window-ID|.
|
|
|
|
|
2022-11-22 18:54:48 -07:00
|
|
|
|v:event| is set with information about size
|
|
|
|
and scroll changes. |WinScrolled-event|
|
|
|
|
|
2022-11-19 17:38:46 -07:00
|
|
|
Only starts triggering after startup finished
|
|
|
|
and the first screen redraw was done.
|
2022-11-22 18:54:48 -07:00
|
|
|
Does not trigger when defining the first
|
|
|
|
WinScrolled or WinResized event, but may
|
|
|
|
trigger when adding more.
|
2022-11-19 17:38:46 -07:00
|
|
|
|
|
|
|
Non-recursive: the event will not trigger
|
|
|
|
while executing commands for the WinScrolled
|
|
|
|
event. However, if the command causes a
|
|
|
|
window to scroll or change size, then another
|
2022-04-11 01:24:15 -07:00
|
|
|
WinScrolled event will be triggered later.
|
2022-11-19 17:38:46 -07:00
|
|
|
|
2022-11-22 18:54:48 -07:00
|
|
|
|
|
|
|
*WinResized*
|
|
|
|
WinResized After a window in the current tab page changed
|
|
|
|
width or height.
|
|
|
|
See |win-scrolled-resized|.
|
|
|
|
|
|
|
|
|v:event| is set with information about size
|
|
|
|
changes. |WinResized-event|
|
|
|
|
|
|
|
|
Same behavior as |WinScrolled| for the
|
|
|
|
pattern, triggering and recursiveness.
|
2017-01-08 08:12:52 -07:00
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
==============================================================================
|
2022-01-17 03:58:36 -07:00
|
|
|
6. Patterns *autocmd-pattern* *{aupat}*
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2022-11-19 17:38:46 -07:00
|
|
|
The {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: >
|
2016-01-27 06:17:19 -07:00
|
|
|
:autocmd BufRead *.txt,*.info set et
|
|
|
|
Is equivalent to: >
|
|
|
|
:autocmd BufRead *.txt set et
|
|
|
|
:autocmd BufRead *.info set et
|
|
|
|
|
2022-01-17 03:58:36 -07:00
|
|
|
The file pattern {aupat} is tested for a match against the file name in one of
|
2014-07-10 21:05:51 -07:00
|
|
|
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).
|
|
|
|
|
|
|
|
The special pattern <buffer> or <buffer=N> is used for buffer-local
|
|
|
|
autocommands |autocmd-buflocal|. This pattern is not matched against the name
|
|
|
|
of a buffer.
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
:autocmd BufRead *.txt set et
|
|
|
|
Set the 'et' option for all text files. >
|
|
|
|
|
|
|
|
:autocmd BufRead /vim/src/*.c set cindent
|
|
|
|
Set the 'cindent' option for C files in the /vim/src directory. >
|
|
|
|
|
|
|
|
:autocmd BufRead /tmp/*.c set ts=5
|
|
|
|
If 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.
|
|
|
|
|
2023-05-13 12:33:22 -07:00
|
|
|
Note: To match part of a path, but not from the root directory, use a "*" as
|
2014-07-10 21:05:51 -07:00
|
|
|
the first character. Example: >
|
|
|
|
:autocmd BufRead */doc/*.txt set tw=78
|
|
|
|
This 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.
|
|
|
|
|
|
|
|
|
|
|
|
The file name that the pattern is matched against is after expanding
|
|
|
|
wildcards. Thus if you issue this command: >
|
|
|
|
:e $ROOTDIR/main.$EXT
|
|
|
|
The argument is first expanded to: >
|
|
|
|
/usr/root/main.py
|
|
|
|
Before 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.
|
|
|
|
|
|
|
|
|
|
|
|
Environment variables can be used in a pattern: >
|
|
|
|
:autocmd BufRead $VIMRUNTIME/doc/*.txt set expandtab
|
|
|
|
And ~ can be used for the home directory (if $HOME is defined): >
|
2015-10-17 07:25:53 -07:00
|
|
|
:autocmd BufWritePost ~/.config/nvim/init.vim so <afile>
|
2014-07-10 21:05:51 -07:00
|
|
|
:autocmd BufRead ~archive/* set readonly
|
|
|
|
The 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'
|
|
|
|
|
|
|
|
Note that for all systems the '/' character is used for path separator (even
|
2016-01-06 16:45:50 -07:00
|
|
|
Windows). This was done because the backslash is difficult to use in a pattern
|
2014-11-17 23:44:47 -07:00
|
|
|
and to make the autocommands portable across different systems.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2017-04-28 14:09:56 -07:00
|
|
|
It is possible to use |pattern| items, but they may not work as expected,
|
|
|
|
because of the translation done for the above.
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
*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 modified
|
|
|
|
|
|
|
|
This 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.
|
|
|
|
|
|
|
|
However, buffer-local autocommands will not be executed for a buffer that has
|
|
|
|
been wiped out with |:bwipe|. After deleting the buffer with |:bdel| the
|
|
|
|
buffer actually still exists (it becomes unlisted), thus the autocommands are
|
|
|
|
still executed.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
7. Buffer-local autocommands *autocmd-buflocal* *autocmd-buffer-local*
|
2022-09-25 16:58:27 -07:00
|
|
|
*<buffer>* *<buffer=N>* *<buffer=abuf>* *E680*
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
Buffer-local autocommands are attached to a specific buffer. They are useful
|
|
|
|
if the buffer does not have a name and when the name does not match a specific
|
|
|
|
pattern. But it also means they must be explicitly added to each buffer.
|
|
|
|
|
|
|
|
Instead of a pattern buffer-local autocommands use one of these forms:
|
|
|
|
<buffer> current buffer
|
|
|
|
<buffer=99> buffer number 99
|
|
|
|
<buffer=abuf> using <abuf> (only when executing autocommands)
|
|
|
|
|<abuf>|
|
|
|
|
|
|
|
|
Examples: >
|
|
|
|
:au CursorHold <buffer> echo 'hold'
|
|
|
|
:au CursorHold <buffer=33> echo 'hold'
|
2016-04-17 15:47:44 -07:00
|
|
|
:au BufNewFile * au CursorHold <buffer=abuf> echo 'hold'
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
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 buffer
|
|
|
|
|
|
|
|
Note 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.
|
|
|
|
|
|
|
|
To test for presence of buffer-local autocommands use the |exists()| function
|
|
|
|
as follows: >
|
|
|
|
:if exists("#CursorHold#<buffer=12>") | ... | endif
|
|
|
|
:if exists("#CursorHold#<buffer>") | ... | endif " for current buffer
|
|
|
|
|
|
|
|
When 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=6
|
|
|
|
|
|
|
|
It is not possible to define buffer-local autocommands for a non-existent
|
|
|
|
buffer.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
8. Groups *autocmd-groups*
|
|
|
|
|
|
|
|
Autocommands can be put together in a group. This is useful for removing or
|
|
|
|
executing a group of autocommands. For example, all the autocommands for
|
|
|
|
syntax highlighting are put in the "highlight" group, to be able to execute
|
|
|
|
":doautoall highlight BufRead" when the GUI starts.
|
|
|
|
|
|
|
|
When no specific group is selected, Vim uses the default group. The default
|
|
|
|
group does not have a name. You cannot execute the autocommands from the
|
|
|
|
default group separately; you can execute them only by executing autocommands
|
|
|
|
for all groups.
|
|
|
|
|
|
|
|
Normally, when executing autocommands automatically, Vim uses the autocommands
|
|
|
|
for all groups. The group only matters when executing autocommands with
|
|
|
|
":doautocmd" or ":doautoall", or when defining or deleting autocommands.
|
|
|
|
|
|
|
|
The group name can contain any characters except white space. The group name
|
|
|
|
"end" is reserved (also in uppercase).
|
|
|
|
|
|
|
|
The group name is case sensitive. Note that this is different from the event
|
|
|
|
name!
|
|
|
|
|
|
|
|
*:aug* *:augroup*
|
|
|
|
:aug[roup] {name} Define the autocmd group name for the
|
|
|
|
following ":autocmd" commands. The name "end"
|
|
|
|
or "END" selects the default group.
|
2016-05-03 12:16:53 -07:00
|
|
|
To avoid confusion, the name should be
|
|
|
|
different from existing {event} names, as this
|
|
|
|
most likely will not do what you intended.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2017-04-28 14:09:56 -07:00
|
|
|
*:augroup-delete* *E367* *W19* *E936*
|
2014-07-10 21:05:51 -07:00
|
|
|
:aug[roup]! {name} Delete the autocmd group {name}. Don't use
|
|
|
|
this if there is still an autocommand using
|
2017-03-21 04:11:53 -07:00
|
|
|
this group! You will get a warning if doing
|
2021-04-29 15:34:49 -07:00
|
|
|
it anyway. When the group is the current
|
|
|
|
group you will get error E936.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
To enter autocommands for a specific group, use this method:
|
|
|
|
1. Select the group with ":augroup {name}".
|
|
|
|
2. Delete any old autocommands with ":au!".
|
|
|
|
3. Define the autocommands.
|
|
|
|
4. Go back to the default group with "augroup END".
|
|
|
|
|
|
|
|
Example: >
|
|
|
|
:augroup uncompress
|
|
|
|
: au!
|
|
|
|
: au BufEnter *.gz %!gunzip
|
|
|
|
:augroup END
|
|
|
|
|
|
|
|
This prevents having the autocommands defined twice (e.g., after sourcing the
|
2015-10-17 07:25:53 -07:00
|
|
|
vimrc file again).
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2021-10-05 05:12:16 -07:00
|
|
|
*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
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
==============================================================================
|
|
|
|
9. Executing autocommands *autocmd-execute*
|
|
|
|
|
|
|
|
Vim can also execute Autocommands non-automatically. This is useful if you
|
|
|
|
have changed autocommands, or when Vim has executed the wrong autocommands
|
|
|
|
(e.g., the file pattern match was wrong).
|
|
|
|
|
|
|
|
Note that the 'eventignore' option applies here too. Events listed in this
|
|
|
|
option will not cause any commands to be executed.
|
|
|
|
|
2019-08-01 21:44:48 -07:00
|
|
|
*:do* *:doau* *:doaut* *:doautocmd* *E217*
|
2014-07-10 21:05:51 -07:00
|
|
|
: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: >
|
2015-10-17 07:25:53 -07:00
|
|
|
:au BufEnter *.cpp so ~/.config/nvim/init_cpp.vim
|
2014-07-10 21:05:51 -07:00
|
|
|
:au BufEnter *.cpp doau BufEnter x.c
|
2019-03-09 20:32:58 -07:00
|
|
|
< Be careful to avoid endless loops. |autocmd-nested|
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
When the [group] argument is not given, Vim executes
|
|
|
|
the autocommands for all groups. When the [group]
|
|
|
|
argument is included, Vim executes only the matching
|
2019-08-04 18:33:45 -07:00
|
|
|
autocommands for that group. Undefined group is an
|
|
|
|
error.
|
2014-07-10 21:05:51 -07:00
|
|
|
*<nomodeline>*
|
|
|
|
After applying the autocommands the modelines are
|
|
|
|
processed, so that their settings overrule the
|
2019-08-04 18:33:45 -07:00
|
|
|
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.
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
*:doautoa* *:doautoall*
|
|
|
|
:doautoa[ll] [<nomodeline>] [group] {event} [fname]
|
|
|
|
Like ":doautocmd", but apply the autocommands to each
|
2021-05-02 08:10:25 -07:00
|
|
|
loaded buffer. The current buffer is done last.
|
|
|
|
|
|
|
|
Note that [fname] is used to select the autocommands,
|
2021-05-02 09:55:08 -07:00
|
|
|
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.
|
2021-05-02 08:10:25 -07:00
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
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|
|
|
|
|
|
2023-06-21 06:40:40 -07:00
|
|
|
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
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
|
|
|
|
2023-06-21 06:40:40 -07:00
|
|
|
The "*WritePre" and "*AppendPre" autocommands must not delete the buffer from
|
2014-07-10 21:05:51 -07:00
|
|
|
which the lines are to be written.
|
|
|
|
|
|
|
|
The '[ and '] marks have a special position:
|
2023-06-21 06:40:40 -07:00
|
|
|
- Before the "*ReadPre" event the '[ mark is set to the line just above where
|
2014-07-10 21:05:51 -07:00
|
|
|
the new lines will be inserted.
|
2023-06-21 06:40:40 -07:00
|
|
|
- Before the "*ReadPost" event the '[ mark is set to the first line that was
|
2014-07-10 21:05:51 -07:00
|
|
|
just read, the '] mark to the last line.
|
2023-06-21 06:40:40 -07:00
|
|
|
- Before executing the "*WriteCmd", "*WritePre" and "*AppendPre" autocommands the '[
|
2014-07-10 21:05:51 -07:00
|
|
|
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
|
2018-10-29 01:29:42 -07:00
|
|
|
buffer. This also works for buffers that don't have a name. But it doesn't
|
2014-07-10 21:05:51 -07:00
|
|
|
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
|
2022-02-27 03:56:30 -07:00
|
|
|
: autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " .. expand("%:r")
|
2014-07-10 21:05:51 -07:00
|
|
|
: 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.
|
|
|
|
|
|
|
|
("<afile>: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*
|
2019-08-01 13:30:40 -07:00
|
|
|
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
|
2019-03-13 14:13:25 -07:00
|
|
|
those commands. If you do want this, use the "++nested" flag for those
|
2019-03-11 13:01:47 -07:00
|
|
|
commands in which you want nesting. For example: >
|
2019-03-13 14:13:25 -07:00
|
|
|
:autocmd FileChangedShell *.c ++nested e!
|
2014-07-10 21:05:51 -07:00
|
|
|
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.
|
|
|
|
|
|
|
|
*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.java
|
|
|
|
|
2023-06-21 06:40:40 -07:00
|
|
|
To insert the current date and time in a "*.html" file when writing it: >
|
2014-07-10 21:05:51 -07:00
|
|
|
|
|
|
|
:autocmd BufWritePre,FileWritePre *.html ks|call LastMod()|'s
|
|
|
|
:fun LastMod()
|
|
|
|
: if line("$") > 20
|
|
|
|
: let l = 20
|
|
|
|
: else
|
|
|
|
: let l = line("$")
|
|
|
|
: endif
|
2022-02-27 03:56:30 -07:00
|
|
|
: exe "1," .. l .. "g/Last modified: /s/Last modified: .*/Last modified: " ..
|
2014-07-10 21:05:51 -07:00
|
|
|
: \ strftime("%Y %b %d")
|
|
|
|
:endfun
|
|
|
|
|
|
|
|
You 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.
|
|
|
|
|
|
|
|
When entering :autocmd on the command-line, completion of events and command
|
|
|
|
names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
|
|
|
|
|
|
|
|
Vim executes all matching autocommands in the order that you specify them.
|
|
|
|
It is recommended that your first autocommand be used for all files by using
|
|
|
|
"*" as the file pattern. This means that you can define defaults you like
|
|
|
|
here for any settings, and if there is another matching autocommand it will
|
|
|
|
override these. But if there is no other matching autocommand, then at least
|
|
|
|
your default settings are recovered (if entering this file from another for
|
|
|
|
which autocommands did match). Note that "*" will also match files starting
|
|
|
|
with ".", unlike Unix shells.
|
|
|
|
|
|
|
|
*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
|
2015-07-05 16:26:44 -07:00
|
|
|
autocommand. Use the 'h' flag in the 'shada' option to disable search-
|
2014-07-10 21:05:51 -07:00
|
|
|
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://*".
|
|
|
|
|
|
|
|
When defining a BufReadCmd it will be difficult for Vim to recover a crashed
|
|
|
|
editing session. When recovering from the original file, Vim reads only those
|
|
|
|
parts of a file that are not found in the swap file. Since that is not
|
|
|
|
possible with a BufReadCmd, use the |:preserve| command to make sure the
|
|
|
|
original file isn't needed for recovery. You might want to do this only when
|
|
|
|
you expect the file to be modified.
|
|
|
|
|
|
|
|
For file read and write commands the |v:cmdarg| variable holds the "++enc="
|
|
|
|
and "++ff=" argument that are effective. These should be used for the command
|
|
|
|
that reads/writes the file. The |v:cmdbang| variable is one when "!" was
|
|
|
|
used, zero otherwise.
|
|
|
|
|
|
|
|
See the $VIMRUNTIME/plugin/netrwPlugin.vim for examples.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
11. Disabling autocommands *autocmd-disable*
|
|
|
|
|
|
|
|
To disable autocommands for some time use the 'eventignore' option. Note that
|
|
|
|
this may cause unexpected behavior, make sure you restore 'eventignore'
|
|
|
|
afterwards, using a |:try| block with |:finally|.
|
|
|
|
|
|
|
|
*: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.gz
|
|
|
|
|
|
|
|
This will write the file without triggering the autocommands defined by the
|
|
|
|
gzip plugin.
|
|
|
|
|
2019-08-01 13:30:40 -07:00
|
|
|
Note that some autocommands are not triggered right away, but only later.
|
|
|
|
This specifically applies to |CursorMoved| and |TextChanged|.
|
|
|
|
|
2014-07-10 21:05:51 -07:00
|
|
|
|
2014-09-13 12:21:15 -07:00
|
|
|
vim:tw=78:ts=8:noet:ft=help:norl:
|