Skip to content

Instantly share code, notes, and snippets.

@anthonyclarka2
Created August 7, 2018 14:27
Show Gist options
  • Save anthonyclarka2/f2aae1e167c7899d7d263a3d89cd4349 to your computer and use it in GitHub Desktop.
Save anthonyclarka2/f2aae1e167c7899d7d263a3d89cd4349 to your computer and use it in GitHub Desktop.
Change the colours of your iTerm2 tabs based on hostname patterns
# A dotfile to turn iTerm2 tabs a different colour based on the hostname glob.
# Useful for marking different environments in different colours.
# Cargo-culted from multiple sources, apologies for not fully acknowledging them :(
# The colours you set are defined in a function that calls another pair of functions.
# Here I've used Solarized Dark, but of course you can use anything you like.
# Solarized colours
function tab_sol_yellow { set_iterm_title "$1"; tab_color 181 137 0; }
function tab_sol_orange { set_iterm_title "$1"; tab_color 203 75 22; }
function tab_sol_red { set_iterm_title "$1"; tab_color 220 50 47; }
function tab_sol_magenta { set_iterm_title "$1"; tab_color 211 54 130; }
function tab_sol_violet { set_iterm_title "$1"; tab_color 108 113 196; }
function tab_sol_blue { set_iterm_title "$1"; tab_color 38 139 210; }
function tab_sol_cyan { set_iterm_title "$1"; tab_color 42 161 152; }
function tab_sol_green { set_iterm_title "$1"; tab_color 133 153 0; }
title_help0()
{
echo "ERROR: No argument provided."
echo "Usage:"
echo " `basename -- $0` \"title\" to provide a new Terminal title."
}
title_help2()
{
echo "ERROR: Too many arguments provided."
echo "Usage:"
echo " `basename -- $0` \"title\" to provide a new Terminal title."
}
function set_iterm_title() {
if [ $# -eq 0 ]
then
title_help0;
elif [ $# -eq 1 ]
then
echo -ne "\033]0;$1\007"
elif [ $# -gt 1 ]
then
title_help2;
fi
}
alias title='set_iterm_title'
function titlepwd() {
set_iterm_title `pwd`
}
# If you have a different terminal application, then you will almost
# certainly need to use different escape codes
# https://iterm2.com/documentation-escape-codes.html
function tab_color() {
echo -n -e "\033]6;1;bg;red;brightness;$1\a"
echo -n -e "\033]6;1;bg;green;brightness;$2\a"
echo -n -e "\033]6;1;bg;blue;brightness;$3\a"
}
# iTerm2 tab color commands
if [[ -n "$ITERM_SESSION_ID" ]]; then
tab-reset() { echo -ne "\033]6;1;bg;*;default\a"; }
function iterm2_tab_precmd() {
tab-reset
}
function iterm2_tab_preexec() {
if [[ "$1" =~ "^ssh " ]]; then
# In this example, we're switching on the hostname argument given to ssh
# and relying on that hostname having the correct environment assigned.
# There's probably a much better way to do this, but I am not aware of how.
case $1 in
(*prod*)
tab_sol_red
;;
(*stage*)
tab_sol_orange
;;
(*uat*)
tab_sol_yellow
;;
(*dev*)
tab_sol_green
;;
*)
tab-reset
;;
esac
# Reset the tab if SSH isn't being used. If you
# want to set colours for other commands, then
# you'll need to modify this.
else
tab-reset
fi
}
# all of that work for the magic to happen in these next 3 lines:
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