I generally want font-locking only for comments and strings. This would seem easy to do: customize the relevant font lock faces to make them identical to default. That unfortunately is not satisfactory, because many other places which actually benefit from some highlighting (dired, org, probably magit) reuse the font lock faces.

The next best solution seems to be adding some buffer-local face remaps in prog-mode buffers. I've been using this for some time, and it mostly works fine.

One problem with this approach is that the font lock colors "leak" into other buffers that copy the original buffer's content (since my face remaps are buffer local). This is particularly bad for Swiper, consult-lines and the like, since the superposition of match highlights with the font locking becomes quite disconcerting (to me at least).

Is there a (simple, major-mode indepedent) way to tell font-lock-mode to actually not add any face properties except for those I want?

  • 1
    Interesting. What you want is only what's called Syntactic Font Lock - no search-based font-lock. I don't see documented a way to get this, but perhaps you can get it by setting font-lock-keywords to nil. Or hopefully someone will provide a simple answer. (The opposite, getting only search-based and not also syntactic (strings, comments), is easily obtainable.) – Drew Jan 21 at 19:07
  • I think unsetting font-lock-keywords is the way to do it! It also removes a few nice little things, like the distinction of docstring from regular ones and f-strings in Python, but this can be fixed by scrutinizing the content of font-lock-keywords. – Augusto Jan 22 at 13:52
  • Please post that as an answer, then. And you can accept your own answer. Comments can be deleted at any time. Q & A need to stand on their own - only Q & A are searchable and findable by googling - not comments. Thx. – Drew Jan 22 at 16:52

Following Drew's suggestion in the comments, here's a simple solution that can also be adapted to more specific needs:

(defun boring-font-lock ()
  "Activate font lock only for strings and comments."
  (setq-local font-lock-keywords nil))

(add-hook 'prog-mode-hook 'boring-font-lock)

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.