Skip to content

Instantly share code, notes, and snippets.

@andremacola
Forked from wadey/iterm2.zsh
Last active November 3, 2024 02:22
Show Gist options
  • Save andremacola/12337455f5dc43a3aa348a5aebcacbcf to your computer and use it in GitHub Desktop.
Save andremacola/12337455f5dc43a3aa348a5aebcacbcf to your computer and use it in GitHub Desktop.
Change iTerm2 tab color when using SSH
# Usage:
# source iterm2.zsh
# iTerm2 tab color commands
# https://iterm2.com/documentation-escape-codes.html
if [[ -n "$ITERM_SESSION_ID" ]]; then
declare -A SERVERS_TAB_COLORS=(
["srv1.domain.tld"]="255 165 0"
["srv2.domain.tld"]="0 14 156"
["srv2.domain.tld"]="0 105 255"
["srv4.domain.tld"]="199 70 52"
)
declare -A SERVERS_BG_COLORS=(
["srv1.domain.tld"]="422a00"
["srv2.domain.tld"]="000969"
["srv3.domain.tld"]="00273f"
["srv4.domain.tld"]="420800"
)
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"
}
tab-reset() { echo -ne "\033]6;1;bg;*;default\a" }
tab-label() { echo -ne "\033]1;$1\a" } # not working properly
background-color() { echo -ne "\033]Ph$1\033\\" }
background-reset() { echo -ne "\033]Ph131218\033\\" }
function iterm2_tab_precmd() {
tab-reset
background-reset
# tab-label ""
}
function iterm2_tab_preexec() {
if [[ "$1" =~ ^ssh ]]; then
local server=$(echo "$1" | awk '{print $2}' | awk -F'@' '{print $NF}')
if [[ -n "${SERVERS_TAB_COLORS[$server]}" ]]; then
tab-color ${${(s: :)SERVERS_TAB_COLORS[$server]}}
background-color ${${(s: :)SERVERS_BG_COLORS[$server]}}
# tab-label "$server"
else
tab-reset
background-reset
# tab-label ""
fi
fi
}
autoload -U add-zsh-hook
add-zsh-hook precmd iterm2_tab_precmd
add-zsh-hook preexec iterm2_tab_preexec
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment