Skip to content

Instantly share code, notes, and snippets.

@st3v
Last active January 31, 2023 00:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st3v/5008165 to your computer and use it in GitHub Desktop.
Save st3v/5008165 to your computer and use it in GitHub Desktop.
Automatically set background color in iTerm depending on ssh host
#!/bin/bash
#
# ssh into a machine and automatically set the background
# color of Mac OS X Terminal depending on the hostname.
#
# Installation:
# 1. Save this script to /usr/local/bin/ssh-host-color
# 2. chmod 755 /usr/local/bin/ssh-host-color
# 3. alias ssh=/usr/local/bin/ssh-host-color
# 4. export PRODUCTION_HOST="<hostname_production_server>"
# 5. Configure your host colors below.
#
# Taken from http://talkfast.org/2011/01/10/ssh-host-color
#
set_term_bgcolor(){
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm"
tell the current terminal
tell the current session
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
end tell
end tell
end tell
EOF
}
if [[ "$@" =~ "$PRODUCTION_HOST" ]]; then
set_term_bgcolor 50 0 0
else
set_term_bgcolor 0 40 0
fi
ssh $@
set_term_bgcolor 0 0 0
clear
@mihigh
Copy link

mihigh commented Dec 11, 2015

You can use profiles to make this work easier. See https://gist.github.com/mihigh/ead75ff55756680ae7cd

@Timmmm
Copy link

Timmmm commented Jan 17, 2019

Doesn't work as of now unfortunately. Gives the error:

44:52: syntax error: Expected end of line but found identifier. (-2741)

It's complaining about the terminal bit (despite that not being on line 44?).

@Timmmm
Copy link

Timmmm commented Jan 17, 2019

Probably easier to use their escape codes: https://iterm2.com/documentation-escape-codes.html

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