Vim, how to reload syntax highlighting

Solution 1:

Use :syntax sync fromstart

I got that tip from http://vim.wikia.com/wiki/Fix_syntax_highlighting

That article also suggests creating a mapping for that command e.g. to map F12:

noremap <F12> <Esc>:syntax sync fromstart<CR>
inoremap <F12> <C-o>:syntax sync fromstart<CR>

Solution 2:

Try:

:e

If you have no unsaved changes to the file.

Or:

:syn off | syn on

Solution 3:

Tried all the above - none worked for me.

The only thing that does work is: :filetype detect so I remapped a quick shortcut :noremap <Leader>,ftd :filetype detect

Solution 4:

Sometimes syntax is OK, but all that is getting broken are foldings. In that case zx helps alot.

From VIM documentation:

zx

Update folds: Undo manually opened and closed folds: re-apply 'foldlevel', then do "zv": View cursor line. Also forces recomputing folds. This is useful when using 'foldexpr' and the buffer is changed in a way that results in folds not to be updated properly.