1

From what I have read in a book by Steve Losh (chapter 14), it seems that to avoid performance issues, it is best practise to enclose autocommands, even the single ones, in autocommand groups:

augroup testgroup
  autocmd!
  autocmd BufWrite * :echom "Cats"
augroup END

I've just started to learn Vim and my question is quite simple. Is it the same as using

augroup testgroup
  autocmd! BufWrite * :echom "Cats"
augroup END

?

I ask this because Steve hasn't provided this second version in his book (at least in the 14 chapter), though it looks more concise.

5
  • 1
    Probably :help autocmd-remove can clarify the difference, or press K in a vim file over autocmd 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.)
    – D. Ben Knoble
    Aug 9, 2022 at 14:53
  • @D.BenKnoble Thanks a lot, I have just read it. One more small question: Is it correct that using augroups here is in fact optional? Isn't it that I can simply use autocmd! BufWrite * :echom "Cats" without enclosing it in an augroup and this will be enough to avoid performace issues?
    – james
    Aug 9, 2022 at 14:59
  • @james, about your "small question," it's not the same. For example, if you use two autocmd! for the same BufWrite * event-pattern pair, the second one would delete the first one. Whereas if you wrapped these two in an augroup, a single au! 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.
    – 3N4N
    Aug 9, 2022 at 15:34
  • 2
    @kadekai worse: without a group, autocmd! deletes all the commands in the default group (see my answer below and :help autocmd-groups). Really, just use autocmd! once at the top of the group and don't confuse anyone :)
    – D. Ben Knoble
    Aug 9, 2022 at 15:37
  • @D.BenKnoble yeah, now that I see it, I was trying to explain exactly what you wrote in your answer. I just didn't see it before.
    – 3N4N
    Aug 9, 2022 at 15:38

1 Answer 1

5

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").


  1. @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].

2
  • 1
    There's actually a difference for a single autocmd in a group. For example, if you update the vimscript file to change the autocmd from BufWrite to BufWritePost and reload it, the first one (with the bare autocmd!) will erase the BufWrite and keep only the BufWritePost, while the second one (with the autocmd! for the specific event/command) will keep both the BufWrite and BufWritePost. Right? You go into details of removing all auto-commands in a group, but maybe best to make it explicit of the case where you're updating the vimscript.
    – filbranden
    Aug 9, 2022 at 16:01
  • 1
    @filbranden great point that I hadn't considered.
    – D. Ben Knoble
    Aug 9, 2022 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.