if I write a function in the following way:

int foo(int bar)

Sublime highlights foocorrectly. However, in this case:

int foo
    (int bar)

foo loses all highlighting.

One of the things I love about C++ is that it does not care about whitespace. Is there a way I can make sublime hightlight this code correctly?


After getting some pointers and browsing the defualt syntax files, this:

(?:  ^                                 # begin-of-line
     (?: (?= \s )           (?&lt;!else|new|return) (?&lt;=\w)      #  or word + space before name
       | (?= \s*[A-Za-z_] ) (?&lt;!&amp;&amp;)       (?&lt;=[*&amp;&gt;])   #  or type modifier before name
(\s*) (?!(while|for|do|if|else|switch|catch|enumerate|return|r?iterate)\s*\()
    (?: [A-Za-z_][A-Za-z0-9_]*+ | :: )++ |                  # actual name
    (?: (?&lt;=operator) (?: [-*&amp;&lt;&gt;=+!]+ | \(\) | \[\] ) )  # if it is a C++ operator

seems to be the regex that's malfunctioning. However, I can't understand why it's not getting my code; the last line, \s*(?=\(), should match any amount of whitespace or newlines before the opening paren, no?


Sublime Text's syntax highlighting is fully configurable. You can create a copy of default C++ syntax highlighting rules, improve them and tell ST3 to use your improved version.

ST3 uses Textmate-compatible syntax highlighting rules, described here. If you don't like working directly on Plists, you can use JSON or some other formats and then automatically convert them to Plist with AAAPackageDev (easily installable with Package Control).

Default C++ package (which includes syntax highlighting rules) is located at C:\Program Files\Sublime Text 3\Packages\C++.sublime-package. Your custom packages can be placed in C:\Users\%USERNAME%\AppData\Roaming\Sublime Text 3\Packages\User. The easiest way to get there is through ST's menu: Preferences → Browse Packages.

| improve this answer | |
  • Thanks, I also found that and played around with it after reading docs.sublimetext.info/en/latest/extensibility/syntaxdefs.html. What stopped me then however was that the defualt C++ tmLanguage file is a giant beast, and I don't really know how to approach it to to get the desired result. Could you point me in the direction of what I should change there? – Ludwik May 10 '15 at 8:59
  • Sorry, I'm not familiar with that specific file too. I can give you one tip: press Ctrl+Shift+Alt+P (on Windows, other OSes use different shortcuts) with cursor on function name. Currently used highlighting styles will appear in statusbar. These are used to set colors in syntax highlighting rules, so you may look for them in Plists to locate sections you need a bit quicker. – gronostaj May 10 '15 at 9:40
  • Thank you! Another place suggested that thing, but gave the shortcut Ctrl+Shift+P, which didn't work for me. I might be able to take it from here on. – Ludwik May 10 '15 at 13:31

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.