2

I am accessing a new Ubuntu 12.10 installation via PuTTY. I am attempting to get my shell to display in color. Color works fine in interactive applications like vim, but I want to get color in e.g. my $PS1 prompt, git, ls, etc.

When I use a screen session, the color magically works. Without screen there is no color.

Without screen:

$ set |grep -i term
TERM=xterm

With screen:

$ set |grep -i term
TERM=screen
TERMCAP=$'SC|screen|VT 100/ANSI X3.64 virtual terminal:\\\n\t:DO=\\E[%dB:LE=\\E[%dD:RI=\\E[%dC:UP=\\E[%dA:bs:bt=\\E[Z:\\\n\t:cd=\\E[J:ce=\\E[K:cl=\\E[H\\E[J:cm=\\E[%i%d;%dH:ct=\\E[3g:\\\n\t:do=^J:nd=\\E[C:pt:rc=\\E8:rs=\\Ec:sc=\\E7:st=\\EH:up=\\EM:\\\n\t:le=^H:bl=^G:cr=^M:it#8:ho=\\E[H:nw=\\EE:ta=^I:is=\\E)0:\\\n\t:li#62:co#115:am:xn:xv:LP:sr=\\EM:al=\\E[L:AL=\\E[%dL:\\\n\t:cs=\\E[%i%d;%dr:dl=\\E[M:DL=\\E[%dM:dc=\\E[P:DC=\\E[%dP:\\\n\t:im=\\E[4h:ei=\\E[4l:mi:IC=\\E[%d@:ks=\\E[?1h\\E=:\\\n\t:ke=\\E[?1l\\E>:vi=\\E[?25l:ve=\\E[34h\\E[?25h:vs=\\E[34l:\\\n\t:ti=\\E[?1049h:te=\\E[?1049l:us=\\E[4m:ue=\\E[24m:so=\\E[3m:\\\n\t:se=\\E[23m:mb=\\E[5m:md=\\E[1m:mr=\\E[7m:me=\\E[m:ms:\\\n\t:Co#8:pa#64:AF=\\E[3%dm:AB=\\E[4%dm:op=\\E[39;49m:AX:\\\n\t:vb=\\Eg:G0:as=\\E(0:ae=\\E(B:\\\n\t:ac=\\140\\140aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~..--++,,hhII00:\\\n\t:po=\\E[5i:pf=\\E[4i:k0=\\E[10~:k1=\\EOP:k2=\\EOQ:k3=\\EOR:\\\n\t:k4=\\EOS:k5=\\E[15~:k6=\\E[17~:k7=\\E[18~:k8=\\E[19~:\\\n\t:k9=\\E[20~:k;=\\E[21~:F1=\\E[23~:F2=\\E[24~:F3=\\E[1;2P:\\\n\t:F4=\\E[1;2Q:F5=\\E[1;2R:F6=\\E[1;2S:F7=\\E[15;2~:\\\n\t:F8=\\E[17;2~:F9=\\E[18;2~:FA=\\E[19;2~:kb=\177:K2=\\EOE:\\\n\t:kB=\\E[Z:kF=\\E[1;2B:kR=\\E[1;2A:*4=\\E[3;2~:*7=\\E[1;2F:\\\n\t:#2=\\E[1;2H:#3=\\E[2;2~:#4=\\E[1;2D:%c=\\E[6;2~:%e=\\E[5;2~:\\\n\t:%i=\\E[1;2C:kh=\\E[1~:@1=\\E[1~:kH=\\E[4~:@7=\\E[4~:\\\n\t:kN=\\E[6~:kP=\\E[5~:kI=\\E[2~:kD=\\E[3~:ku=\\EOA:kd=\\EOB:\\\n\t:kr=\\EOC:kl=\\EOD:km:'

What is the TERMCAP environment variable, and how does it get set by screen? How can I get that to be set outside of screen (short of just copy-pasting it into my .bashrc)?

2
  • 1
    Do you get color if you use ls --color=tty instead of ls?
    – terdon
    Dec 7, 2012 at 13:43
  • @terdon: Yes, that works. Dec 7, 2012 at 13:45

2 Answers 2

3

The issue was that ~/.bashrc is not sourced when logging in directly via PuTTY, but it is when using a screen session. Apparently, logging in via PuTTY is considered a "remote non login non interactive shell".

As @terdon pointed out, ls, grep et al are colorless by default, but my .bashrc has it aliased to use colors.

My fix was to source .bashrc in my .bash_profile:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
2
  • Any idea how you get PuTTY to act like a terminal client on Linux distributions so that it happens by default? I don't want to have to reconfigure my bash profile or force-colors on all Linux boxes I connect to
    – Ashley
    Feb 25, 2015 at 16:14
  • 1
    Actually this blog post summarises how you should configure PuTTY instead... using the terminal string of putty-256color would be a good start. The default of xterm is highly compatible, but pretty naff. blog.sanctum.geek.nz/putty-configuration
    – Ashley
    Feb 25, 2015 at 16:24
1

ls etc do not use color by default, if you want to have the colored you need to set the specific option. You can do this globally by setting aliases in your $HOME/.bashrc file (on the remote server):

  1. Set $PS1 (this one is from my .bashrc., you can change it as desired):

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\u@\h\[\033[01;34m\] \W \\$\[\033[00m\] '
    

    This looks like so: enter image description here

  2. Set aliases for (in this example ls and grep you need to find the appropriate option for git and any other programs you want to always have colored):

    alias grep='grep --color' 
    alias ls='ls --color=tty'
    
2
  • Hmm, it seems I have all of those defined as aliases when I use screen. My $PS1 is also different. I didn't realize screen did all of that. Dec 7, 2012 at 13:55
  • I guess screen exports these variables by default. Does it work OK now @jbinto?
    – terdon
    Dec 7, 2012 at 16:56

You must log in to answer this question.

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