Search code examples
emacsfont-lock

How to set emacs operator color?


How can I set font color for operators? I'm programing in C++, and I would like operators such as '+', '=', '!=', '<<' and such be colored as I wish.

I tried move the cursor onto an operator and 'M-x customize-face' but it takes me to 'all faces' by default. Which is the one I should edit?


Solution

  • I believe this is what you're looking for.

    ;;       * the name of our face *
    (defface font-lock-operator-face
      '((((class color)
           :background "darkseagreen2")))
      "Basic face for highlighting."
      :group 'basic-faces)
    
    ;; You'll have a hard time missing these colors
    (set-face-foreground 'font-lock-operator-face "red")
    (set-face-background 'font-lock-operator-face "blue")
    
    (font-lock-add-keywords 'c++-mode
      '(("\\(~^&\|!<>:=,.\\+*/%-]\\)" 0 'font-lock-operator-face)))