In atom, C++ files are well colored unlike emacs cpp-mode. Emacs does not highlight references to member variables, function calls or member functions. They all are uncolored.

The left is from emacs and the other is from atom:

Syntax coloring in emacs Syntax coloring in atom See, emacs does not syntax-color for variables and functions apart from their declarations.

Emacs does not utilize a semantic syntax highlighting system, neither does atom. I think such highlighting can be easily added using regexp like in atom.

Atom, for example, looks for parantheses to distinguish function calls from variables. Similarly it looks for a dot to distinguish member variables from the others.

I would like to add these coloring rules to emacs, however, I am just a beginner who does not know regexp and font-lock-mode.

Could you help me with this issue?

  • Some screenshots would be nice... not everyone is familiar with Atom.
    – PythonNut
    Commented Sep 19, 2015 at 14:45
  • @PythonNut just added the screenshots
    – Hckr
    Commented Sep 19, 2015 at 14:59
  • ([a-z,A-Z,_]+[_]*[a-z,A-Z,1-9]*)+[(] seems okay for function calls. But I do not know how to add it as a new rule for cpp-mode
    – Hckr
    Commented Sep 19, 2015 at 18:11

3 Answers 3


The Emacs one is actually better. Here's why, The purpose of syntax highlighting in text editors is not being pretty, but to make important code structures stand out.

If you look at the Emacs sample, you'll see 'MyClass' being colored in the 'type name' color and 'obj' in 'variable' color, which makes the important information that you have one variable 'obj' of type 'MyClass' in the current scope really stand out.

If you happen to have other local variables, member variables or function parameters, you'll see them colored in similar way. I personally think this helps a lot because I can easily see where are these variables declared and where are they instantiated in a glance.

The logic behind Emacs' syntax highlighting is that it doesn't highlight where you reference(use) symbols, instead, it only highlights where symbols are defined or declared. These are much more important and informative then symbols being used. Let's face it, other than declarations and keywords, almost everything else is some symbols being referenced. Coloring these only makes the screen messier and the really important things less obvious.

  • 1
    This must have been the best written "first post" in the history of Stack Exchange! I tip my hat! Welcome! Commented Feb 12, 2016 at 12:20
  • I dig your comment, however, I find function calls inside other functions something that is worth highlighting but none of the emacs packages I came across does that.
    – SFbay007
    Commented Jan 14, 2020 at 17:53
  • 1
    this is purely an opinion and does not answer the question.
    – 12Me21
    Commented Dec 15, 2020 at 21:21
  • I don't see why this gets the highest vote, the question asks help to 'add' colors, not to change everything over to that like atom. You can have all those declarations color-coded in addition to the functions and variables being colored as well. Knowing easily where objects are declared is helpful, and knowing easily the sequences of operations is not less helpful either.
    – nougako
    Commented Feb 23, 2021 at 16:45

this seems to answer the member function bit of your question

(font-lock-add-keywords 'c++-mode
   "\\<[_a-zA-Z][_a-zA-Z0-9]*\\>"       ; Object identifier
   "\\s *"                              ; Optional white space
   "\\(?:\\.\\|->\\)"                   ; Member access
   "\\s *"                              ; Optional white space
   "\\<\\([_a-zA-Z][_a-zA-Z0-9]*\\)\\>" ; Member identifier
   "\\s *"                              ; Optional white space
   "(")                                 ; Paren for method invocation
   1 'font-lock-function-name-face t)))

source: https://www.reddit.com/r/emacs/comments/27eqwm/highlighting_c_member_function_calls/


Adding this to my init.el gives fairly good syntax highlighting for function calls and operators, it doesn't handle members though.

This highlights:

  • operators + - ... = * .. etc.
  • brackets [{()}] (as delimiters), as well as ;:
  • function_call().

Using simple regex, I find this gives nicer highlighting for C like languages.

;; C-Like
(dolist (mode-iter '(c-mode c++-mode glsl-mode java-mode javascript-mode rust-mode))
    '(("\\([~^&\|!<>=,.\\+*/%-]\\)" 0 'font-lock-operator-face keep)))
    '(("\\([\]\[}{)(:;]\\)" 0 'font-lock-delimit-face keep)))
  ;; functions
    '(("\\([_a-zA-Z][_a-zA-Z0-9]*\\)\s*(" 1 'font-lock-function-name-face keep))))
  • Can you please add a brief explanation of the three main components of the above code? E.g. what they are for etc.
    – nougako
    Commented Feb 23, 2021 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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