Question

Quite honestly, raw string literals are a great addition to the C++ language. But (as expected) editors have a hard time to properly display those literals.

I am using Vim 7.4 and out-of-the-box raw string literals completely break the syntax highlighting. For example in

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

the 2nd '(' is highlighted red in vim.

Something like

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

breaks the highlighting of curly braces and the syntax based auto-ident - and so on.

For a start I would be happy to have Vim ignore everything between R"( and )" when doing syntax highlighting.

But note that raw string literals are flexible - arbitrary matching strings are allowed between the first/last double-quote/brace pair, e.g.

R"abcd()")")abcd"

is also a valid raw string literal which encodes

)")"

See also the cppreference link for a general definition of the syntax.

Thus my question how to configure Vim such that C++ raw string literals are properly recognized.

Vim already seems to include some facilities to properly synatx highlight language fragments embedded in a host language (e.g. for compiler-compiler source files). Perhaps they can be used for the raw string literal case as well?

Was it helpful?

Solution 2

Just add cpp-vim as a plugin. I have added strict support for newer 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 too.

OTHER TIPS

Add this

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

to your custom C++ syntax file (normally ~/.vim/syntax/cpp.vim ; create this file if you don't have one).

A tiny tweak on the above syntax rule:

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

The original attempts to greedily select the longest match; so if you have multiple raw strings in a file (using the same open/close pattern) is would break. This one is non-greedy, and should match correctly. Thank you so much for the original though, it was a huge help to me!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top