Skip to content

Instantly share code, notes, and snippets.

@a0s
Created July 3, 2026 09:58
Show Gist options
  • Select an option

  • Save a0s/c18e98466c61591ed72daaae0afc0ac4 to your computer and use it in GitHub Desktop.

Select an option

Save a0s/c18e98466c61591ed72daaae0afc0ac4 to your computer and use it in GitHub Desktop.
iTerm2 color preset switching per directory via direnv

Switch iTerm2 color presets automatically when entering directories — no profile switching, just color presets. Works with any preset visible in Settings → Profiles → Colors → Color Presets.

How it works

• iterm_theme "Name" in .envrc exports ITERM_THEME via direnv • A precmd hook in zsh sends iTerm2's proprietary escape code SetColors=preset= on every prompt • When you leave the directory, direnv unsets ITERM_THEME and the hook reverts to your default preset

Setup

1. Add to ~/.direnvrc

# Usage in .envrc:  iterm_theme "Solarized Dark"
iterm_theme() {
  export ITERM_THEME="$1"
}

2. Add to ~/.zshrc — right after eval "$(direnv hook zsh)"

export ITERM_DEFAULT_THEME="Claude Dark"   # your default preset name

_iterm_sync_theme() {
  [[ "$TERM_PROGRAM" == "iTerm.app" ]] || return
  local want="${ITERM_THEME:-$ITERM_DEFAULT_THEME}"
  [[ "$want" == "$_ITERM_THEME_CUR" ]] && return
  printf '\e]1337;SetColors=preset=%s\a' "$want" > /dev/tty
  _ITERM_THEME_CUR="$want"
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd _iterm_sync_theme

3. Add .envrc to any directory

iterm_theme "Solarized Dark"

Then run direnv allow once.

Notes

• Preset names are case-sensitive and must match exactly • ITERM_DEFAULT_THEME is the preset to revert to when outside any themed directory — save your current colors as a named preset first if you haven't already (Color Presets → Save as…) • The hook only fires when the theme actually changes, so there's no flicker on every prompt • Works only inside iTerm2; silently skips in other terminals or over SSH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment