How many ssh session do you open usually? In Scribd we have about 50 machines, and most of the time I have to connect to several of them to do my work. But there is a big problem — it’s hard to distinguish among different tabs in iTerm. Of course, you can see the host where you connected to in the tab title, but the title is really small, low contrast, etc. So I had a dream since I’ve started using Mac — to get current host written with large letters on the background.
iTerm has a great support of AppleScript. Looking through the commands list I’ve found a really useful one: background image path. The last thing to figure out is how to create an image with the current host name, and I decided to use ImageMagick. So, let’s do it!
First, you need to install ImageMagick. You can use binary distribution, but I’d prefer the MacPorts version:
1 | port install ImageMagick |
Let’s create a test image:
1 2 3 | convert -size "570x342" xc:black -gravity "NorthEast" -fill "#662020" \ -family "Georgia" -style "Normal" -pointsize "60" -antialias \ -draw "text 20,10 'kpumuk.info'" "/tmp/iTermBG.png" |
Here is the result and it looks pretty good!

Ok, now the most interesting part. I’ve created a bash script in ~/bin/ssh, which sets the background before calling ssh and clears it on exit. I suppose you have this folder specified before /usr/bin in your .bash_profile:
1 | export PATH=$HOME/bin:/opt/local/bin:$PATH |
Here is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!/bin/bash BG_COLOR="#000000" # Background color FG_COLOR="#662020" # Foreground color DIMENSIONS="570x342" # Console dimensions GRAVITY="NorthEast" # Text gravity (NorthWest, North, NorthEast, # West, Center, East, SouthWest, South, SouthEast) OFFSET="20,10" # Text offset FONT_SIZE="60" # Font size in points FONT_FAMILY="Georgia" # Font family FONT_STYLE="Normal" # Font style (Any, Italic, Normal, Oblique) HOSTNAME=`echo $@ | sed "s/.*@//" | sed "s/ .*//"` set_bg () { osascript -e "tell application \"iTerm\" set current_terminal to (current terminal) tell current_terminal set current_session to (current session) tell current_session set background image path to \"$1\" end tell end tell end tell" } on_exit () { if [ ! -f /tmp/iTermBG.empty.png ]; then convert -size "$DIMENSIONS" xc:"$BG_COLOR" "/tmp/iTermBG.empty.png" fi set_bg "/tmp/iTermBG.empty.png" rm "/tmp/iTermBG.$$.png" } trap on_exit EXIT convert -size "$DIMENSIONS" xc:"$BG_COLOR" -gravity "$GRAVITY" -fill "$FG_COLOR" -family "$FONT_FAMILY" -style "$FONT_STYLE" -pointsize "$FONT_SIZE" -antialias -draw "text $OFFSET '$HOSTNAME'" "/tmp/iTermBG.$$.png" set_bg "/tmp/iTermBG.$$.png" /usr/bin/ssh "$@" |
Let’s try it:

So now I can stop shaking with fear when doing some destructive things in console — I know which host exactly I’m trying to kill. I hope you will find this post useful.
Updated 05/01/2009: Updated on_exit function to fix bug with scrolling after ssh session being closed.
Updated 05/03/2009: Added some variables to customize how host name will look.
It’s interesting feature. But I think it’s not very useful.
Very cool, that would be so useful in large data centers!!!
This will work better with arguments passed to ssh:
e.g. on OSX
-Yforwards X11 connections easily, but using your sed expressions the commandssh -Y myhostwill result in-Ybeing the image shown in the background.Also, you still need to be careful that you are on the host that you logged onto originally from OSX. if you chain your ssh’s your background image will be incorrect.
Hi,
Thanks a lot for this, that solved problems I had for ages connecting to too many servers….
just a little problem, the background image disapear when I enter full screen (CMD-Enter)
cheers
julien
Great tip, thanks for sharing!
I did discover an error when attempting to run in snow leopard (v10.6.1):
2
3
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
After a quick google search, I ran into a post (http://forums.adobe.com/thread/486208) that suggested prefixing the osascript command with the appropriate supported architecture:
So if anyone runs into this issue, just change the following line:
with this:
I loved this, but the problem is, I use a fullscreen monitor specifically for SSH at work, but I only have my regular laptop screen at home. So, of course, the background would be extremely distorted.
So I came up with the following; this should net you the approximate resolution by converting the rows/columns to a resolution. I simply used the ratio of the columns/rows to the resolution on my laptop:
2
3
4
H=$((1440/178*`stty size | cut -d ' ' -f2`))
V=$((900/47*`stty size | cut -d ' ' -f1`))
DIMENSIONS=$H"x"$V
Its not foolproof, but its very dynamic and works great for me! :)
Andrew
A quick improvement I had to implement when dealing with non-terminal windows (i.e., bash scripts):
2
3
4
5
6
7
[ "$( tty )" == 'not a tty' ] && /usr/bin/ssh "$@" && exit
# Generate dimension based on screensize
H=$((1440 / 178 * `stty size | cut -d ' ' -f2` ))
V=$((900 / 47 * `stty size | cut -d ' ' -f2` ))
DIMENSIONS=$H"x"$V # Console dimensions
This uses the the tty variable to detect whether or not its a terminal session, so it won’t try and grab the screen size from stdin-less sessions. Note, this depends on the tty variable being set which, from my basic testing, works flawlessly on bash-3.2.
Andrew
That’s sweet, thank you! Will test on my machine and update the post. Good work!
Wait! I realized a mistake; ssh doesn’t always exit with a 0 status, even though it may be a normal exit. To make it a little clearer, the following worked:
2
3
4
5
if [ "$( tty )" == 'not a tty' ] ; then
/usr/bin/ssh "$@"
exit
fi