Next: , Previous: Deleting menus and menu options, Up: Common requests

5.44 How do I turn on syntax highlighting?

font-lock-mode is the standard way to have Emacs perform syntax highlighting in the current buffer. It is enabled by default in Emacs 22.1 and later.

With font-lock-mode turned on, different types of text will appear in different colors. For instance, in a programming mode, variables will appear in one face, keywords in a second, and comments in a third.

To turn font-lock-mode off within an existing buffer, use M-x font-lock-mode <RET>.

In Emacs 21 and earlier versions, you could use the following code in your .emacs file to turn on font-lock-mode globally:

     (global-font-lock-mode 1)

Highlighting a buffer with font-lock-mode can take quite a while, and cause an annoying delay in display, so several features exist to work around this.

In Emacs 21 and later, turning on font-lock-mode automatically activates the new Just-In-Time fontification provided by jit-lock-mode. jit-lock-mode defers the fontification of portions of buffer until you actually need to see them, and can also fontify while Emacs is idle. This makes display of the visible portion of a buffer almost instantaneous. For details about customizing jit-lock-mode, type C-h f jit-lock-mode <RET>.

In versions of Emacs before 21, different levels of decoration are available, from slight to gaudy. More decoration means you need to wait more time for a buffer to be fontified (or a faster machine). To control how decorated your buffers should become, set the value of font-lock-maximum-decoration in your .emacs file, with a nil value indicating default (usually minimum) decoration, and a t value indicating the maximum decoration. For the gaudiest possible look, then, include the line

     (setq font-lock-maximum-decoration t)

in your .emacs file. You can also set this variable such that different modes are highlighted in a different ways; for more information, see the documentation for font-lock-maximum-decoration with C-h v (or M-x describe-variable <RET>).

Also see the documentation for the function font-lock-mode, available by typing C-h f font-lock-mode (M-x describe-function <RET> font-lock-mode <RET>).

To print buffers with the faces (i.e., colors and fonts) intact, use M-x ps-print-buffer-with-faces or M-x ps-print-region-with-faces. You will need a way to send text to a PostScript printer, or a PostScript interpreter such as Ghostscript; consult the documentation of the variables ps-printer-name, ps-lpr-command, and ps-lpr-switches for more details.