重新加载缓冲区中的所有文件后,文件类型设置丢失

时间:2012-05-09 09:43:49

标签: vim

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

任何人都知道如何解决这个问题?

运行:bufdo set ft=XXX不起作用,我不想以任何速度将所有文件设置为相同的文件类型。

干杯。

3 个答案:

答案 0 :(得分:15)

由于性能原因,bufdo命令不会更新语法高亮显示:

来自vim docs:

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

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

:syntax on

答案 1 :(得分:5)

您可以通过以下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

编辑:添加autocmd嵌套以正确恢复突出显示组并处理缓冲区重新加载,因为明确要求此问题。

答案 2 :(得分:5)

如果您要检查更改的文件(例如在VCS中切换分支后),那么:checktime可能是比:bufdo e!更合适的解决方案 - 它是为此目的而设计的,但没有语法高亮问题。

相关问题