9

C++14 provided us with the option of using single quotes to separate the digits of large numeric literals. This breaks Vim's syntax highlighting.

For example, consider the following C++14 program:

#include <stdlib.h>

int main(int argc, char** argv) {
  // small numbers are fine
  int normal_number = 5;

  // large numbers have the correct highlighting but they're hard to visually parse
  int huge_number = 1000000000;

  // apostrophes help with visual parsing but mess up syntax higlighting
  int huge_number_again = 1'000'000'000;

  return EXIT_SUCCESS;
}

When I view this in Vim, I see the following syntax highlighting (note that I am using some additional highlighting scripts):

Vim syntax highlighting for the program described above

I would like all of the digits in huge_number_again to appear red. Is there a modification I can make to one of my syntax files or to my ~/.vimrc to correct this?

  • I see some support for C++14 was added in v7.4.923 (binary literals). But not the number separator it seems :-/ The latest C++ files from here also doesn't seem to have it yet. – Martin Tournoij Mar 14 '16 at 6:17
  • 4
    For a quick fix, you can probably add syn match cNumber display contained "\d\(\d\|'\)*\(u\=l\{0,2}\|ll\=u\)\>" to ~/.vim/syntax/cpp.vim. (Btw, if that's C++, use cstdlib, not stdlib.h.) – muru Mar 14 '16 at 6:19
  • 3
    I just noticed there's an open issue since May 2014 btw. – Martin Tournoij Mar 14 '16 at 6:20
  • @muru That seems to have fixed the problem. I'm fine with using a quick hack for now. Thanks! – Jake Mar 14 '16 at 6:30
  • 2
    @Carpetsmoker I am not to sure of syntax matching to know exactly what my change does. I think it will just match a digit followed any number of digits or single quotes, plus the ull fluff, but I don't know if it will match some invalid case. And apparently C++14 allows the separator in octal, binary, hex literals and floats and after the decimal point in floats. That's a lot more than I'm willing to take on. – muru Mar 14 '16 at 7:46
4

The vim-jp/vim-cpp repo included this change in July 2016:

syn match   cNumber     display contained "\d\('\=\d\+\)*\(u\=l\{0,2}\|ll\=u\)\>"
syn match   cNumber     display contained "0x\x\('\=\x\+\)*\(u\=l\{0,2}\|ll\=u\)\>"
syn match   cNumber     display contained "0b[01]\('\=[01]\+\)*\(u\=l\{0,2}\|ll\=u\)\>"

If you don't want to use this repo, you can add these lines to ~/.vim/syntax/cpp.vim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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