vim Autocommands

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

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?