Retaining bash prompt colors when starting a screen session

14,555

Solution 1

You can edit the following line in your .bashrc (it's #39 in my .bashrc):

#force_color_prompt=yes

Change to:

force_color_prompt=yes

This could possibly be annoying if you log in from somewhere where color is not supported, but i find it highly unlikely.

Solution 2

Screen normally uses a special terminal type, such as "screen", or if you set it in your .screenrc, "screen-256color".

Just look in your .bashrc for the color detection case statement and add screen to the list.

For example, something like this:

case "$TERM" in
    xterm)
        color_prompt=yes
        ;;
    screen)
        color_prompt=yes
        ;;
    *256*) 
        color_prompt=yes
        ;;
esac

I use 256-color terminal types, so I just need the 256 case statement, since it catches xterm-256color, gnome-256color, and screen-256color. Your mileage may vary. 🙂

Solution 3

The .screenrc file is a mystery to me. Mine is gobbledygook that I copypasta'd from the internets. However, I see a few lines that look to be relevant to your problem:

# terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I" 
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'

I think if you add the above lines to yours, you'll get colour. Here's my whole .screenrc for reference:

[email protected]:~$ cat .screenrc 
startup_message off # skip splash screen
vbell off # Kill the annoying dog

# Voodoo
hardstatus alwayslastline
hardstatus string '%{= wk}%-Lw%{= KW}%50>%n%f* %t%{= dK}%+Lw%<'

# terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I" 
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
# erase background with current bg color 
defbce "on"

Solution 4

Your PS1 setting should be placed in .bashrc, not .bash_profile.

If that is not the problem, please edit your question to include the line you have set PS1 to.

You could also try running these:

echo "$TERM"                                 # will probably print "screen"
tput setaf 2 | cat -vte                      # should print "^[[32m"
echo "$(tput setaf 2)"green"$(tput sgr0)"    # should print "green" in green

Solution 5

Add this to your ~/.screenrc

shell -$SHELL
Share:
14,555

Related videos on Youtube

chmullig
Author by

chmullig

Updated on September 17, 2022

Comments

  • chmullig
    chmullig 5 months

    When I ssh into a Ubuntu Lucid box the prompt is all pretty, with colors. Everything is the default, as far as I know. Here's my $PS1 outside screen:

    \[\e]0;\[email protected]\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
    

    Before connecting with screen

    But then once I start screen up the colors go away. Everything else is fine, and my screen can support colors (notice that the . and .. in this ls -al are blue) but the prompt isn't. In theory everything should be exactly the same. $PS1 inside screen:

    ${debian_chroot:+($debian_chroot)}\[email protected]:\w\$
    

    after screening in

    EDIT: This is just plain vanilla screen.

    • belacqua
      belacqua almost 12 years
      Is this plain vanilla screen, or do you have byobu installed? (Though this matter not matter.)
    • Mikel
      Mikel almost 12 years
      Does echo $PS1 print the same thing inside screen and outside screen?
    • chmullig
      chmullig almost 12 years
      $PS1 is indeed different. Edited question to include that.
  • chmullig
    chmullig almost 12 years
    Perfect, that did it for me. I can see it being problematic if I happen to login from somewhere without color, but that's going to be pretty rare. Thanks!
  • Adam Ryczkowski
    Adam Ryczkowski over 10 years
    The change needs to be done on the server machine, not the client.
  • funroll
    funroll about 9 years
    +1. This got me back my nice looking bash prompt on OS X.
  • phi
    phi over 2 years
    This looks like the cleanest way to enable color for screen sessions.
  • Rémy Hosseinkhan Boucher
    Rémy Hosseinkhan Boucher 11 months
    Nice, allows to keep host color with GNU Screen in local.