Obsolete tip
This tip has been merged into another tip.
See VimTip269 for the current tip.
Please do not edit this tip, and do not edit the discussion page.
If anything needs to be improved, please fix VimTip269.
created April 14, 2003 · complexity basic · author Robert (MetaCosm) · version 5.7
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"
Notes
- Vim syntax files should define the best syntax sync method for that langauge
- Many vim syntax files do not do #1
- Almost all syntax files do a :syntax clear which removes _your_ sync setting
The syntax files that tend to have the most trouble keeping sane highlighting are multilangauge files (html/javascript -- html/php/javascript ...), yet the all work perfectly with :syntax sync fromstart. This is not a vim bug, but a problem with the syntax files, yet, it has been argued that maybe vim should NOT clear out the sync setting when doing a syntax clear -- and just wait for it to be overwritten... like everything else -- that is debatable.
Comments
syn sync fromstart
makes for improved accuracy in syntax highlighting, but at a price: sluggish screen updating. This command is telling 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, moreso, of course, on older (slower) computers. Fair warning!
sluggish screen updates vs. constantly messed up syntax highlighting == easy choice
I was about to give up on vim entirely before I found out about this. Since vim is basically useless if everytime you page down or pageup it breaks you syntax highlighting.
Depends on how sluggish!
As a less sluggish alternative, something like
syn sync minlines=200
may be of help. That tells vim to start looking for synchronization points 200 lines previous in the file; use more if one needs to and fewer 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 its in the middle of a list of equations. It needs to hunt backwards to do this. If the minlines is too small, it won't see the \begin{eqnarray} and so highlighting will be incorrect. Set minlines too large or use the fromstart suggestion, and vim will be doing a lot of extra work that gets thrown away at every screen refresh.
On the other hand, if one has a multi-gigahertz machine, extra cpu work is cheap.
Even if you decide to use minlines, you still need to use an autobuf since they will be lost on the :syntax clear. Also, I was wondering if someone could how minlines effects languages that call other syntax files (does it effect all the active syntax highlighting methods).
Doesn't matter -- since I do have a multi-ghz machine, and generally work with sub-1000 line files -- so worring about preformance in my case is really silly.