filetype setting lost after reloading all files in buffer

前端 未结 3 1025
失恋的感觉
失恋的感觉 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 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.

提交回复
热议问题