Skip to content

Instantly share code, notes, and snippets.

@beporter
Last active January 9, 2020 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beporter/bee28f9edb994b1c50c6a27af284ae38 to your computer and use it in GitHub Desktop.
Save beporter/bee28f9edb994b1c50c6a27af284ae38 to your computer and use it in GitHub Desktop.
Set iTerm2 tab color based on current working directory name ($PWD).
# The follow snippets will automatically change your iTerm2 tab color when you enter a configured directory.
#
# Installation:
#
# Add the function definitions in this file to your `~/.bash_profile` or `~/.profile`.
#
# Configuring your command prompt to include $(_iterm_tab_color).
# For example:
# PS1="\[$(iterm2_prompt_mark; _iterm_tab_color)\]${PS1}"
#
# You can configure a color for your current directory by running
# `_set_cwd_tab_color r g b` (where `r`, `g` and `b` are numbers between 0 and 255.)
#
# (You should also consider adding `.iterm_tab_color` to your `~/.gitignore_global` file.)
#
# See also: https://iterm2.com/documentation-escape-codes.html
_tab_color_rgb() {
local R=${1?'Provide red brightness (0-255) as first arg.'}
local G=${2?'Provide green brightness (0-255) as second arg.'}
local B=${3?'Provide blue brightness (0-255) as third arg.'}
echo -ne "\033]6;1;bg;red;brightness;${R}\a"
echo -ne "\033]6;1;bg;green;brightness;${G}\a"
echo -ne "\033]6;1;bg;blue;brightness;${B}\a"
}
_tab_color_reset() {
echo -ne "\033]6;1;bg;*;default\a"
}
_read_cwd_tab_color() {
if [ -r '.iterm_tab_color' ]; then
echo $(< .iterm_tab_color)
fi
}
_set_cwd_tab_color() {
local R=${1?'Provide red brightness (0-255) as first arg.'}
local G=${2?'Provide green brightness (0-255) as second arg.'}
local B=${3?'Provide blue brightness (0-255) as third arg.'}
echo "$R $G $B" > '.iterm_tab_color'
}
_iterm_tab_color() {
local OVERRIDE="$(_read_cwd_tab_color)"
if [ -n "$OVERRIDE" ]; then
_tab_color_rgb $OVERRIDE
else
local DIR="$(basename "$PWD")"
case "$DIR" in
# You can also specify dir colors here to avoid loose files everywhere.
# Example:
# your_dir_basename_here) _tab_color_rgb 255 0 0 ;;
*) _tab_color_reset ;;
esac
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment