I can find it hard to differentiate between my dev and production server. I am using SSH to connect to the machines (both Ubuntu 10.04), and I was wondering whether there is a way to change the colour of the Terminal window depending on what SSH server it is connected to as I will forget to change the colour for each server...
4 Answers
This blog post provides a script to solve this problem.
#!/bin/bash
#
# ssh into a machine and automatically set the background
# color of Mac OS X Terminal depending on the hostname.
#
# Installation:
# 1. Save this script to /some/bin/ssh-host-color
# 2. chmod 755 /some/bin/ssh-host-color
# 3. alias ssh=/some/bin/ssh-host-color
# 4. Configure your host colors below.
set_term_bgcolor() {
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "Terminal"
tell window 0
set the background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
end tell
end tell
EOF
}
# Host-specific background colors.
if [[ "$@" =~ production1.com ]]; then
set_term_bgcolor 127 0 0
elif [[ "$@" =~ production2.com ]]; then
set_term_bgcolor 0 127 0
fi
ssh $@
# Default background color.
set_term_bgcolor 34 79 188
In Terminal.app you can change the "theme" per tab (e.g. Pro or Ocean) by right-click on a single tab --> Inspect Tab--> Settings. Also you can use iTerm for it has this option: "Tab labels can change color to indicate the session activities"
I tried using different colors for different SSH sessions a while back. It didn’t really work out because I would forgot which machine was the red one and which was the blue too easily.
I ended up learning myself a few commands that tell you where you are, and who you are there. whoami for example.
Using iTerm2, you can change the title bar and tab color for each server from the .bashrc on each machine. See the docs for information on this. I combine this with setting the prompt color to make it easy to tell apart. I generally have one iTerm window per server, possible with multiple tabs. This makes it easy to find the right window in Expose.
Here is the relevant section of my .cshrc. It would need to be adapted for bash (NOTE: ^] is control+] and ^G is control+G)
if ( ${?term} != 0 ) then
# tab window
alias cwdcmd 'printf "^]1;%s^G^]2;%s^G" "$cwd:t" "$HOST `echo $cwd | sed s $HOME-~-`" '
# Set title/tab color
alias settermr "echo -n '^]6;1;bg;red;brightness;\!*^G'"
alias settermg "echo -n '^]6;1;bg;green;brightness;\!*^G'"
alias settermb "echo -n '^]6;1;bg;blue;brightness;\!*^G'"
settermr 171;settermg 0;settermb 0
# Set title text
alias settitle 'echo -n "^]2; "\!*"^G"'
settitle `hostname -s`
# Set tab text
alias settab 'echo -n "^]1; "\!*"^G"'
settab $cwd:h:t/$cwd:t
cwdcmd
endif