自动命令

环绕 autocmd 命令

autocmd 是一个附加命令,默认情况下你可能不希望出现此行为。

例如,如果你在编辑时重新发送你的 .vimrc 几次,vim 可能会变慢。

这是证据:

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

如果你现在输入:autocmd BufWritePost *,你会看到输出中的行,而不只是一行。两者都被执行了。

要避免此行为,请按如下方式环绕所有 autocmds:

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