Skip to content

Instantly share code, notes, and snippets.

@cesalazar
Last active January 7, 2016 04:24
Show Gist options
  • Save cesalazar/18534ece282dd5209271 to your computer and use it in GitHub Desktop.
Save cesalazar/18534ece282dd5209271 to your computer and use it in GitHub Desktop.
Set the tab AND background color of iTerm based on ssh-host
#!/bin/bash
#
# (1) copy to: ~/ssh-host-color
# (2) set: alias ssh=~/ssh-host-color
# iTerm2 window color commands
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
}
# iTerm2 tab color commands
tab-color() {
echo -ne "\033]6;1;bg;red;brightness;$1\a"
echo -ne "\033]6;1;bg;green;brightness;$2\a"
echo -ne "\033]6;1;bg;blue;brightness;$3\a"
}
term-reset-color() {
echo -ne "\033]6;1;bg;*;default\a"
term-bgcolor 0 0 0
}
# Change the color of the bg & tab when using SSH
color-ssh() {
if [[ -n "$ITERM_SESSION_ID" ]]; then
trap "term-reset-color" INT EXIT
if [[ "$@" =~ "server1" ]]; then
tab-color 0 125 255
term-bgcolor 0 0 20
elif [[ "$@" =~ "server2" ]]; then
tab-color 200 0 155
term-bgcolor 20 0 00
elif [[ "$@" =~ "host3" ]]; then
tab-color 0 200 0
term-bgcolor 0 20 0
else
term-reset-color
term-bgcolor 0 0 0
fi
fi
ssh $@
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment