36

I recently decided to change my PS1 variable to accommodate some pretty Solarized colors for my terminal viewing pleasure. When not in a tmux session, everything is great! Rainbows, ponies, unicorns and a distinguishable prompt! Cool!

The problem is within tmux, however. I've verified that the value of PS1 is what I expect it to be and the same as it is when tmux isn't running, namely \[\033]0;\w\007\]\[\[\]\]\u\[\]@\[\[\]\]\h\[\]:\[\]\W\[\]$ \[\].

All of my aliases, etc. in my .bash_profile are also functioning as expected. tmux is also displaying colors without incident, as echo -ne "\033[1;33m hi" behaves as expected as does gls --color.

The current relevant line in my .bash_profile is export PS1="\[\033]0;\w\007\]\[\[\]\]\u\[\]@\[\[\]\]\h\[\]:\[\]\W\[\]$ \[\]", although originally I was sourcing a script located in a .bash_prompt file to handle some conditionals, etc. I tried reverting to the simpler version.

Executing bash will cause the prompt to colorize, but must be done in each pane. export PS1=[that long string I've already posted] will not.

My .tmux.conf is as follows:

set-option -g default-command "reattach-to-user-namespace -l /usr/local/bin/bash"
set -g default-terminal "xterm-256color"
set-window-option -g automatic-rename on
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

Relevant portions of .bash_profile:

export TERM="xterm-256color"
if which tmux >/dev/null 2>&1; then
    test -z "$TMUX" && (tmux attach || tmux new-session)
fi

I'm using macOS Sierra, iTerm 2, I've tried both the current homebrew version of bash and the system bash (it's currently using the homebrew), tmux 2.4.

I also placed touch testing_touch_from_bash_profile in my .bash_profile while in a tmux session with two panes, killed one pane, opened a pane and verified that the file was in fact created.

echo $TERM returns xterm-256color.

I've ensured that when exiting tmux to test settings changes that I've exited tmux and that no tmux process is currently running on the system via ps -ax | grep tmux.

Oddly, sourcing the .bash_prompt script also changes the color so long as I do it within each tmux pane.

I've looked at https://stackoverflow.com/questions/21005966/tmux-prompt-not-following-normal-bash-prompt-ps1-w and tried adding the --login flag after the bash call in the first line of my .tmux.conf. Launching tmux with tmux new bash will cause the first pane to colorize, but subsequent panes will not.

The $PS1 variable is being honored for seemingly all aspects except colorizing any of the fields.

Anyone have any ideas?

2
  • Try single quotes
    – Kevin
    Apr 22, 2017 at 5:24
  • 5
    The PS1 setting you posted doesn't have any command to change colors. Post an actual setting that does work outside tmux. Also, you should set PS1 in .bashrc, not in .bash_profile: .bash_profile might not get executed at all, and the settings might be overridden later. Apr 22, 2017 at 21:42

5 Answers 5

63

On my machine the solution is to add

set -g default-terminal "xterm-256color"

to ~/.tmux.conf.

3
  • This seems like the cleanest solution. Thank you.
    – ipetrik
    Jun 16, 2020 at 14:35
  • This should be the accepted answer IMHO. Sourcing .bashrc in each sub shell is both cumbersome and not needed. I do beleive that the point behind this is that tmux default to a $TERM='tmux' which is not handled by termcap as a "color capable shell"
    – webofmars
    Jan 20, 2021 at 12:55
  • 2
    For me, xterm-256color works but disables the home and end keyboard keys. screen-256color works too without the keyboard problem on Debian.
    – ciencia
    Dec 21, 2021 at 15:07
28

My solution to this problem was setting

force_color_prompt=yes

in my .bashrc. Now my tmux prompt has color.

3
  • Not sure why this behaves differently in tmux to outside of it, but this did the trick for me too.
    – Matt Lacey
    Feb 22, 2019 at 2:01
  • 3
    I th ink you should specify this: what you have to do is run the command tmux , and then inside a tmux session run source ~/.bashrc . You need to do that in each panel.
    – evaristegd
    Jul 19, 2019 at 5:59
  • Thanks, this work for me also on Ubuntu 18.04 gnome-terminal.
    – intijk
    Nov 27, 2019 at 19:12
2

Your PS1 also gives me black and white output.

However switching back to mine gives me color, so you should be able to figure out the different, I use

$ echo $PS1
\[\033[01;31m\]\t \[\033[01;32m\]durrantm \[\033[02;36m\]\h \[\033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"` \[\033[00;33m\]$(git_branch)\[\033[00m\]\n\$

git_branch is a bash function I have which is:

git_branch () { 
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

Yours vs. Mine:

enter image description here

This works the same on OSX and Ubuntu

1
  • 2
    -1 This in fact dosent give him an answer to the question posted but instead show him that there is an error in his example. This should be handled has a comment rather that an "answer" IMHO
    – webofmars
    Jan 20, 2021 at 12:57
1

Change $TERM to xterm-256color

No color:

$ echo $TERM                                              
screen

With color:

TERM=xterm-256color

Add this to your dotfile.

Remarks

This may not be the OPs situation but in case anyone else has landed on this page with this situation I'm hoping it helps.

0

I had this issue when working on Ubuntu 20.04.

While Panki's answer worked for me, I found myself always specifying source ~/.bashrc whenever I logged into Tmux shell following evaristegd's comment.

Here's how I fixed it:

Firstly, create a file called .tmux in the home directory of your machine, which is ~/.tmux.conf.

Next, put in the configuration that you want to use for Tmux inside it and save it. For me the configuration I needed was the same as the GNU screen package that comes default with Ubuntu uses, so I copied the content of the ~/.bashrc file into the ~/.tmux.conf file. This way if I have to edit any of the configurations for Tmux, I wouldn't tamper with the one for the GNU screen.

Note: You could skip this step and just use the ~/.bashrc file if you do not care for separation of concerns.

Next, add the file to either the ~/.bash_profile, ~/.bash_login, and ~/.profile file. It should be added at the bottom of the file:

source "$HOME/.tmux.conf"

OR

source "$HOME/.bashrc"

Here's an example:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

source "$HOME/.tmux.conf"

Note: The ~/.bash_profile file takes the first precedence, then the ~/.bash_login file as the second precedence, and then the ~/.profile file as the last precedence.

That's all.

I hope this helps

You must log in to answer this question.

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