27

I did some updating on my Mac and seem to have broken some of my settings. I have the following set in my .bash_profile

export PS1="\W $"

This is working in my normal bash session to show just the current directory instead of the whole path. However, when I switch into tmux, it again displays the whole path. Other changes to the PS1 in the bash profile such as color or other characters work fine and are reflected in tmux. I have emptied out my .tmux.conf to see if that was causing conflict but there was no change to this behavior.

I did create a new user on the system and tried the same PS1 and it worked perfectly in both a normal session and tmux.

I am mostly confused because I know I had it working and can't figure out what would have changed in the update. What files besides .bash_profile and .tmux.conf could be at play here? Is there a way to tell where tmux is pulling it's settings from?

Additional info: This behavior is the same in both iTerm2 and Terminal Tmux version 1.8 Mac OSX 10.9.1

3
  • tmux may be creating non-login shells, in which case .bashrc would be sourced instead.
    – chepner
    Jan 8, 2014 at 20:50
  • I don't think that is the case as the other aliases in my .bash_profile are carrying over into tmux and any other changes I make to the PS1 reflect properly in tmux however the \W variable just doesn't seem to want to behave normally.
    – Paige
    Jan 9, 2014 at 1:24
  • once you're in tmux, what is $PS1 set to?
    – graywh
    Apr 14, 2014 at 14:54

9 Answers 9

40

This one works for me: In tmux/terminal:

tmux set-option -g default-command bash

Or simply put into ~/.tmux.conf:

set-option -g default-command bash
6
  • 2
    +1. Yes, still valid under tmux 2.7. I spawn several panes automatically everytime I launch gnome-terminal by executing tmux -u new-session \; split-window -h \; split-window -v \; select-pane -t 0 \; send-keys ' ' C-m \; ... After several solid hours of poring over many many wikis and posts, this finally worked for me. Cheers.
    – Cbhihe
    Sep 23, 2018 at 17:47
  • 1
    Simple, clean, valid. This should be the accepted answer. Jun 25, 2019 at 17:15
  • @Cbhihe thanks for this tip. But why do you need the send-keys? Without that last part it works for me.
    – SPRBRN
    Aug 1, 2020 at 14:06
  • @SPRBRN: Woaah ! It's been quite some time, but if I recall well send-keys ' ' C-m will just send the empty key ' ' with a carriage return to the pane previously selected with select-pane -t 0. So it should position your cursor in said pane in a manner that is visually identifiable. That is the only reason for using it. It's one fewer mouse movement and click ... You can execute the command above (you can even program your term emulator to do so automatically upon launch) and you are set. Just start typing directly in the pane selected by default, as identified by your cursor. ;-)
    – Cbhihe
    Aug 2, 2020 at 20:01
  • This is the right answer, but lets point out that first command only works untill you restart the system. So you should use the second command and put it inside your ~/.tmux.
    – 71GA
    Apr 16, 2021 at 11:32
16

Add the following in your ~/.tmux.conf

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

From the beloved ArchWiki tmux

1
  • Worked for me. @tmux-lover's one didn't Nov 3, 2020 at 10:10
7

I have the similar problem. I get the correct result if I always start tmux with the bash command applied, like so:

tmux new bash

So, to simplify this, I just created an alias in my ~/.bash_aliases file, which I use all the time:

alias tn='tmux new bash'

This does only work for the first window though. When creating new windows, you have to start bash again, by executing bash in the terminal.

1
  • thumbs up man .. the accepted answer didn't work for me either. Feb 13, 2015 at 13:36
7

Add the following line to ~/.tmux.conf:

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

Don't forget to save ~/.tmux.conf and restart tmux in order for the changes to take effect.

0
4

I had the same problem and after some research I have added the following command to my ~/.tmux.conf:

set-option -g default-command "reattach-to-user-namespace -l /opt/local/bin/bash --login"

I am using OSX 10.9.5 with iTerm2 Build 2.0.0.20141103, bash 4.3.30(1)-release, tmux 1.9a. Bash and tmux are from macports.

1
  • This worked for me, but I also had to run brew install reattach-to-user-namespace
    – BananaNeil
    Sep 1, 2019 at 13:28
2

I know I'm far late to the party. But here's what worked for me.

I just added -256color to TERM.

  1. Open/Run tmux.
  2. Run the following command: echo $TERM. We are gonna use the output of this. I got screen. Use your own in the next step.
  3. Edit your tmux conf file: vim ~/.tmux.conf and add this line: set -g default-terminal screen-256color.
  4. Exit tmux.
  5. Open/Run tmux again.

Using tmux-256color didn't work for me. I'm using Ubuntu 18.04, bash 4.4.20, tmux 2.6.

1

Better to detect which terminals are known to the system's terminfo database via the infocmp command. I've got the following in my ~/.tmux.conf:

# Last match wins
if-shell "infocmp xterm-256color" "set-option -g default-terminal xterm-256color"
if-shell "infocmp screen-256color" "set-option -g default-terminal screen-256color"
if-shell "infocmp tmux" "set-option -g default-terminal tmux"

If the infocmp shell-command succeeds, then the following set-option tmux command is executed.

0

When using tmux with byobu, you can try the byobu-prompt command. It'will ask you whether you want a bash color prompt. After answering Yes, the prompt will change to the usual user@host.

-5

Add the following to your .tmux.conf:

new -n WindowName bash --login

You can replace WindowName with whatever you want the first window to be named. When bash is invoked this way, it sources to your .bash_profile, .bash_rc, .profile, etc. which is where you change your $PS1.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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