Programming

Disable font-lock-warning-face Globally in Emacs (All Modes)

Prevent Emacs from applying font-lock-warning-face in any mode. Set font-lock-ignore or add a hook to strip keywords so the warning face isn't applied.

1 answer 1 view

How can I disable font-lock-warning-face globally in Emacs so that the face is not applied in any mode (rather than just removing its styles or making it “invisible”)? I’m using a plain/scratch Emacs with no preconfigurations or frameworks. The face appears like this:

elisp
(face (font-lock-warning-face) help-echo "Easy to misread; consider moving the element to the next line" fontified t)

What Emacs Lisp or init configuration should I add to prevent font-lock-warning-face from being applied across all modes?

To disable font-lock-warning-face globally in Emacs, tell Font Lock not to use that face by setting the ignore list — for example (setq font-lock-ignore '((t font-lock-warning-face))) in your init file. If your Emacs doesn’t provide font-lock-ignore, add a small Emacs Lisp hook that filters font-lock-keywords in each buffer so any keyword spec that would use font-lock-warning-face is removed.


Contents


Disable font-lock-warning-face globally (preferred)

If your Emacs supports the variable font-lock-ignore, this is the cleanest, simplest way to disable the face everywhere. The Emacs Lisp manual documents using font-lock-ignore to tell Font Lock not to use a particular face (the manual shows (setq font-lock-ignore '((prog-mode font-lock-warning-face))) — replace prog-mode with t to cover all buffers). See the manual for details: the Customizing Keywords node.

Add this near the top of your init file:

elisp
;; Preferred: prevent font-lock from ever using font-lock-warning-face
(when (boundp 'font-lock-ignore)
 ;; `t` means "all major modes"; change to 'prog-mode or another mode if you prefer.
 (setq font-lock-ignore '((t font-lock-warning-face)))
 ;; Optional: refresh Font Lock in already-open buffers (if supported in your Emacs)
 (when (fboundp 'font-lock-refresh-defaults)
 (font-lock-refresh-defaults)))

Why this works: Font Lock checks font-lock-ignore when it builds highlighting patterns. By listing (t font-lock-warning-face) you ask it not to ever use that face. That prevents the face from being applied at all (not merely hidden or remapped).


Fallback: remove font-lock-warning-face from font-lock-keywords (alternate disable method)

What if your Emacs doesn’t have font-lock-ignore, or some package adds keywords after font-lock-ignore is set? Then filter the buffer-local font-lock-keywords when Font Lock turns on, removing any keyword specs that mention font-lock-warning-face. This approach actually strips the keyword specs that would apply the face.

Drop this into your init (it’s conservative and runs per-buffer when font-lock-mode starts):

elisp
;; Fallback: filter out any font-lock keyword specs that mention font-lock-warning-face
(require 'cl-lib)

(defun my/font-lock--spec-mentions-face-p (spec face)
 "Return non-nil if SPEC mentions FACE. Walk lists and symbols safely."
 (let ((found nil))
 (cl-labels ((walk (x)
 (cond
 ((eq x face) (setq found t))
 ((consp x) (mapc #'walk x))
 (t nil))))
 (walk spec))
 found))

(defun my/remove-warning-face-from-font-lock-keywords ()
 "Remove keyword specs that would use `font-lock-warning-face' in the current buffer."
 (when (boundp 'font-lock-keywords)
 (let* ((kw font-lock-keywords)
 ;; If kw is a symbol holding the real list, use its value
 (kw (if (symbolp kw) (and (boundp kw) (symbol-value kw)) kw)))
 (when (listp kw)
 (let ((filtered
 (cl-remove-if (lambda (spec)
 (my/font-lock--spec-mentions-face-p spec 'font-lock-warning-face))
 kw)))
 (unless (eq filtered kw)
 (setq-local font-lock-keywords filtered)
 (when (fboundp 'font-lock-refresh-defaults)
 (font-lock-refresh-defaults))))))))

(add-hook 'font-lock-mode-hook #'my/remove-warning-face-from-font-lock-keywords)

A few practical notes on this fallback:

  • It tries to handle the most common font-lock-keywords shapes by walking the spec recursively and discarding any element that mentions the face.
  • It sets font-lock-keywords buffer-locally, then refreshes Font Lock so changes take effect.
  • Some modes compute keywords lazily or via functions; if you still see the face in a particular mode, run the filter again after that mode has set up its keywords (e.g. via after-change-major-mode-hook, or by wrapping the mode’s keyword-generating function).

This solution prevents the face from being applied rather than simply hiding or remapping it.


How to test disable font-lock-warning-face (verify it’s not applied)

Want a quick check? Add a small keyword that would normally use font-lock-warning-face and see whether it’s applied:

  1. Open any buffer (or use *scratch*).
  2. Evaluate:
elisp
;; Add a test keyword that would use font-lock-warning-face
(font-lock-add-keywords nil '(("\\<WARN\\>" . font-lock-warning-face)))
;; Re-fontify the buffer
(when (fboundp 'font-lock-refresh-defaults)
 (font-lock-refresh-defaults))
(font-lock-fontify-buffer)
  1. Insert the word WARN and look: if your configuration blocked the face, the word won’t be highlighted with the warning face.

Programmatic check (returns nil when the face wasn’t applied):

elisp
(with-temp-buffer
 (insert "WARN")
 (font-lock-mode 1)
 (font-lock-add-keywords nil '(("\\<WARN\\>" . font-lock-warning-face)))
 (font-lock-fontify-buffer)
 (goto-char (point-min))
 (get-text-property (point) 'face)) ;; => nil if the face was not applied

If you still see font-lock-warning-face, try restarting Emacs (or re-open the buffer) to ensure the font-lock-ignore/hook was loaded early enough.


Notes, caveats and compatibility

  • Emacs versions: font-lock-ignore is the simplest API when available. If boundp reports it’s not present in your Emacs, use the fallback hook above. (Check with (boundp 'font-lock-ignore).)
  • Packages that add keywords after font-lock-mode turns on may reintroduce specs that mention the face. If that happens, either:
  • Add an extra hook after the package loads and re-run the filter, or
  • Advise or wrap font-lock-add-keywords to ignore additions that use font-lock-warning-face.
  • This approach prevents the face from being applied. That’s different from remapping the face to look like the default (face remapping) or setting its attributes to be invisible. Those options keep the face property on text; the methods above remove the assignment itself.
  • Some code might rely on detecting font-lock-warning-face to find warnings. Removing the face changes that behavior. That’s intentional here, but keep an eye on any tooling that searches for that face.
  • For deep debugging, inspect font-lock-keywords and the font-lock implementation in the source; see font-lock.el for how patterns are applied. The implementation is readable at the Emacs mirror: https://github.com/emacs-mirror/emacs/blob/master/lisp/font-lock.el.
  • Official documentation: Font Lock overview and how faces/keywords work are covered in the main manual: Font Lock (GNU Emacs Manual) and Faces for Font Lock. The specific font-lock-ignore example is in the Customizing Keywords node.

Sources


Conclusion

To disable font-lock-warning-face globally, the recommended, minimal change is to set font-lock-ignore in your init: (setq font-lock-ignore '((t font-lock-warning-face))). If your Emacs doesn’t expose that variable, use the fallback hook that filters font-lock-keywords in each buffer to strip any specs that would apply font-lock-warning-face. Either way, those methods actually prevent the face from being applied (not just hidden), so they implement the “disable font-lock-warning-face emacs” behavior you asked for.

Authors
Verified by moderation
Moderation
Disable font-lock-warning-face Globally in Emacs (All Modes)