Ad blocker interference detected!
Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers
Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.
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,
:syntax sync to specify the synchronization method to be used for a particular file type.
Highlight from start of fileEdit
For the most accurate but slowest result, set the syntax synchronization method to
fromstart. This can be done with an autocmd in your vimrc:
autocmd BufEnter * :syntax sync fromstart
Highlight from an amount backwardsEdit
Vim supports highlighting synchronization by searching a variable amount backwards from the current position for a recognized syntax state. An example for C, from
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
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:
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:
syntax sync minlines=200
Using a mapping to fix when brokenEdit
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:
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.
- :help :syn-sync
- Search for 'sync' in your favorite syntax file in the
:echo expand('$VIMRUNTIME/syntax')to see its location).
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)