Preface: VIM beginner, and I suspect this question will evolve significantly as I understand more about it (and VIM). Documenting the issue here as I was unable to find any other references to "substitution caused my syntax/highlighting to change". enter image description here Above is the normal syntax highlighting of my .Rmd file. After performing a substitution with :%s/foo_bar/bar_baz/gc the syntax highlighting occasionally changes (though I don't yet know how to replicate it on demand) to the picture below, with highlighted underscores and different highlighting for other text. This is clearly some different mode of highlighting, but I don't know what and I don't know how to easily revert it.

When the issue occurs, I seem to have normal VIM command functionality. When I save and exit VIM the normal syntax highlighting returns. enter image description here

This and this SE question only speak to how to override specific highlighting, but I need the highlighting to revert to the top picture.

Running VIM 8.2, GUI 32 bit on Windows.

  • 2
    When highlighting breaks, just do :e. It'll reload the buffer. As for fixing the problem: vim's syntax highlighting is a dragon best left alone. It's built with complex regex and will fry your eyebrows off if you so much as try to understand it. (Buf if you can fix it, by all means, have at it.)
    – 3N4N
    Commented Aug 3, 2022 at 9:02
  • 3
    You might try :syntax sync fromstart when this happens; I suspect the highlighter is getting out of sync (which is particularly common in deeply nested syntax or in syntaxes where one part of the file is one thing and another part is another thing, and delimiters are used to define which regions are which).
    – D. Ben Knoble
    Commented Aug 3, 2022 at 13:01
  • Following the comment by @D.BenKnoble, I have the following in my .vimrc to do this when I get this problem: nnoremap <F12> :syntax sync fromstart<CR> (you can choose a key other than <F12> if you like).
    – mattb
    Commented Aug 3, 2022 at 13:06


Your Answer

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

Browse other questions tagged or ask your own question.