A shell script to set Terminal window colors per SSH host

Aug 02, '05 08:29:00AM

Contributed by: jdsmith

I wanted Terminal's background color to change based on the SSH host I access, and then change back when I log out. This hint describes how to programmatically control the color and opacity of newly created terminal windows, and is part of the solution. Unfortunately, it works by setting the properties of the front window. When the Terminal is used in focus follows mouse, the active window is not always the front window, and this method sometimes changes colors in the wrong window.

To solve this, I wrote a modified script (4KB binary download -- it's binary to maintain the BEL and Escape characters encoded in the script) that first sets a unique window title, and then selects the appropriate window based on that title. With one script, you can manage as many different SSH host/color combinations you'd like. Simply add custom colors to the case statement where indicated, and link to the script using the name of the new host you'd like. Example:

% cd bin
% ln -s ssh_host foobar
% rehash    ## for tcsh shell [or]
% hash -r   ## for bash shell
% foobar
To keep host names short, you can either add an /etc/hosts alias, or link with the full hostname (e.g. ln -s ssh_host foobar.baz.com) and create a shell alias for it. If you set your own window title with a precmd alias or equivalent, you might want to remove the last echo (which clears the title).

Comments (15)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050723153226344