Skip to content

Instantly share code, notes, and snippets.

@santtu
Created February 16, 2017 11:42
Show Gist options
  • Save santtu/e269470f844233b9c1c5175b5cced9f3 to your computer and use it in GitHub Desktop.
Save santtu/e269470f844233b9c1c5175b5cced9f3 to your computer and use it in GitHub Desktop.
Simple function and hook to put into .zshrc to change iterm2 tab color according to the current directory Raw
########################################################################
# Automatic iterm2 tab/title bar color based on current directory.
# ~/.ztabcolors syntax:
#
# DIRECTORY-PREFIX RED GREEN BLUE
# # COMMENT
#
# That's a *TAB* between directory prefix, and spaces between RGB
# values. RGB values are in decimal 0-255. Lines starting with # are
# comments. For example:
#
# / 0 0 255
# /tmp 255 255 255
#
# This picks the *longest* matching directory thus the previous will
# show BLUE tab for all directories except WHITE for /tmp.
function iterm2_tab_color() {
local color
if [ -f ~/.ztabcolors ]; then
color=$(egrep -v '^#' ~/.ztabcolors | awk -v OFS="\t" -F"\t" "{m=\"$PWD/\"; if (substr(m, 1, length(\$1)) == \$1) print length(\$1), \$2 }" | sort -rn | head -1 | cut -f2 -d" ")
fi
if [ -n "$color" ]; then
local colors=("${(@s/ /)color}")
echo -n -e "\033]6;1;bg;red;brightness;$colors[1]\a"
echo -n -e "\033]6;1;bg;green;brightness;$colors[2]\a"
echo -n -e "\033]6;1;bg;blue;brightness;$colors[3]\a"
else
echo -n -e "\033]6;1;bg;*;default\a"
fi
}
chpwd_functions=(${chpwd_functions[@]} "iterm2_tab_color")
# Run the hook immediately too, the initial shell doesn't get chdir
# hook.
iterm2_tab_color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment