I'm editing a ~1000 line Python file containing long docstrings. Occasionally, when I'm in editing the middle of a file, the syntax highlighting will 'invert' so that everything outside the docstrings is highlighted as if it were a docstring, and the contents of the docstrings themselves are highlighted as Python code.

It's as if the highlighting algorithm only considers a small local region around the current editing area for its algorithm, and it always assumes the start of that region is code, even if that region happens to begin inside a docstring.

Other colleagues have the same issue when they edit this file too, and we don't have anything special or nonstandard in our vimrcs.


1 Answer 1


Because of the way Vim syntax highlighting works, it can sometimes lose track of its context, causing issues such as the one you describe. You can check if this is the issue by running the command:

:syntax sync fromstart

This will cause Vim always to read all the way back to the start of the file when highlighting. This can be slow, so if this fixes the issue, you might like to investigate the other options: Vim offers several different methods to determine how far back to read when highlighting.

These are described at :help :syn-sync.

If you want to check what sync values Vim is currently using, you can use the bare command:

:syntax sync

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.