Skip to content

Instantly share code, notes, and snippets.

@pablete
Forked from porras/.bash_profile
Last active March 28, 2022 14:03
Show Gist options
  • Save pablete/5871811 to your computer and use it in GitHub Desktop.
Save pablete/5871811 to your computer and use it in GitHub Desktop.
Change iTerm2 profile (ie: background color) when SSHing into another machine
# Changing iTerm2 color in MacOSX when SSHing (so you know at a glance that you're no longer in Kansas)
# Adapted from https://gist.github.com/porras/5856906
# 1. Create a theme in your terminal setting with the name "SSH" and the desired colors, background, etc.
# 2. Add this to your .bash_profile (or .bashrc, I always forget the difference ;))
# 3. Optional but useful: in the terminal, go to Settings > Startup and set "New tabs open with" to
# "default settings" (otherwise, if you open a new tab from the changed one, you get a local tab with
# the SSH colors)
function tabc() {
NAME=$1; if [ -z "$NAME" ]; then NAME="Default"; fi # if you have trouble with this, change
# "Default" to the name of your default theme
echo -e "\033]50;SetProfile=$NAME\a"
}
function colorssh() {
tabc SSH
ssh $*
tabc
}
alias ssh="colorssh"
# This would be easy to extend to check if a theme with the name of the server exists and set it, and
# fall back to the SSH theme. This way you can have different colors for different remote environments
# (per project, production, staging, etc.)
@domingusj
Copy link

Thank you for your super helpful post! I'm looking to do the same thing but our naming scheme for production and staging is a little different. For example, production would be: host1.pr.location.example.com and staging would be host1.stg.location.example.com. I've tried modifying your script in the if/else statements to be "if [[ "$" =~ ".pr.location.example.com" ]]; then..." but it doesn't seem to like the *. It works if I use the FQDN with no *'s. Any ideas why it wouldn't be working?

@d-robin
Copy link

d-robin commented Oct 7, 2016

Thank you! I wasn't aware of, echo -e "\033]50;SetProfile=$NAME\a"
and I liked the hack with those two functions :-)

@tripleee
Copy link

Thanks for this useful snippet! Can I suggest a few small changes, though?

  • You should not use uppercase for the variable NAME and you should declare it local to avoid clobbering any existing variable with the same name.

  • Using an alias for a function is sort of roundabout; you can call the function ssh and use command ssh inside the function to call the actual command (instead of go into a recursive loop) and get rid of the alias.

  • You should probably use ssh "$@" (or, as per the above, command ssh "$@") to correctly preserve argument quoting.

@zhiqipan
Copy link

Thanks for the snippet, it helps a lot! Here's a tiny change I made to fix a bug where control+c doesn't work after exiting ssh:

function tab-reset() {
  trap - INT EXIT #untrap signals
  local name="Default"
  echo -e "\033]50;SetProfile=$name\a"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment