Last active
October 30, 2024 14:18
-
-
Save aclarknexient/84ebe33c1879f92168530487528802c4 to your computer and use it in GitHub Desktop.
Set iTerm2 tab colours based on the command you are running
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A dotfile to turn iTerm2 tabs a different colour based on the command glob. | |
# Useful for marking different environments in different colours. | |
# Cargo-culted from multiple sources, apologies for not fully acknowledging them :( | |
# Edit the line that reads "if [[ "$1" =~ "^(login-helper) " ]]; then" and replace | |
# "login-helper" with whatever command you use to authenticate. Then modify the case | |
# statement to match your auth command's arguments. For example: you may use a script | |
# called "authsaml" to authenticate, and its arguments are dev, test, preprd, and prd. | |
# Change the colour function you call if you want to change tab colours. | |
# This basic framework can also be used to execute environment setup functions too. | |
# For example: setting environment variables, copying files, etc etc. | |
# Save this as ~/.iterm-colours.zsh then call it from your .zshrc with: | |
# [[ -f ~/.iterm-colours.zsh ]] && source ~/.iterm-colours.zsh | |
# The colours you set are defined in a function that calls another pair of functions. | |
# Here I've used Solarized Dark, One Dark, and a Dichromatic-friendly palette, | |
# but of course you can use anything you like. | |
# To see colours automatically in VSCode, use the "Color Highlight" extension. | |
# Solarized colours https://gist.github.com/ninrod/b0f86d77ebadaccf7d9d4431dd8e2983 | |
function tab_sol_yellow { set_iterm_title "$1"; tab_color 181 137 0; } #b58900 | |
function tab_sol_orange { set_iterm_title "$1"; tab_color 203 75 22; } #cb4b16 | |
function tab_sol_red { set_iterm_title "$1"; tab_color 220 50 47; } #d30102 | |
function tab_sol_magenta { set_iterm_title "$1"; tab_color 211 54 130; } #d33682 | |
function tab_sol_violet { set_iterm_title "$1"; tab_color 108 113 196; } #6c71c4 | |
function tab_sol_blue { set_iterm_title "$1"; tab_color 38 139 210; } #268bd2 | |
function tab_sol_cyan { set_iterm_title "$1"; tab_color 42 161 152; } #2aa198 | |
function tab_sol_green { set_iterm_title "$1"; tab_color 133 153 0; } #859900 | |
# One Dark colours, from https://github.com/joshdick/onedark.vim/blob/main/colors/onedark.vim | |
function tab_one_yellow { set_iterm_title "$1"; tab_color 229 192 123; } #e5c07b | |
function tab_one_orange { set_iterm_title "$1"; tab_color 209 154 102; } #d19a66 | |
function tab_one_red { set_iterm_title "$1"; tab_color 224 108 117; } #e06c75 | |
function tab_one_magenta { set_iterm_title "$1"; tab_color 198 120 221; } #c678dd | |
function tab_one_blue { set_iterm_title "$1"; tab_color 97 175 239; } #61afef | |
function tab_one_cyan { set_iterm_title "$1"; tab_color 86 182 194; } #56b6c2 | |
function tab_one_green { set_iterm_title "$1"; tab_color 152 195 121; } #98c379 | |
# More colorblind-friendly, from https://github.com/romainl/vim-dichromatic | |
function tab_dichro_liblue { set_iterm_title "$1"; tab_color 136 204 238; } #88ccee | |
function tab_dichro_yellow { set_iterm_title "$1"; tab_color 221 204 119; } #ddcc77 | |
function tab_dichro_cyan { set_iterm_title "$1"; tab_color 68 170 153; } #44aa99 | |
function tab_dichro_khaki { set_iterm_title "$1"; tab_color 153 153 51; } #999933 | |
function tab_dichro_lipink { set_iterm_title "$1"; tab_color 204 102 119; } #cc6677 | |
function tab_dichro_purple { set_iterm_title "$1"; tab_color 170 68 153; } #aa4499 | |
function tab_dichro_dkblue { set_iterm_title "$1"; tab_color 51 34 136; } #332288 | |
function tab_dichro_green { set_iterm_title "$1"; tab_color 17 119 51; } #117733 | |
function tab_dichro_dkpink { set_iterm_title "$1"; tab_color 136 34 85; } #882255 | |
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" =~ "^(login-helper) " ]]; then | |
# Here we are switching on the AWS profile argument given to our helper | |
# functions. | |
case $1 in | |
(*prod*) | |
tab_one_red | |
;; | |
(*stage*) | |
tab_one_orange | |
;; | |
(*qa*) | |
tab_one_yellow | |
;; | |
(*dev*) | |
tab_one_green | |
;; | |
*) | |
tab-reset | |
;; | |
esac | |
# Reset the tab if needed. | |
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
First a screenshot of the solarized colours:

Here's a screenshot of the one dark colours:

And here's one for the colourblind dichromatic colours:

(Font is Iosevka Term Light, prompt is powerlevel10k