iTerm & CLI-Coding: Automated Profile & Theme Switching

Adding themes & automatic switching to your AI Agent's iTerm sessions
iTerm & CLI-Coding: Automated Profile & Theme Switching

I prefer to run my LLM coder agents via a CLI instead of an IDE or using the desktop applications. I find the CLI faster and less resource intensive than running the app's fat clients.

But with this, comes the problem since I'm running multiple AI agent coding sessions, I want to ensure I'm in the right window/session when I have all the agents open

Before & After Preview

Below is my default iTerm session using Rose-pine Moon theme, and the Claude Dark theme being applied when I start a claude session

Walkthrough Instructions

The following guide will walk us through how to:

  • Create a custom iTerm profile / theme per coder
  • Configure your Shell to automate the switching when using claude , codex , gemini or any other command, and to revert when exiting the session
  • Testing it.

Step 1. iTerm Profiles

  1. Open iTerm2.app
  2. Open Settings (or hit CMD + ,)
  3. Navigate to Settings > Profiles and select '+' in the bottom left to create a new profile
  4. The first profile will be for claude-code and will be named agent-claude
    1. Profile Name: agent-claude
    2. Profile Type: Terminal
    3. Title: Profile: Profile & Session Name
    4. Icon: Custom (find a .png at dashboard icons)
  5. Switch to colors and select 'Color Presets', you can import iterm themes
    1. Go to https://iterm2colorschemes.com/ to download themes. The's a Dark Claude theme I'm using for my Claude profile
  6. To create others, right click the newly created profile and rename it, upload a new icon and ensure the theme is differently coloured.

Now that your themes are ready, next is to configure your Shell to auto-switch themes.

Step 2. Updating your Shell Profile

I'm running .zsh but if you're running bash (default MacOS shell) then edit your ~/.bashrc file instead.

  1. Edit your Shell file via iTerm.app or an app like SublimeText.app
    1. zsh: sudo vi ~/.zshrc
    2. Bash: sudo vi ~/.bashrc
  2. Append the following code was added to my .zshrc file.
# iTerm profile switch
_iterm_profile() { printf '\033]1337;SetProfile=%s\007' "$1"; }
_iterm_active=""

_iterm_precmd_restore() {
[[ -n $_iterm_active ]] || return
_iterm_profile Default
_iterm_active=""
}

autoload -Uz add-zsh-hook
add-zsh-hook precmd _iterm_precmd_restore

_iterm_run() {
local p=$1 c=$2; shift 2
_iterm_active=$p; _iterm_profile "$p"
command "$c" "$@"
local rc=$?
_iterm_precmd_restore
return $rc
}

claude()  { _iterm_run agent-claude  claude  "$@"; }
#codex()   { _iterm_run agent-codex   codex   "$@"; }
#gemini()  { _iterm_run agent-gemini  gemini  "$@"; }
#copilot() { _iterm_run agent-copilot copilot "$@"; }

Code Snippet. Remove the Hashtags and/or update the 'agent-names'

Note: Make sure the name of the profile matches what is named in iTerm. I'm using agent-claude for my profile names. I've commented out codex/gemini/copilot. Remove the Hashes if you'd like to use them. Also my default profile is named "Default", you may need to change that too.

  1. Save to file using esc then typing :wq! (Or google how to save&quit vim for the umpteenth time)
  2. Reapply the new shell session using source
    1. zsh: source ~/.zshrc
    2. bash: source ~/.bashrc
  3. Now test with opening Claude by typing claude (Assuming you have Claude Code installed) - The session window should change themes
  4. Now test reverting / exiting the session with a opt+c, it should revert back to your default session.
Voilá
My Gemini & CoPilot could do with some more differences but I'm happy enough for the time being.

Member discussion