vim Autocommands

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

Surround autocmd commands

autocmd is an additive command, and you probably don't want this behaviour by default.

For example, if you re-source your .vimrc a few times while editing it, vim can slow down.

Here's proof:

:autocmd BufWritePost * if &diff | diffupdate | endif " update diff after save
:autocmd BufWritePost * if &diff | diffupdate | endif " update diff after save

If you now type :autocmd BufWritePost *, you'll see both lines in the output, not just one. Both get executed.

To avoid this behaviour, surround all your autocmds as follows:

if has ('autocmd')       " Remain compatible with vi which doesn't have autocmd
  augroup MyDiffUpdate   " A unique name for the group.  DO NOT use the same name twice!
      autocmd!           " Clears the old autocommands for this group name
      autocmd BufWritePost * if &diff | diffupdate | endif   " Update diff after save
      " ... etc ...
  augroup END
endif


Got any vim Question?