Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #454 - Fix syntax highlighting so it keeps working

Created: April 14, 2003 8:22 Complexity: basic Author: Robert (MetaCosm) Version: 5.7 Karma: 71/30 Imported from: Tip#454

If you 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 (_vimrc on windows): 
"autocmd BufEnter * :syntax sync fromstart" 


now, you might say, "Robert, I already put 'syntax sync fromstart' in my .vimrc, and it doesn't work!" -- and in saying that, you would be correct, because of a few factors coming together to cause you pain.


Factors:

1. Vim syntax files should define the best syntax sync method for that langauge 
2. Many vim syntax files do not do #1 
3. Almost all syntax files do a :syntax clear which removes _your_ sync setting 


Notes:

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 :) 


Hope this helps you... join #vim on irc.freenode.net for all your vim help needs :)

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!


cec--AT--NgrOyphSon.gPsfAc.nMasa.gov , April 16, 2003 8:32


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.

private , April 16, 2003 12:02


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.


cec--AT--NgrOyphSon.gPsfAc.nMasa.gov , April 17, 2003 8:58


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.

Anonymous , April 21, 2003 14:04


on last comment :%s/could/could explain :)

Anonymous , April 21, 2003 14:06


Advertisement