5.3 How do I get colors and syntax highlighting on a TTY?

Colors and faces are supported in non-windowed mode, i.e., on Unix and GNU/Linux text-only terminals and consoles, and when invoked as ‘emacs -nw’ on X, MS-DOS and MS-Windows. Emacs automatically detects color support at startup and uses it if available. If you think that your terminal supports colors, but Emacs won’t use them, check the termcap entry for your display type for color-related capabilities.

The command M-x list-colors-display pops up a window which exhibits all the colors Emacs knows about on the current display.

Syntax highlighting is also on by default on text-only terminals.

Emacs 26.1 and later support direct color mode in terminals. If Emacs finds Terminfo capabilities ‘setb24’ and ‘setf24’, 24-bit direct color mode is used. The capability strings are expected to take one 24-bit pixel value as argument and transform the pixel to a string that can be used to send 24-bit colors to the terminal.

Standard terminal definitions don’t support these capabilities and therefore custom definition is needed.

$ cat terminfo-custom.src

xterm-emacs|xterm with 24-bit direct color mode for Emacs,
  use=xterm-256color,
  setb24=\E[48\:2\:\:%p1%{65536}%/%d\:%p1%{256}%/%{255}%&\
     %d\:%p1%{255}%&%dm,
  setf24=\E[38\:2\:\:%p1%{65536}%/%d\:%p1%{256}%/%{255}%&\
     %d\:%p1%{255}%&%dm,

$ tic -x -o ~/.terminfo terminfo-custom.src

$ TERM=xterm-emacs emacs -nw

Emacs 27.1 and later support Terminfo capability ‘RGB’ for detecting 24-bit direct color mode. Multiple standard terminal definitions support this capability.

$ TERM=xterm-direct infocmp | grep seta[bf]

  setab=\E[%?%p1%{8}%<%t4%p1%d%e48\:2\:\:%p1%{65536}%/\
     %d\:%p1%{256}%/%{255}%&%d\:%p1%{255}%&%d%;m,
  setaf=\E[%?%p1%{8}%<%t3%p1%d%e38\:2\:\:%p1%{65536}%/\
     %d\:%p1%{256}%/%{255}%&%d\:%p1%{255}%&%d%;m,

$ TERM=xterm-direct emacs -nw

If your terminal is incompatible with XTerm, you may have to use another TERM definition. Any terminal whose name includes ‘direct’ should be a candidate. The toe command can be used to find out which of these are installed on your system:

$ toe | grep '\-direct'

konsole-direct  konsole with direct-color indexing
vte-direct      vte with direct-color indexing
st-direct       st with direct-color indexing
xterm-direct2   xterm with direct-color indexing (old)
xterm-direct    xterm with direct-color indexing

If Terminfo database is not available, but 24-bit direct color mode is supported, it can still be enabled by defining the environment variable COLORTERM to ‘truecolor’.

Terminals with ‘RGB’ capability treat pixels #000001 - #000007 as indexed colors to maintain backward compatibility with applications that are unaware of direct color mode. Therefore the seven darkest blue shades may not be available. If this is a problem, you can always use custom terminal definition with ‘setb24’ and ‘setf24’.