I have 2 rules and the second one highlighting text disables the first...

(font-lock-add-keywords 'org-mode '(("^rtfm .*$" 0 '(:foreground "red"))))
(font-lock-add-keywords 'org-mode '(("'.*'"      0 '(:background "OrangeRed"))))

rtfm ... works
and 'this' works
but when I use them together only the second works.
rtfm whatever but then 'xx' and now only 'xx' is highlighted

This maybe sloppy, any tips are appreciated. (quote matching simplified here)

1 Answer 1


Try this:

(font-lock-add-keywords 'org-mode '(("^rtfm .*$" 0 '(:foreground "red"))))
(font-lock-add-keywords 'org-mode '(("'.*'"      0 '(:background "OrangeRed") 'keep)))

See the Elisp manual, node Search-based Fontification - look for keep:


In this kind of element, SUBEXP-HIGHLIGHTER is a list which specifies how to highlight matches found by MATCHER. It has the form:


The CAR, SUBEXP, is an integer specifying which subexpression of the match to fontify (0 means the entire matching text). The second subelement, FACESPEC, is an expression whose value specifies the face, as described above.

The last two values in SUBEXP-HIGHLIGHTER, OVERRIDE and LAXMATCH, are optional flags. If OVERRIDE is t, this element can override existing fontification made by previous elements of font-lock-keywords. If it is keep, then each character is fontified if it has not been fontified already by some other element. If it is prepend, the face specified by FACESPEC is added to the beginning of the font-lock-face property. If it is append, the face is added to the end of the font-lock-face property.

If you want both faces used for the text that both regexps match then use prepend or append instead of keep. That will combine the OrangeRed background with the Red foreground. (It's not clear from your question just what behavior you really want.)

  • Permutations and combinations... emacs -q many many times. If regexps matching overlap... it does not work. Maybe I need a matching function that can go back to beginning of line (where other matches may have already occurred). Thank you for the help, the documentation for font-lock-add-keywords does refer to font-lock-keywords that documents all that (what you pasted above).
    – 9mjb
    Commented Nov 23, 2020 at 14:55
  • This makes both work... but not when overlapping. It does not do foreground AND background (when overlapping) -- (font-lock-add-keywords 'org-mode '(("^rtfm .*" 0 '(:foreground "red") )) 'keep) (font-lock-add-keywords 'org-mode '(("'.*'" 0 '(:background "green") )) 'keep)
    – 9mjb
    Commented Dec 4, 2021 at 23:01
  • 1
    If you want to combine the faces you can use append or prepend, just as @Drew write in his answer. Commented Jan 17, 2022 at 17:53
  • @Lindydancer : do I understand it right that without clear integer based priorities append/prepend will work (does it? have you tested it?) for maximum of three different overlapping faces: prepended properties applied first, than already before added properties (middle of the list), then append as third will be applied to the final outcome? Why not provide an answer with an example code and an image demonstrating how it works and proving that it works? Too much effort?
    – oOosys
    Commented May 12, 2023 at 19:18
  • 1
    @Claudio, yes, I have tested it several times. In fact, I've written several packages and a suite of tools for font-lock where I've used this extensively. An answer like you suggested would have a pedagogical value, feel free to write one. Commented May 13, 2023 at 6:54

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.