Fix syntax highlighting
From Vim Tips Wiki
Tip 269 Previous Next created 2002 · complexity basic · version 6.0
Contents |
[edit] Original tip (to be merged with following)
Summary:
- :help :syn-sync
- Search for 'sync' in your favorite syntax file in $VIMRUNTIME/syntax.
Long Version:
The syntax highlight code utilizes a certain synchronization method to efficiently figure out syntax highlighting, specifically if you aren't at the very beginning or end of a file. The specific setting is 'syntax sync'. For various file types the method is set by default in this is setup in the syntax file and one can vary the degree of trouble which VIM goes to to try and figure this out. As an example for C, from $VIMRUNTIME/syntax/c.vim:
if exists("c_minlines")
let b:c_minlines = c_minlines
else
if !exists("c_no_if0")
let b:c_minlines = 50 " #if 0 constructs can be long
else
let b:c_minlines = 15 " mostly for () constructs
endif
endif
exec "syn sync ccomment cComment minlines=" . b:c_minlines
Where c_minlines is the minimum number of lines that VIM goes backward to try to find the start of a comment for syntax highlighting. If that line which starts a comment is outside of that range, highlighting will appear wrong.
You can easily set up something like this in your vimrc:
let c_minlines=500
or even bigger, but realize that it is a performance trade-off and that syntax highlighting will slow things down.
[edit] Material from tip 454 (to be merged to this tip)
If your Vim syntax highlight keeps breaking as you move around your document this is the tip for you!
First of all, how to fix it: Put the following in your vimrc:
autocmd BufEnter * :syntax sync fromstart
[edit] Notes
Almost all syntax files do a :syntax clear which removes your preferred sync setting.
Using syntax sync fromstart gives the best syntax highlighting accuracy, but it may cause slow updates to the screen. This setting tells Vim to parse from the beginning of the file every time the screen needs updating. Long files and languages that involve complex highlighting rules will be adversely affected.
You may get accurate results with faster performance using something like:
syntax sync minlines=200
That tells Vim to start looking for synchronization points 200 lines before the current position in the file. Use a larger value if needed, and a smaller value to improve speed.
The problem occurs in recognizing large regions. Consider a large list of equations in LaTeX, say 100 of them, one per line. With the screen starting its display in the middle of the list, proper highlighting will occur only if Vim recognizes that it is in the middle of a list of equations. It needs to hunt backwards to do this. If minlines is too small, it won't see the \begin{eqnarray} and so highlighting will be incorrect. A large minlines value, or using fromstart, causes Vim to do a lot of unnecessary checking at every screen refresh.
[edit] Using a mapping
Is your syntax highlighting sometimes messed up? Try adding this to your vimrc:
noremap <F12> <Esc>:syntax sync fromstart<CR> inoremap <F12> <C-o>:syntax sync fromstart<CR>
Now you can press F12 to clean up most syntax highlighting problems. Sometimes, pressing Ctrl-L to redraw the screen helps.
