Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=269
 
|id=269
 
|previous=268
 
|previous=268
 
|next=270
 
|next=270
|created=June 28, 2002
+
|created=2002
 
|complexity=basic
 
|complexity=basic
|author=Douglas Potts
+
|author=
|version=5.7
+
|version=6.0
 
|rating=38/18
 
|rating=38/18
 
|category1=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
  +
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 <code>:syntax sync</code> 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, <code>$VIMRUNTIME/syntax/python.vim</code>) uses <code>:syntax sync</code> to specify the synchronization method to be used for a particular file type.
Summary:
 
*{{help|:syn-sync}}
 
*Search for 'sync' in your favorite syntax file in $VIMRUNTIME/syntax.
 
   
  +
==Highlight from start of file==
Long Version:
 
  +
For the most accurate but slowest result, set the syntax synchronization method to <code>fromstart</code>. This can be done with an autocmd in your [[vimrc]]:
 
  +
<pre>
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:
 
  +
autocmd BufEnter * :syntax sync fromstart
  +
</pre>
   
  +
==Highlight from an amount backwards==
  +
Vim supports highlighting synchronization by searching a variable amount backwards from the current position for a recognized syntax state. An example for C, from <code>$VIMRUNTIME/syntax/c.vim</code>, is:
 
<pre>
 
<pre>
 
if exists("c_minlines")
 
if exists("c_minlines")
Line 33: Line 34:
 
</pre>
 
</pre>
   
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.
+
Where <code>c_minlines</code> 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:
  +
<pre>
 
let c_minlines=500
  +
</pre>
   
  +
Larger values improve accuracy, but slow down syntax highlighting.
You can easily set up something like this in your .vimrc:
 
   
  +
Rather than using <code>fromstart</code> syntax highlighting, you may get accurate but faster results with something like:
let c_minlines=500
 
  +
<pre>
  +
syntax sync minlines=200
  +
</pre>
   
  +
==Using a mapping to fix when broken==
or even bigger, but realize that it is a performance trade-off and that syntax highlighting will slow things down.
 
  +
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:
  +
<pre>
  +
:syntax sync fromstart
  +
</pre>
  +
  +
If wanted, mappings can be defined to make entering the command easier:
  +
<pre>
  +
noremap <F12> <Esc>:syntax sync fromstart<CR>
  +
inoremap <F12> <C-o>:syntax sync fromstart<CR>
  +
</pre>
  +
  +
Now you can press F12 to clean up most syntax highlighting problems. Sometimes, pressing Ctrl-L to redraw the screen helps.
  +
  +
==References==
 
*{{help|:syn-sync}}
 
*Search for 'sync' in your favorite syntax file in the <code>$VIMRUNTIME/syntax</code> directory (use <code>:echo expand('$VIMRUNTIME/syntax')</code> to see its location).
   
 
==Comments==
 
==Comments==
  +
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 <code>syntax/c.vim</code> 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. [[User:JohnBeckett|JohnBeckett]] 10:06, May 1, 2011 (UTC)
If you are not worried about the speed tradeoff -- and you just want the problem solved (and a brief explanation of why your syntax highlighting was broken in the first place) -- check out [[VimTip454]]
 
   
  +
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.
----
 

Revision as of 12:30, 15 July 2012

Tip 269 Printable Monobook Previous Next

created 2002 · complexity basic · version 6.0


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.

Highlight from start of file

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 backwards

Vim supports highlighting synchronization by searching a variable amount backwards from the current position for a recognized syntax state. An example for C, from $VIMRUNTIME/syntax/c.vim, is:

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

syntax sync minlines=200

Using a mapping to fix when broken

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.

References

  • :help :syn-sync
  • Search for 'sync' in your favorite syntax file in the $VIMRUNTIME/syntax directory (use :echo expand('$VIMRUNTIME/syntax') to see its location).

Comments

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.