filetype setting lost after reloading all files in buffer

前端 未结 3 1011
失恋的感觉
失恋的感觉 2020-12-16 19:15

After running :bufdo e! All my files lose their filetype setting and I have to manually run :set ft=XXX in each file.

Anyone know how to so

相关标签:
3条回答
  • 2020-12-16 19:52

    The bufdo command doesn't update syntax hightlighting for performance reasons:

    From vim docs:

    Note: While this command is executing, the Syntax autocommand event is disabled by adding it to 'eventignore'. This considerably speeds up editing each buffer

    You can update the syntax highlighting for affected buffers by re-running:

    :syntax on

    0 讨论(0)
  • 2020-12-16 19:59

    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.

    0 讨论(0)
  • 2020-12-16 20:02

    You can fix this automatically via the following 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.

    0 讨论(0)
提交回复
热议问题