Was wondering if anyone knew of a way to go into the .emacs file and add an attribute to change the color of a specific string in all files it appears. The specific example I wish to use this for would be for any given file, if the string "TODO" exists, make the color of that text red. I want that string to stand out in my java, python, c++, etc. files so I can pick them out when easily looking around.

  • 1
    See the manual and wiki on adding font lock keywords.
    – Dan
    Nov 21 '16 at 20:22
  • What @Dan said. Use syntax-based (aka font-lock) highlighting, and add it via a mode hook. That will take care of any buffer in that mode.
    – Drew
    Nov 21 '16 at 21:15

A quick example would be this, which adds the font-lock-warning-face to all regex matches in all modes that derive from prog-mode:

(add-hook 'prog-mode-hook '(lambda ()
        nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|REFACTOR\\):" 1 font-lock-warning-face t)))))

Further reading with examples in the emacs wiki: https://www.emacswiki.org/emacs/AddKeywords

  • Should this work on Java files as well?
    – Michael
    Nov 22 '16 at 15:27
  • If the java-mode you use is derived from the prog-mode and calls the prog-mode-hook, then yes. Otherwise try to change the 'prog-mode-hook to 'java-mode-hook. If the java-mode is done well it should call this hook upon creation of the java-mode buffer. Nov 22 '16 at 15:38
  • Ok and the 'java-mode is something that comes standard in emacs or that's something I would have to implement myself?
    – Michael
    Nov 22 '16 at 15:43
  • Emacs comes with java-mode -- if you just try opening a .java file it should be activated. Java mode does derive from prog-mode so the suggestion should work fine for you.
    – glucas
    Dec 22 '16 at 16:44

If this is specifically about TODO-style comments, there are various packages available from MELPA that provide highlighting and related commands such as jumping to the next/previous such comment.

For example:

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.