7

I have a color scheme that I like for when I'm in a terminal, but I ssh into the machine I work on from multiple sources (locally, PuTTY, my netbook, etc.) and I want to maintain the same color scheme throughout. Is this possible?

I especially want it in PuTTY; it's difficult to change PuTTY colors.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233

2 Answers2

10

Colors in terminals are determined in two steps:

  • the program running in the terminal tells the terminal to use a certain color number;
  • the terminal translates each color number into a color value.

Xterm has an escape sequence to change the color value associated with a color number. I don't remember whether PuTTY supports this sequence; I know Mintty does.

set_color_value () {
  printf "\\e]4;$1;$2\\a"
}
set_color_value 4 '#6495ed'  # set color 4 (blue) to CornflowerBlue

These settings won't survive a terminal reset. You can overcome this difficulty by appending the cursor configuration changing sequence to your terminal's reset string.

  • On a terminfo-based system using ncurses, save your terminal's terminfo settings to a file with infocmp >>~/etc/terminfo.txt. Edit the description to change the rs1 (basic reset) sequence, e.g. replace rs1=\Ec by rs1=\Ec\E]4;4;#6495ed\E\\. With some programs and settings, you may need to change the rs2 (full reset) as well. Then compile the terminfo description with tic ~/etc/terminfo.txt (this writes under the directory $TERMINFO, or ~/.terminfo if unset).
  • On a termcap-based system, grab the termcap settings from your termcap database (typically /etc/termcap). Change the is (basic reset) and rs (full reset) sequences to append your settings, e.g. :is=\Ec\Ec\E]4;4;#6495ed\E\\:. Set the TERMCAP environment variable to the edited value (beginning and ending with :).

Now you can put something like this in your ~/.profile:

if [ "$(ps -p $PPID -o comm=)" = sshd ] &&
   [ "$TERM" = "xterm" ]; then
  set_color_value … # set color scheme
  TERMCAP=…  # if necessary
fi
  • unless I'm misunderstanding his question.... I think this has way overcomplicated things. – xenoterracide Jan 16 '11 at 02:28
  • @xeno: I think you are misunderstanding the question. I believe Gilles got it right. – sepp2k Jan 16 '11 at 02:44
  • @sepp2k could be – xenoterracide Jan 16 '11 at 02:56
  • This is a bit over my head; I am still pretty new to Linux. Am I to put the code for set_color_value() into my .bashrc? And how do I know if I'm using terminfo or termcap? – Benjamin Carlsson Jan 16 '11 at 20:55
  • 1
    @skoh-fley: Start with putting the set_color_value stuff in your .bashrc. If you're unsure about .bashrc vs .profile vs .bash_profile, search on this site, Super User and Ask ubuntu where the topic has been discussed at length. You may find that the color scheme goes away when you start a full-screen application; only then should you worry about the bit about the terminal reset. If you don't know, you probably have terminfo. – Gilles 'SO- stop being evil' Jan 16 '11 at 23:38
0

You're ssh-ing into just one box right? why not just set the PS1 variable on that box to use the colorscheme you want? If you keep it to 16 colors you shouldn't have a problem on any modern TERM, most should support 256 colors, but most don't set TERM=xterm-256color out of the box, and some fools (cough my employer cough) sanitize TERM to be alpha-numeric only. Unfortunately what to put in your PS vars, is highly dependent on the shell you are using.

xenoterracide
  • 59,188
  • 74
  • 187
  • 252