Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamatypeofwalrus/5593405 to your computer and use it in GitHub Desktop.
Save iamatypeofwalrus/5593405 to your computer and use it in GitHub Desktop.
Customize SSH tab name in Mac OS X terminal auto-magically.

What

Automtically configure tab name in Terminal.app when ssh-ing to a server. You can use any shell commands you want to get the desired output.

Why

I work on external servers 8 hours a day M-F and more likely than not I have multiple ssh sessions at the same time to the same sever under different users. It became a bit of a pain to have to rename the tabs everytime I logged on to a server. I just wanted that to happen auto-magically.

I searched the interwebs and came across this CNet article which had about 99% of what I was looking for. However, it never really mentioned how to modify the script to insert your own formatting. It didn't even mention adding it to your $PATH! So after a few minutes of messing with it I got something quite reasonable. I thought I'd share the results.

How

You can test your formatting pretty simply:

$ echo username@server.com | <YOUR-FORMATTING-COMMAND-HERE>

My test looked like this:

$ echo jfeeneysd@awesome_underpants | awk '{split($1,a,"@");print a[2]" -> "a[1]}'

Output:

awesome_underpants -> jfeeneysd

So here's a template for the script once you've settled on a format:

#!/usr/bin/env sh

printf "\e]1;`echo $* | <YOUR-FORMATTING-COMMAND-HERE> `\a"
ssh $*
printf "\e]1;bash\a"

Here's my code:

#!/usr/bin/env sh

printf "\e]1;`echo $* | awk '{split($1,a,"@");print a[2]" -> "a[1]}'`\a"
ssh $*
printf "\e]1;bash\a"

If you want to be able to call this from the command line make sure to put the folder where your script resides in your .bashrc or .bash_profile. I've named my script "sshj" and it resides in my $HOME/bin/ folder.

Add something like this to your bash:

PATH:$PATH:~/<folder_where_you_saved_the_ssh_script>/

This is the line I have in my bash:

PATH:$PATH:~/bin/

So on my computer I can call the script like this:

$ sshj someusername@badass.server.com

It's not a massive time saving effort, but it's a little pain point in my day that's resolved.

Resources

  1. CNET Article on modifying terminal tab name in Mac OS X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment