6 Jan 2012 redi   » (Master)

Vim syntax highlighting for C++11 raw strings



A little over a year ago I mentioned a vim syntax file for C++0x which I was using for a while, but it only adds support for some new keywords, not new syntax such as lambda expressions and list-initialization. Last month I found cpp11.vim in the Vim online script database, which intends to support the new C++11 syntax, by replacing your existing cpp.vim entirely.

Not quite satisfied with either of those, I've started using my own cpp.vim to extend the default cpp.vim file (which is done by putting it in ~/.vim/after/syntax/cpp.vim) with syntax highlighting for the new keywords and also C++11 raw strings.

With my colour settings they look like this:
  
char raw[] = R"#(s/"\([^"]*\)"/'\1'/g)#";
which is equivalent to:
  
char yuk[] = "s/\"\\([^\"]*\\\"/'\\1'/g";
only without suffering from leaning toothpick syndrome.

The # in the example can be (almost) any sequence of zero or more characters acting as a delimiter. The example above requires a non-empty delimiter because the string contains )", which would be taken as the end of the raw string otherwise:
  
char raw[] = R"(s/"\([^"]*\)"/'\1'/g)"; // ERROR!


You could choose to use "" as the delimiter sequence, as a nod to Python
  
char raw[] = R"""(triple-quoted for great justice)""";

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!