Tint the iTerm2 window background based on which top-level project folder you're in, so you can tell terminal tabs and windows apart at a glance.
Any directory under ~/projects/<name>/… gets a stable dark background — from a
blue / purple / maroon / green family chosen by hashing <name> — with light,
faintly-tinted text. Every tab and window inside the same project gets the same
color (it's derived from the name, so there's no shared state). Leaving ~/projects
resets to your iTerm2 profile default.
The color changes automatically on every cd.
- iTerm2 (uses its proprietary
OSC 1337 SetColorsescape sequences). - zsh.
- Standard
awkandcksum(preinstalled on macOS).
-
Copy the script to your home directory:
cp project-colors.zsh ~/.project-colors.zsh -
Source it from
~/.zshrc(add near the end):# Tint iTerm2 background per top-level ~/projects folder [ -f ~/.project-colors.zsh ] && source ~/.project-colors.zsh
-
Open a new iTerm2 tab (or run
source ~/.zshrc).
If the color doesn't change, set Settings → Profiles → Colors → Minimum Contrast
to 0 so it doesn't override the foreground color.
cd ~/projects/drums # background -> dark blue, light text
cd ~/projects/drums/src # same color (inherits the top-level name)
cd ~ # resets to your profile defaultpc-previewPrints a swatch for every folder directly under ~/projects, painted in that
project's actual background + foreground, followed by the hex values.
Set these before the source line in ~/.zshrc:
-
Different projects root:
PROJECTS_ROOT="$HOME/code"
Tweak the palette by editing __pc_colors in the script:
hues=(222 278 348 140)— the four base hues (blue, purple, maroon, green).0.55— background saturation.14 + … % 8— background lightness range (0.14–0.21); lower = darker.fg=$(__pc_hsl_to_hex "$hue" 0.20 0.90)— the light text color.
- Key = the first folder under
~/projects(${rel%%/*}), so all subfolders inherit the parent's color. - Stable color =
cksumhash of that name → a family + hue/lightness jitter, so it's identical across every tab, window, and machine. - Readable pairing = very dark background (HSL lightness ~0.14–0.21) with a light same-hue foreground (~0.90).
- Triggers on
cdvia zsh'schpwdhook; resets to the profile default when you leave~/projects.
Escape sequences are wrapped in tmux passthrough when $TMUX is set. You may also
need this in ~/.tmux.conf:
set -g allow-passthrough on