How to change color profiles on ssh iterm2 on SSH?

Very late answer, but I've found a way here that I wanted to share. This assumes you're running Zsh and oh-my-zsh but with some shell scripting experience you should be able to adjust this to other shells.

I've copied these instructions here for posterity:


Create two iTerm profiles:

*   Your personal preference theme (can be named anything)
*   SSH color theme (must be named SSH)

Go into your ~/.oh-my-zsh/custom directory and create a new file entitled iTerm2-ssh.zsh. Copy and paste the following or copy from the repo:

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="YOUR_CUSTOM_PROFILE_NAME_HERE"
    echo -e "\033]50;SetProfile=$NAME\a"
}

function colorssh() {
    if [[ -n "$ITERM_SESSION_ID" ]]; then
        trap "tab-reset" INT EXIT
        if [[ "$*" =~ "web*|production|ec2-.*compute-1" ]]; then
            tabc SSH
        fi
    fi
    ssh $*
}
compdef _ssh tabc=ssh

alias ssh="colorssh"

The breakdown of this code:

tabc() grabs the ssh name after the command $ ssh is entered. This changes the SetProfile name to ssh.

tab-reset() is responsible for when you exit the ssh session to return back to a profile name of your choosing. Remember to create a custom profile name and replace the _YOUR_CUSTOM_PROFILE_NAME_HERE_ with your profile name.

colorssh determines when to change the SetProfile name. Currently it will change to the SSH profile when one of the following values exist after $ ssh:

-   web *
-   production
-   ec2-.*compute-1

Again, not my code, just a happy user. Thanks for this one, Hector Leiva!