As in :help autocmd-remove
:
:au[tocmd]! [group] {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].
[…]
When the [group] argument is not given, Vim uses the current group (as defined
with ":augroup"); otherwise, Vim uses the group defined with [group].
For a single autocmd in a group, there is (almost1) no difference It is liable to be confusing, especially with multiple autocommands—you likely do not want !
on the subsequent ones.
Note the qualifer: autocmd!
outside of a group removes all autocommands in the default group (so autocmd! BufWrite * :echom "Cats"
would remove all ungrouped BufWrite *
autocommands and add the single autocommand :echom "Cats"
).
- @filbranden points out below that there is a large difference if you modify the code:
For example, if you update the vimscript file to change the autocmd
from BufWrite
to BufWritePost
and reload it, […] the bare autocmd!
will erase the BufWrite
and keep only the BufWritePost
, while […] the autocmd!
for the specific event/command will keep both the BufWrite
and BufWritePost
[since it will delete only the matching BufWritePost
].
:help autocmd-remove
can clarify the difference, or pressK
in a vim file overautocmd
and scroll down. (Answer: for a single autocmd, there is no difference; however, it is liable to be confusing esp. with multiple autocommands where you want to use no!
on the subsequent ones.)autocmd! BufWrite * :echom "Cats"
without enclosing it in an augroup and this will be enough to avoid performace issues?autocmd!
for the sameBufWrite *
event-pattern pair, the second one would delete the first one. Whereas if you wrapped these two in an augroup, a singleau!
at the start of the augroup would delete all autocmds in that augroup before re-defining them. So both the autocmds for that event-pattern pair will be registered.autocmd!
deletes all the commands in the default group (see my answer below and:help autocmd-groups
). Really, just useautocmd!
once at the top of the group and don't confuse anyone :)