3

I would like for tmux to automatically pick up 256-color mode, i.e. have an environment variable

TERM=tmux-256color

inside tmux when the environment variable

TERM=xterm-256color

was set outside.

Default behaviour

256-color terminal emulator

The current behaviour is for tmux to pick 8 color mode in any case: use a terminal emulator that supports 256 colors, i.e. Gnome Terminal or xterm, and make sure it supports 256 colors

echo $TERM; tput colors
# xterm-256colors
# 256

then start tmux. By default tmux will switch to 8 colors:

echo $TERM; tput colors
# screen
# 8

8-color terminal emulator

If instead you switch to a 8 color terminal, i.e. by switching to /dev/tty2 by pressing Ctrl+Alt+F2, you get

echo $TERM; tput colors
# linux
# 8

and inside tmux the same old

echo $TERM; tput colors
# screen
# 8

Forced 256-color mode

I can force tmux to pick up 256-color mode by adding the following to ~/.tmux.conf

set -s default-terminal "tmux-256color"

However this also applies if the outer terminal emulator did not support 256 colors to begin with. This is not what I want.

Adaptive 256-color mode

There is an issue and a FAQ on GitHub that state setting one of (people are reporting contradicting parameters)

set -ga terminal-overrides ",xterm-256color*:Tc"
set -sa terminal-overrides ",xterm-256color*:Tc"
set -ga terminal-overrides ",*256col*:Tc"
set -sa terminal-overrides ",*256col*:Tc"

should make tmux pick up the terminal's color mode, but none of them work on my installation (tmux 3.0 on Arch).

Is there a definitive way of letting tmux pick up either 8 or 256 colors, depending on what the current terminal emulator supports?

1 Answer 1

3

This isn't really something tmux can entirely do for you itself because it can't change TERM on programs that are already running, for example if you detach from a terminal with xterm-256color and reattach to a terminal with xterm.

You can do it yourself, however. The client's TERM is available with the client_termname format. So you can put something like this in your PS1 or .profile or whatever:

if [ -n "$TMUX" ]; then
    T=$(tmux display -p '#{client_termname}')
    case "$T" in
    *-256color)
         export TERM=tmux-256color
         ;;
    *)
         export TERM=tmux
         ;;
    esac
fi

Obviously this will default to tmux if you create panes in an unattached session (client_termname will be empty), or to the TERM from the most recently used client if you have a session attached to multiple clients.

If you don't want to do it in your shell like this, you could use the client-attached hook to change default-terminal - but this will only apply to new panes, not existing ones.

Another option would be to run with tmux-256color all the time, and let tmux translate the colour down when the terminal outside can't do 256 colours.

All the stuff in the FAQ about Tc is about RGB colour (true colour), not what you are asking about.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .