Emacs colors based on $TERM environment variable

colorsemacsgnu-screenterminaltmux

I want to know if it is possible to make emacs, when running in a gnu screen or tmux session, use the same colors as when TERM=xterm. In a gnu screen or tmux session, TERM=screen and emacs uses a different set of colors as compared to when TERM=xterm.

For example, when TERM=xterm-256color, the foreground color of font-lock-comment-face is 'Firebrick'. But when TERM=screen-256color, it is set to 'chocolate1'.

For tmux to work properly, the TERM variable must be set to screen or some derivative, so resetting TERM is not an option for me.

Best Answer

The definition of font-lock-comment-face includes many variants for cases of varying color support.

  • The chocolate1 variant is used when there are at least 88 colors available and the “background” is “dark”.
  • The Firebrick variant is used when there are at least 88 colors available and the “background” is “light”.

The difference is caused by some code that gives special meaning to TERM values that start with xterm, rxvt, dtterm, and eterm: it considers them to have a “light” background.

You should be able to customize frame-background-mode* to its light value to always use the “light” color variants.

If your Emacs is not new enough to have its own term/screen.el, then you will also need to adapt one for yourself that makes the appropriate color-adjusting calls. You might use the one from Emacs trunk, or I have a Gist that includes a .emacs.d/lisp/term/screen.el (with some extra bits to recognize the modifier+arrows/Home/End sequences provided by tmux’s xterm-keys option), and the .emacs modification needed to let it automatically load.

Of course, there could still be other bits of code that directly check the TERM value and do something different for screen- and xterm-like values…

* M-x customize-variable frame-background-mode

Related Question