自動命令

環繞 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