Change terminal color when SSH from OS-X
My terminal
- Use iTerm2 as my terminal
- zsh as the shell
- oh-my-zsh to manage zsh configurations
- More info - my customised terminal
Background
- In day today developments and deployments we have to ssh into different machines/servers
-
For an instance we may have to ssh into
- Production servers
- Staging servers
- Amazon boxes
- Local testing servers etc..
Usual behaviour is open new terminal tab to and starting ssh session
Sometimes we may have multiple ssh session in different terminal tabs simultaneously
In these kind of situations its bit confuse to identifies where I am(which host currently I am in)
To solve this confusing issue we can use different terminal themes(terminal colors) for different ssh sessions
iTerm profiles with oh-my-zsh
- In my scenario I'm using different iTerm profiles with different themes(colors)
- I'm loading these terminal profiles when ssh into different environments
- Its(terminal profile selecting) done by using custom oh-my-zsh command(shell script)
-
In my scenario I'm using three different iTerm profiles(with different colors) for ssh session
- Production ssh session - dark red color
- Staging ssh session - dark purple color
- Other ssh session - dark blue color
You can use different iTerm themes to obtain different terminal colors - iTerm themes
Following are the steps that need to follow in order to change the terminal color when SSH'ing
1. Create iTerm terminal profiles
- Goto iTerm preferences and create profiles according to your ssh environments
- In my scenario I have 4 iTerm profiles
-
Default
profile uses in local terminal and other three profiles(Production
,Staging
andOther
) use in different ssh sessions
2. Create custom shell script
- Goto
~/.oh-my-zsh/custom
and create a fileiTrem2-ssh.zsh
- Put following content into
iTrem2-ssh.zsh
file
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 tab-reset() {
NAME="Default"
echo -e "\033]50;SetProfile=$NAME\a"
}
function colorssh() {
if [[ -n "$ITERM_SESSION_ID" ]]; then
trap "tab-reset" INT EXIT
if [[ "$*" =~ "production*" ]]; then
tabc Production
elif [[ "$*" =~ "staging*" ]]; then
tabc Staging
else
tabc Other
fi
fi
ssh $*
}
compdef _ssh tabc=ssh
alias ssh="colorssh"
-
colorssh()
function selecting different terminal profile according to ssh'ing host -
It selects
-
Production
profile when ssh to production server (ssh eranga@production_box
) -
Staging
profile when ssh to staging server(ssh eranga@staging_box
) -
Other
profile when ssh to any other server(local server, amazon box etc)
Change
tabc <profile-name>
according to your profile names -
tab-reset()
function reset the terminal profile toDefault
profile when exit from the ssh sessionalias ssh="colorssh"
creates an alias to ssh(when executessh
from the terminal it calls tocolorssh
function)You can get this code from my github
Example terminal session
- Default terminal
- SSH into production (
ssh eranga@production_box1
)
- SSH into staging (
ssh eranga@staging_box1
)
- SSH into other server (
ssh eranga@10.2.4.201
)
Reference
Written by eranga bandara
Related protips
1 Response
Thanks, I failed to setup Automatic Profile Switching and this is what I was looking for.