Skip to content

Instantly share code, notes, and snippets.

@LukaPrebil
Created April 8, 2026 06:19
Show Gist options
  • Select an option

  • Save LukaPrebil/97e9d8a9b01c3e5cf784443b142d5965 to your computer and use it in GitHub Desktop.

Select an option

Save LukaPrebil/97e9d8a9b01c3e5cf784443b142d5965 to your computer and use it in GitHub Desktop.
Switch Claude Code accounts automatically based on directory

Switching Claude Code accounts by directory

Claude Code uses ~/.claude/ as its config directory by default, which includes your login credentials in .claude.json. You can override this with the CLAUDE_CONFIG_DIR environment variable to point at a different config directory, enabling automatic account switching based on where you run claude.

Setup

1. Create a separate config directory for your second account:

mkdir ~/.claude-personal

2. Log in to the second account in that config directory:

CLAUDE_CONFIG_DIR="$HOME/.claude-personal" claude login

3. Add a wrapper function to your ~/.zshrc (or ~/.bashrc):

claude() {
  if [[ "$PWD" == "$HOME/code/personal"* ]]; then
    CLAUDE_CONFIG_DIR="$HOME/.claude-personal" command claude "$@"
  else
    command claude "$@"
  fi
}

Adjust the path pattern to match whichever directories should use the alternate account. You can add multiple conditions:

claude() {
  if [[ "$PWD" == "$HOME/code/personal"* || "$PWD" == "$HOME/side-projects"* ]]; then
    CLAUDE_CONFIG_DIR="$HOME/.claude-personal" command claude "$@"
  else
    command claude "$@"
  fi
}

4. Reload your shell:

source ~/.zshrc

How it works

  • CLAUDE_CONFIG_DIR tells Claude Code where to find its config, credentials, session data, and settings
  • The wrapper function checks your current directory and sets the env var inline before invoking claude
  • command claude bypasses the function itself to call the real claude binary, avoiding infinite recursion
  • Each config directory is fully independent — separate login, settings, history, and MCP config

Verifying

Run claude from each directory type and check which account is active. You can also verify by checking which .claude.json is being read:

# Should use team account
cd ~/code/work && claude --version

# Should use personal account
cd ~/code/personal && claude --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment