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

Paige
  • 665
  • 1
  • 9
  • 14
  • `tmux` may be creating non-login shells, in which case `.bashrc` would be sourced instead. – chepner Jan 08 '14 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 09 '14 at 01:24
  • once you're in tmux, what is $PS1 set to? – graywh Apr 14 '14 at 14:54

9 Answers9

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
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
tmux-lover
  • 409
  • 4
  • 2
  • 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 '18 at 17:47
  • 1
    Simple, clean, valid. This should be the accepted answer. – Brett Holman Jun 25 '19 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 01 '20 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 02 '20 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 '21 at 11:32
  • For me, none of the ones here worked. But https://unix.stackexchange.com/a/577104 worked. – masec Jun 17 '22 at 07:46
16

Add the following in your ~/.tmux.conf

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

From the beloved ArchWiki tmux

nightm4re
  • 171
  • 1
  • 3
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.

Samuel Lampa
  • 4,336
  • 5
  • 42
  • 63
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.

gmdev
  • 2,725
  • 2
  • 13
  • 28
ds-bos-msk
  • 780
  • 9
  • 13
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.

srk
  • 725
  • 1
  • 9
  • 22
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.

joker
  • 3,416
  • 2
  • 34
  • 37
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.

RobM
  • 8,373
  • 3
  • 45
  • 37
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.

franbenz
  • 696
  • 1
  • 10
  • 16
-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.

Coder-256
  • 5,212
  • 2
  • 23
  • 51