Skip to content

Font Lock mode

Font Lock mode is a minor mode, always local to a particular buffer, which assigns faces to (or fontifies) the text in the buffer. Each buffer’s major mode tells Font Lock mode which text to fontify; for instance, programming language modes fontify syntactically relevant constructs like comments, strings, and function names.

Font Lock mode is enabled by default in major modes that support it. To toggle it in the current buffer, type M-x font-lock-mode. A positive numeric argument unconditionally enables Font Lock mode, and a negative or zero argument disables it.

Type M-x global-font-lock-mode to toggle Font Lock mode in all buffers. To impose this setting for future Emacs sessions, customize the variable global-font-lock-mode (see Easy Customization Interface), or add the following line to your init file:

emacs-lisp
(global-font-lock-mode 0)

If you have disabled Global Font Lock mode, you can still enable Font Lock for specific major modes by adding the function font-lock-mode to the mode hooks (see Hooks). For example, to enable Font Lock mode for editing C files, you can do this:

emacs-lisp
(add-hook 'c-mode-hook 'font-lock-mode)

Font Lock mode uses several specifically named faces to do its job, including font-lock-string-face, font-lock-comment-face, and others. The easiest way to find them all is to use M-x customize-group RET font-lock-faces RET. You can then use that customization buffer to customize the appearance of these faces. See Customizing Faces.

Fontifying very large buffers can take a long time. To avoid large delays when a file is visited, Emacs initially fontifies only the visible portion of a buffer. As you scroll through the buffer, each portion that becomes visible is fontified as soon as it is displayed; this type of Font Lock is called Just-In-Time (or JIT) Lock. You can control how JIT Lock behaves, including telling it to perform fontification while idle, by customizing variables in the customization group ‘jit-lock’. See Customizing Specific Items.

The information that major modes use for determining which parts of buffer text to fontify and what faces to use can be based on several different ways of analyzing the text:

  • Search for keywords and other textual patterns based on regular expressions (see Regular Expression Search).
  • Find syntactically distinct parts of text based on built-in syntax tables (see Syntax Tables in The Emacs Lisp Reference Manual).
  • Use syntax tree produced by a full-blown parser, via a special-purpose library, such as the tree-sitter library (see Parsing Program Source in The Emacs Lisp Reference Manual), or an external program.