1

I have set up some custom colors for various faces in Emacs. They mostly look like this:

(custom-set-faces
 ; ...
 '(font-lock-constant-face
   ((((type tty)) (:foreground "bright magenta"))
    (t (:foreground "#6c71c4"))))
 ; ...
 )

When running in a terminal, Emacs used to use the basic 16-color escape codes for these colors. For example, the above would cause Emacs to issue the escape sequence ^[[95 to set the color to whatever the terminal had defined as "bright magenta".

Now, however, Emacs tries to detect whether the terminal supports 24-bit colors or 256 color mode; if so, it uses its own idea of what "bright magenta" should be (RGB triple (65535 0 65535)) and picks the closest 24-bit color or 256-color-palette entry to it.

For various reasons⁰, I would like to tell Emacs to use the older 16 color escape codes, even when more color options are available. I'd also prefer not to do it by messing with the termcap/terminfo definitions for my terminal emulator.

Is there a way to get the behavior I want from Emacs, using Emacs's configuration files (or command line)?

I'm on Debian 12, with Emacs 28.2 and xterm 379.


⁰In some cases, I use a set of nested terminal emulators, with Emacs running in TTY mode inside them. The nesting interferes with 24-bit color in such a way that Emacs will think it's available, but I don't end up with the right colors being displayed. Since 16-color escape sequences work properly, I'm hoping to sidestep the nested emulator problem for the moment by restricting Emacs's behavior.

3
  • Does TERM=xterm emacs not work? (or whatever the 16-colour version of your terminal is)
    – rpluim
    Jul 12, 2023 at 15:29
  • That does not work. I still get Emacs using more advanced color escape codes than I want. Even TERM=xterm-16color COLORTERM= emacs doesn't work.
    – asciiphil
    Jul 12, 2023 at 16:10
  • @asciiphil: Strange, it works on my machine ...
    – shynur
    Jul 12, 2023 at 16:46

1 Answer 1

1

The documentation indicates there is a command-line flag --color=mode that you can use ( https://www.gnu.org/software/emacs/manual/html_node/emacs/Colors-X.html ) :

Use color mode for num colors. If num is -1, turn off color support (equivalent to ‘never’); if it is 0, use the default color support for this terminal (equivalent to ‘auto’); otherwise use an appropriate standard mode for num colors. Depending on your terminal’s capabilities, Emacs might be able to turn on a color mode for 8, 16, 88, or 256 as the value of num. If there is no mode that supports num colors, Emacs acts as if num were 0, i.e., it uses the terminal’s default color support mode.

If mode is omitted, it defaults to ansi8.

I expect this to work for your nested terminal use-case since it's entirely emacs-dependent (unless the terminals themselves support fewer colors I suppose).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.