Getting 256 colors to work in terminal multiplexer

colorsgnu-screenterminaltmux

tmux

I followed Getting 256 colors to work in tmux:

  • I have alias tmux='TERM=xterm-256color tmux' in ~/.bashrc
  • and also set-option -g default-terminal "screen-256color" in ~/.tmux.conf

In konsole:

$ echo $TERM
xterm

In tmux:

$ echo $TERM
screen-256color

Still colors doesn't work in tmux:

boris@vasilisa:~$ PROMPT_GREEN=`tput setf 2`
boris@vasilisa:~$ PROMPT_RED=`tput setf 4`
boris@vasilisa:~$ PROMPT_BLACK=`tput setf 8`
boris@vasilisa:~$ PS1='\[$PROMPT_RED\]\w\[$PROMPT_GREEN\]:\[$PROMPT_BLACK\] '

Results in a black-and-white color prompt. Also no color with ls.

screen

I have force_color_prompt=yes in ~/.bashrc. Still:

boris@vasilisa:~$ PROMPT_GREEN=`tput setf 2`
boris@vasilisa:~$ PROMPT_RED=`tput setf 4`
boris@vasilisa:~$ PROMPT_BLACK=`tput setf 8`
boris@vasilisa:~$ PS1='\[$PROMPT_RED\]\w\[$PROMPT_GREEN\]:\[$PROMPT_BLACK\] '

Doesn't make prompt colorful. But in contrast to tmuxls lists files with color.

So

I guess tput setf just shouldn't work with terminal multiplexers?

Edit

I had to change setf to setaf, and also change the color codes:

PROMPT_BLACK=`tput setaf 0`
PROMPT_RED=`tput setaf 1`
PROMPT_GREEN=`tput setaf 2`
PS1='\[$PROMPT_RED\]\w\[$PROMPT_GREEN\]:\[$PROMPT_BLACK\] '

I don't need to aliase tmux, setting

set-option -g default-terminal "screen-256color"

in ~/.tmux.conf is suffice.

So I put

# colorful prompt
PROMPT_BLACK=`tput setaf 0`
PROMPT_RED=`tput setaf 1`
PROMPT_GREEN=`tput setaf 2`
PS1='\[$PROMPT_RED\]\w\[$PROMPT_GREEN\]:\[$PROMPT_BLACK\] '

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto --group-directories-first'
    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# man pages with color!
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'

# enable colors:
force_color_prompt=yes

in both: ~/.bashrc and ~/.bash_login and then colors work in tmux.

Best Answer

Well as you quite correctly guessed setf is not correct capability for setting foreground color in context of xterm-256color(screen-256color) terminfo entry. You should use setaf (set foreground color using ANSI escape).

$ echo $TERM
screen-256color

$ infocmp -1 | grep setf
$ infocmp -1 | grep setaf
    setaf=\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m,

Note:

  1. you should not need to do alias tmux='TERM=xterm-256color tmux', make sure your terminal emulator when started reports correct value of TERM=xterm-256color
  2. if (1) is true then tmux will correctly set your TERM to screen-256color inside it, also it will for example correctly set it if you are running on linux console to screen.linux, there might be case you need to manipulate it but generaly not