How to exclude C ++ source string literals from syntax highlighting in Vim?

Honestly, raw string literals is a great addition to the C ++ language. But (as expected) itโ€™s not easy for editors to correctly display these literals.

I am using Vim 7.4, and the original string literals completely break the syntax highlighting. For example, in

char const txt[] = R"(printf(")"; 

2nd '(' highlighted in red in vim.

Sort of

 char const txt2[] = R"( "{{" )"; 

breaks the selection of braces and is based on the syntax auto-ident - etc.

For starters, I would be happy if Vim ignored everything between R"( )" when syntax highlighting.

But note that string string literals are flexible - arbitrary matching strings are allowed between the first / last double double carriage / brace, for example.

 R"abcd()")")abcd" 

also a valid string string literal that encodes

  ) ")" 

See also the cppreference link for a general syntax definition.

So my question is how to configure Vim so that C ++ string literals are correctly recognized.

Vim already has some possibilities for the correct selection of fragments of the synatx language embedded in the host language (for example, for the source files of the compiler-compiler). Perhaps they can also be used for raw string literal?

+7
source share
3 answers

Just add cpp-vim as a plugin. I have added strict support for new string literals in pull-request # 14 .

This is what you get: http://bl.ocks.org/anonymous/raw/9442865

cpp-vim adds support for other C ++ 11 stuff.

+5
source

Add this

 syntax match cString 'R"\([^(]*\)(\_.*)\1"' 

into your own C ++ syntax file (usually ~/.vim/syntax/cpp.vim ; create this file if you don't have one).

+7
source

A small correction to the syntax rule above:

 syntax match cString 'R"\([^(]*\)(\_.\{-})\1"' 

The original tries eagerly to choose the longest match; so if you have several raw lines in a file (using the same open / close pattern), this will break. This one is not greedy, and should fit correctly. Thank you so much for the original, it really helped me!

0
source

All Articles