Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
set the background color of iTerm based on ssh-host
#!/bin/bash
#
# (1) copy to: ~/bin/ssh-host-color
# (2) set: alias ssh=~/bin/ssh-host-color
#
# Inspired from http://talkfast.org/2011/01/10/ssh-host-color
# Fork from https://gist.github.com/773849
#
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 [[ "$@" =~ thomd ]]; then
set_term_bgcolor 40 0 0
elif [[ "$@" =~ git ]]; then
set_term_bgcolor 0 40 0
fi
ssh $@
set_term_bgcolor 0 0 0
@stantonk

This comment has been minimized.

Show comment
Hide comment
@stantonk

stantonk Jun 18, 2014

did not work for me :(

stantonk commented Jun 18, 2014

did not work for me :(

@stantonk

This comment has been minimized.

Show comment
Hide comment
@stantonk

stantonk Jun 18, 2014

clarification -- did not work until after i exited the ssh session :-\

stantonk commented Jun 18, 2014

clarification -- did not work until after i exited the ssh session :-\

@kjmurphyjr

This comment has been minimized.

Show comment
Hide comment
@kjmurphyjr

kjmurphyjr Jul 5, 2014

This is great. Is it possible to simply change to a preset profile? I have the Solarized Light and Dark colors set in the profiles and I'd like to just switch between the 2 based on whether or not I'm SSH'd into a server.

kjmurphyjr commented Jul 5, 2014

This is great. Is it possible to simply change to a preset profile? I have the Solarized Light and Dark colors set in the profiles and I'd like to just switch between the 2 based on whether or not I'm SSH'd into a server.

@chrisbTakealot

This comment has been minimized.

Show comment
Hide comment
@chrisbTakealot

chrisbTakealot Aug 7, 2014

Thanks. Just what I was looking for and works perfect. Using iTerm on 10.9

chrisbTakealot commented Aug 7, 2014

Thanks. Just what I was looking for and works perfect. Using iTerm on 10.9

@jessefulton

This comment has been minimized.

Show comment
Hide comment
@jessefulton

jessefulton Jan 15, 2015

Awesome. Any idea how to handle a kill signal (ctrl c) during auth?

jessefulton commented Jan 15, 2015

Awesome. Any idea how to handle a kill signal (ctrl c) during auth?

@themoriarty

This comment has been minimized.

Show comment
Hide comment
@themoriarty

themoriarty commented Jun 4, 2015

@jessefulton this version handles ctrl-c during auth correctly: https://gist.github.com/themoriarty/584b759513c5c5864dac

@mihigh

This comment has been minimized.

Show comment
Hide comment
@mihigh

mihigh Dec 11, 2015

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

mihigh commented Dec 11, 2015

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

@lcguida

This comment has been minimized.

Show comment
Hide comment
@lcguida

lcguida Aug 10, 2016

Tried in iTerm2 v3.0.5 on El Capitain, get the following error: 44:52: syntax error: Expected end of line but found identifier. (-2741)

lcguida commented Aug 10, 2016

Tried in iTerm2 v3.0.5 on El Capitain, get the following error: 44:52: syntax error: Expected end of line but found identifier. (-2741)

@seamountain

This comment has been minimized.

Show comment
Hide comment
@seamountain

seamountain Aug 17, 2016

I got same syntax error but I found this solution. Changing tell the current terminal with tell the current window works for me.
This answer was posted at below links.

seamountain commented Aug 17, 2016

I got same syntax error but I found this solution. Changing tell the current terminal with tell the current window works for me.
This answer was posted at below links.

@drbh

This comment has been minimized.

Show comment
Hide comment
@drbh

drbh Nov 14, 2017

The AppleScript needs to be updated to the new iTerm AppleScript Syntax
Docs here: https://www.iterm2.com/documentation-scripting.html

Here is an updated function that allows you to set the background color with RGB and another function to set the color randomly

For a white background
set_term_bgcolor 255 255 255

For a random color
set_random_bgcolor

Add to ~/.bashrc

set_term_bgcolor(){
  local R=$1
  local G=$2
  local B=$3
  /usr/bin/osascript <<EOF
	tell application "iTerm"
	  tell current session of current window
	      set background color to {$(echo "scale=2; ($1/255.0)*65535" | bc),$(echo "scale=2; ($2/255.0)*65535" | bc),$(echo "scale=2; ($3/255.0)*65535" | bc)}
	  end tell
	end tell
EOF
}

set_random_bgcolor(){
	set_term_bgcolor $(echo $(echo 0."$RANDOM")*255 | bc) $(echo $(echo 0."$RANDOM")*255 | bc) $(echo $(echo 0."$RANDOM")*255 | bc)
}

drbh commented Nov 14, 2017

The AppleScript needs to be updated to the new iTerm AppleScript Syntax
Docs here: https://www.iterm2.com/documentation-scripting.html

Here is an updated function that allows you to set the background color with RGB and another function to set the color randomly

For a white background
set_term_bgcolor 255 255 255

For a random color
set_random_bgcolor

Add to ~/.bashrc

set_term_bgcolor(){
  local R=$1
  local G=$2
  local B=$3
  /usr/bin/osascript <<EOF
	tell application "iTerm"
	  tell current session of current window
	      set background color to {$(echo "scale=2; ($1/255.0)*65535" | bc),$(echo "scale=2; ($2/255.0)*65535" | bc),$(echo "scale=2; ($3/255.0)*65535" | bc)}
	  end tell
	end tell
EOF
}

set_random_bgcolor(){
	set_term_bgcolor $(echo $(echo 0."$RANDOM")*255 | bc) $(echo $(echo 0."$RANDOM")*255 | bc) $(echo $(echo 0."$RANDOM")*255 | bc)
}

@whod81

This comment has been minimized.

Show comment
Hide comment
@whod81

whod81 Jul 9, 2018

THe above solution was great but it was a bit dark for my purposes, so I modified for my own usage. Also for for some reason .bash_profile was the proper place.

`set_term_bgcolor(){
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm"
tell current session of current window
set background color to {$(echo "scale=2; ($1/255.0)*65535" | bc),$(echo "scale=2; ($2/255.0)*65535" | bc),$(echo "scale=2; ($3/255.0)*65535" | bc)}
end tell
end tell
EOF
}

set_random_bgcolor(){
local R=$(($RANDOM % 48))
local G=$(($RANDOM % 48))
local B=$(($RANDOM % 48))
set_term_bgcolor $R $G $B
}

set_random_bgcolor`

whod81 commented Jul 9, 2018

THe above solution was great but it was a bit dark for my purposes, so I modified for my own usage. Also for for some reason .bash_profile was the proper place.

`set_term_bgcolor(){
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm"
tell current session of current window
set background color to {$(echo "scale=2; ($1/255.0)*65535" | bc),$(echo "scale=2; ($2/255.0)*65535" | bc),$(echo "scale=2; ($3/255.0)*65535" | bc)}
end tell
end tell
EOF
}

set_random_bgcolor(){
local R=$(($RANDOM % 48))
local G=$(($RANDOM % 48))
local B=$(($RANDOM % 48))
set_term_bgcolor $R $G $B
}

set_random_bgcolor`

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