So, call me crazy, but when I’ve got a dozen Terminal windows and tabs open at once, and they’re all ssh’d to different boxes, it gets hard to remember which host is which. I end up checking the prompt or doing something like hostname to remember where I am. So, I found a way to automatically switch the Terminal settings to color-code my SSH hosts.

There are two steps. First, we need to create an AppleScript script1 that will actually do the switching of the Terminal settings. Save this code somewhere memorable, as we will refer to it later on.

The next step is to update your ssh_config (normally ~/.ssh/config). We’re going to use the LocalCommand directive to trigger our AppleScript script after the host connects. We also need to permit that directive via PermitLocalCommand yes. It’s disabled by default, presumably for security reasons — so only enable it per host, not globally.

Add something like this for each host you want to have color-coded, along with the Terminal setting name to use:

Host example.com
  PermitLocalCommand yes
  LocalCommand osascript %d/scripts/set_term_settings.scpt "Homebrew"

Replace example.com with your host. Refer to the ssh man page for escape character substitutions — I’m using %d here for my local user’s home directory. (Oh, and make sure that the osascript command actually works by running it on the command line first.)

You can add your own Terminal settings, or use the built-in ones. You can also tweak the script to set properties individually rather than using a setting name. I’m using this simply to change the background color and opacity, but you could go nuts and set any other property in the setting. This script could be pretty powerful depending on how you want to use it.

  1. “AppleScript script” looks redundant, but “AppleScript” by itself looks weird, too. Not sure what the standard is, but I’m going with redundant.