Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filetype setting lost after reloading all files in buffer

Tags:

vim

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 solve this issue?

Running :bufdo set ft=XXX doesn't work and I don't want to set all files to the same filetype at any rate.

Cheers.

like image 632
centerback Avatar asked May 09 '12 09:05

centerback


2 Answers

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

like image 113
Benj Avatar answered Oct 13 '22 05:10

Benj


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.

like image 40
Ingo Karkat Avatar answered Oct 13 '22 03:10

Ingo Karkat