10

运行后:bufdo e! 我的所有文件都会丢失它们的文件类型设置,我必须:set ft=XXX在每个文件中手动运行。

有谁知道如何解决这个问题?

运行:bufdo set ft=XXX不起作用,我无论如何都不想将所有文件设置为相同的文件类型。

干杯。

4

3 回答 3

17

出于性能原因,该bufdo命令不会更新语法突出显示:

来自 vim 文档:

注意:在执行此命令时,通过将语法自动命令事件添加到“eventignore”来禁用它。这大大加快了每个缓冲区的编辑速度

您可以通过重新运行来更新受影响缓冲区的语法突出显示:

:syntax on

于 2012-05-09T10:29:19.820 回答
7

您可以通过以下 autocmd 自动修复此问题:

" Enable syntax highlighting when buffers were loaded through :bufdo, which
" disables the Syntax autocmd event to speed up processing.
augroup EnableSyntaxHighlighting
    " Filetype processing does happen, so we can detect a buffer initially
    " loaded during :bufdo through a set filetype, but missing b:current_syntax.
    " Also don't do this when the user explicitly turned off syntax highlighting
    " via :syntax off.
    " Note: Must allow nesting of autocmds so that the :syntax enable triggers
    " the ColorScheme event. Otherwise, some highlighting groups may not be
    " restored properly.
    autocmd! BufWinEnter * nested if exists('syntax_on') && ! exists('b:current_syntax') && ! empty(&l:filetype) | syntax enable | endif

    " The above does not handle reloading via :bufdo edit!, because the
    " b:current_syntax variable is not cleared by that. During the :bufdo,
    " 'eventignore' contains "Syntax", so this can be used to detect this
    " situation when the file is re-read into the buffer. Due to the
    " 'eventignore', an immediate :syntax enable is ignored, but by clearing
    " b:current_syntax, the above handler will do this when the reloaded buffer
    " is displayed in a window again.
    autocmd! BufRead * if exists('syntax_on') && exists('b:current_syntax') && ! empty(&l:filetype) && index(split(&eventignore, ','), 'Syntax') != -1 | unlet! b:current_syntax | endif
augroup END

Edit: Add autocmd nesting for proper restore of highlight groups and handle buffer reloads, as the question explicitly asked for this.

于 2012-05-09T11:27:57.407 回答
6

If you're checking for changed files (for example after switching branches in your VCS) then :checktime may be a more appropriate solution than :bufdo e! - it's designed for this purpose and doesn't have the syntax highlighting issue.

于 2014-01-24T11:48:39.023 回答