Fix syntax highlighting

Syntax highlighting allows files of a certain type (for example, Python programs) to have parts of the text highlighted to distinctively show keywords, comments, or other components. When editing, Vim can lose track of the syntax and may highlight text incorrectly. The :syntax sync command controls how Vim synchronizes the syntax state that should apply at a particular point in the text. The most accurate but slowest result occurs from having Vim always rescan the buffer from the start. Often the syntax file (for example, $VIMRUNTIME/syntax/python.vim) uses :syntax sync to specify the synchronization method to be used for a particular file type.

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 the line which starts a comment is outside that range, highlighting may be incorrect. Your vimrc can define this setting, for example:

let c_minlines=500

Larger values improve accuracy, but slow down syntax highlighting.

Rather than using fromstart syntax highlighting, you may get accurate but faster results with something like:

The procedures discussed above attempt to ensure that syntax highlighting is always correct, at the cost of slower performance. Another procedure is to tolerate occasional highlighting errors, and correct the problem when it arises by entering a command like:

:syntax sync fromstart

If wanted, mappings can be defined to make entering the command easier:

I have tweaked the wording, but haven't considered the content much. The autocmd is a bit extreme (applying to all files). It's not clear why the extract from syntax/c.vim is useful. When synching breaks for me, I find that scrolling up/down often fixes it, with no need for the more elaborate ideas here. This doesn't happen often, so I'm not sure what is actually needed, or whether anything helpful can be said about scrolling. JohnBeckett 10:06, May 1, 2011 (UTC)

Surprisingly, I've actually found :syntax sync fromstart to be faster than only reading back a few hundred lines. This is on a fairly long and complex HTML file with embedded CSS and lots of Javascript.