autocmd函数总是执行两次

时间:2013-04-24 15:15:51

标签: vim duplication autocmd

我把以下一堆很棒的东西放在一起:

if !exists("g:AsciidocAutoSave")
  let g:AsciidocAutoSave = 0
endif

fun! AsciidocRefresh()
  if g:AsciidocAutoSave == 1
    execute 'write'
    execute 'silent !asciidoc -b html5 -a icons "%"'
  endif
endf

fun! AsciidocFocusLost()
  augroup asciidocFocusLost
    autocmd FocusLost <buffer> call AsciidocRefresh()
  augroup END
endfun

augroup asciidocFileType
  autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END

唯一的问题是:每当vim在asciidoc文件中失去焦点时,它就会保存两次。

当我将:autocmd asciidocFileType放入命令行时,它会显示:

---Auto-Commands---
asciidocFileType  FileType
    asciidoc   :call AsciidocFocusLost()
               :call AsciidocFocusLost()

:autocmd asciidocFocusLostAsciidocRefresh()相同。

为什么要重复这个?

2 个答案:

答案 0 :(得分:3)

我不能肯定地告诉你为什么你得到了那些副本(多个来源?),但是防止这种情况的规范方法是首先清除augroup

augroup asciidocFileType
    autocmd!
    autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END

由于您只有一个定义,因此您也可以在一个命令中执行此操作:

augroup asciidocFileType
    autocmd! FileType asciidoc :call AsciidocFocusLost()
augroup END

答案 1 :(得分:0)

@ingo-karkat答案很棒,但我想我知道为什么你会得到两倍autocmd以及如何自然地预防它:

vim / Neovim的某些发行版默认情况下确保filetype plugin indenton。使用env XDG_CONFIG_HOME=/dev/null nvim -c 'filetype'或使用env HOME=/dev/null vim -c filetype

的vim测试neovim

如果输出

filetype detection:ON  plugin:ON  indent:ON

然后您不需要将filetype * on添加到您的vimrc。

相关问题