Skip to content

Instantly share code, notes, and snippets.

@thamam
Created April 5, 2026 16:07
Show Gist options
  • Select an option

  • Save thamam/5a3791e652565048a82b683a467faadf to your computer and use it in GitHub Desktop.

Select an option

Save thamam/5a3791e652565048a82b683a467faadf to your computer and use it in GitHub Desktop.
Dual Claude Code setup (work + personal) with direnv

Dual Claude Code Setup: Work + Personal Accounts with direnv


Section 1: The Problem and The Solution

Claude Code stores authentication, settings, conversation history, and project metadata in ~/.claude. If you use two accounts — say, a personal Max subscription and a work team account — both compete for the same config directory. You need a way to keep them separate.

The solution has two parts:

  1. CLAUDE_CONFIG_DIR — an environment variable that tells Claude Code where to read its config from. Point it at ~/.claude-work and Claude Code uses your work account; point it at ~/.claude-personal and it uses your personal account.

  2. direnv — a shell extension that automatically loads and unloads environment variables when you cd into a directory, so you never have to manually switch accounts.

Together, they give you automatic account switching based on which directory you are working in.


Section 2: How It Works

Diagram 1 — Automatic Switching (direnv)

flowchart LR
    A["cd ~/work/some-repo"] --> B["direnv detects .envrc"]
    B --> C["sources account-map.sh"]
    C --> D["matches ~/work/* \n sets ~/.claude-work"]
    D --> E["CLAUDE_CONFIG_DIR \n = ~/.claude-work"]
    E --> F["claude runs with \n work account"]
Loading

Diagram 2 — Manual Override (aliases)

flowchart LR
    A["run cc-work alias"] --> B["CLAUDE_CONFIG_DIR \n = ~/.claude-work"]
    B --> C["claude runs with \n work account"]
Loading

Section 3: Setup Guide

3.1 Create Separate Config Directories and Authenticate

Launch Claude Code once per account, pointing it at a dedicated config directory. Log in, then exit.

# Personal account
CLAUDE_CONFIG_DIR=~/.claude-personal claude
# Log in, then exit

# Work account
CLAUDE_CONFIG_DIR=~/.claude-work claude
# Log in, then exit

After this step, you have two independent config directories, each authenticated to a different account.

3.2 Organize Projects into Top-Level Directories

Create a clear separation between personal and work projects:

~/personal/       # Personal repos and projects
~/work/           # Work repos and projects

If you already have projects scattered elsewhere, symlink or move them into these directories.

3.3 Install direnv

brew install direnv

Add the hook to your shell config (~/.zshrc):

eval "$(direnv hook zsh)"

Reload your shell:

source ~/.zshrc

3.4 Create the Account-Mapping Script

Create ~/.claude/account-map.sh:

#!/bin/bash
case "$PWD" in
  "$HOME"/personal|"$HOME"/personal/*)  echo "$HOME/.claude-personal" ;;
  "$HOME"/work|"$HOME"/work/*)          echo "$HOME/.claude-work" ;;
  *)                                     echo "$HOME/.claude" ;;
esac

Make it executable:

chmod +x ~/.claude/account-map.sh

3.5 Create .envrc in Each Top-Level Directory

# ~/personal/.envrc
export CLAUDE_CONFIG_DIR=$(source ~/.claude/account-map.sh)
# ~/work/.envrc
export CLAUDE_CONFIG_DIR=$(source ~/.claude/account-map.sh)

3.6 Allow direnv

direnv requires explicit approval for each .envrc file:

direnv allow ~/personal
direnv allow ~/work

3.7 Verify

cd ~/work/some-repo
echo $CLAUDE_CONFIG_DIR
# Expected: /Users/<you>/.claude-work

cd ~/personal/my-project
echo $CLAUDE_CONFIG_DIR
# Expected: /Users/<you>/.claude-personal

That is it -- you are set up. The sections below are optional extras for power users.


Section 4: Manual Override Aliases

Add these to your ~/.zshrc for quick switching anywhere, regardless of direnv:

alias cc-who='echo "Claude config: ${CLAUDE_CONFIG_DIR:-~/.claude (default)}"'
alias cc-personal='export CLAUDE_CONFIG_DIR=~/.claude-personal && echo "Switched to: personal"'
alias cc-work='export CLAUDE_CONFIG_DIR=~/.claude-work && echo "Switched to: work"'
alias cc-default='export CLAUDE_CONFIG_DIR=~/.claude && echo "Switched to: default"'

Usage:

cc-who          # Check which account is active
cc-work         # Force work account in the current shell
cc-personal     # Force personal account in the current shell
cc-default      # Reset to default config

Note: manual overrides apply to the current shell session only. Opening a new terminal or cd-ing into a direnv-managed directory will revert to automatic switching.


Section 5: What Gets Isolated

Each config directory maintains its own:

  • Authentication — separate accounts and subscriptions
  • Conversation history — work and personal histories stay separate
  • Settings and plugins — independent settings.json per account
  • Project metadata — per-account project tracking
  • CLAUDE.md rules — per-account global instructions (e.g., ~/.claude-work/CLAUDE.md vs ~/.claude-personal/CLAUDE.md)

Section 6: Tips

  • Different CLAUDE.md per account. Your work account might enforce strict linting and commit conventions, while your personal account might be more relaxed. Place different CLAUDE.md files in each config directory.

  • Different plugins and MCP servers per account. Each config directory has its own settings.json, so you can configure separate MCP servers, API keys, and plugin settings for work vs personal.

  • Easy to extend. Adding a third account (e.g., freelance) is just another case in account-map.sh, another config directory, and another .envrc.

  • Pair with SSH host aliases. If you use separate GitHub accounts per identity, configure SSH host aliases (github.com-work, github.com-personal) so that git and claude both route through the correct credentials automatically.

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