4

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 tmux -- ls 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.

3

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
0

What worked for me is putting the following lines in my .bashrc file:

    if [ "x$DISPLAY" != "x" ]
    then
        export HAS_256_COLORS=yes
        alias tmux="tmux -2"
        if [ "$TERM" = "xterm" ]
        then
            export TERM=xterm-256color
        fi
    else
        if [ "$TERM" == "xterm" ] || [ "$TERM" == "xterm-256color" ]
        then
            export HAS_256_COLORS=yes
            alias tmux="tmux -2"
        fi
    fi

    if [ "$TERM" = "screen" ] && [ "$HAS_256_COLORS" = "yes" ]
    then
        export TERM=screen-256color
    fi

Let me know if this helps.


Note that this solution is based on this post!

  • This look like the changes jalvesaq posted a few months ago. Did you get this from there? – Anthon Apr 23 '14 at 8:05
  • Thank you, I didn’t see this. I got this solution send per mail a couple of weeks ago. I had some trouble with Vimux. I will edit the post and point to the page. – lord.garbage Apr 23 '14 at 8:12
  • Looks like that was realy setf I used instead of setaf. – Adobe Apr 23 '14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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