Files
ccswitch/README.md
T

446 lines
18 KiB
Markdown
Raw Normal View History

# ccswitch
2026-07-08 17:17:01 +08:00
**Switch between multiple Claude Code accounts — personal, work, different
orgs — and jump straight into a session.**
2026-07-08 17:17:01 +08:00
`ccswitch` is a small, fast, cross-shell command-line tool. An account is two
things that must travel together: the OAuth credential (in the macOS Keychain,
falling back to `~/.claude/.credentials.json`) and the identity Claude Code
2026-07-08 17:17:01 +08:00
validates it against (`oauthAccount` + `userID` inside `~/.claude.json`).
`ccswitch` snapshots both into a plain profile directory and restores them as a
unit, so one command becomes an account and drops you into `claude`.
```sh
ccswitch work # switch to the "work" account and start claude
ccswitch personal -c # switch to "personal", launch claude with -c
ccswitch use work # just switch, don't launch
2026-07-08 17:17:01 +08:00
ccswitch list # every saved profile (* = active)
```
2026-07-08 17:17:01 +08:00
- **One binary, every shell.** A single Rust binary with completions for bash,
zsh, fish, and PowerShell.
- **Token-rotation safe.** OAuth refresh tokens rotate per *login*, shared
across every org that login can see; `ccswitch` re-snapshots the outgoing
credential into every sibling profile on each switch, so an actively used
account never goes stale.
- **Concurrent sessions.** Run two accounts at once in separate terminals with
isolated config dirs but shared project memory and history.
- **Session recall.** `ccswitch search` bridges to
[`csx`](https://github.com/mysqto/csx) + `fzf` to fuzzy-pick and resume any
past session.
2026-07-08 17:17:01 +08:00
> This Rust CLI supersedes the original fish plugin, now preserved under
> [`legacy-fish/`](legacy-fish/). The commands are the same; the tool is now a
> single portable binary rather than a fish-only function.
2026-07-08 17:17:01 +08:00
---
## Install
2026-07-08 17:17:01 +08:00
### Homebrew (macOS & Linux)
2026-07-08 17:17:01 +08:00
```sh
brew tap mysqto/ccswitch https://github.com/mysqto/ccswitch
brew trust mysqto/ccswitch # newer Homebrew requires trusting a third-party tap
2026-07-08 17:17:01 +08:00
brew install ccswitch # formula (recommended) — also installs shell completions
# or:
brew install --cask ccswitch # cask — installs the binary only (no completions)
```
> Recent Homebrew refuses to load a cask/formula from an untrusted tap until you
> run `brew trust <tap>` (a one-time consent per tap). If you skip it you'll see
> "Refusing to load … from untrusted tap"; `brew trust mysqto/ccswitch` fixes it.
2026-07-08 17:17:01 +08:00
The tap ships **both** a formula and a cask, auto-regenerated with real
checksums on each release. Prefer `brew install ccswitch` (the **formula**): it
runs `ccswitch completions {bash,zsh,fish}` during install and drops each script
into Homebrew's completion directories, so tab-completion works right away. The
`--cask` route installs just the binary — use it if you don't want the formula,
and set up completions yourself (see [Shell completions](#shell-completions)). Install one,
not both — they each provide a `ccswitch` binary.
2026-07-08 17:17:01 +08:00
### From source with Cargo
```sh
cargo install --git https://github.com/mysqto/ccswitch # or, in a clone: cargo install --path .
```
### Prebuilt binary
Download the tarball for your platform from the [latest release][releases],
extract it, and put `ccswitch` on your `PATH`. Assets are named
`ccswitch-<tag>-<target>.tar.gz`, each with a matching `.sha256`:
| Platform | Target |
| --- | --- |
| macOS Apple Silicon | `aarch64-apple-darwin` |
| macOS Intel | `x86_64-apple-darwin` |
| Linux x86_64 | `x86_64-unknown-linux-gnu` |
| Linux ARM64 | `aarch64-unknown-linux-gnu` |
Every method produces a single `ccswitch` binary with no runtime dependencies
(the optional `search` bridge aside).
[releases]: https://github.com/mysqto/ccswitch/releases/latest
---
## Commands
Run `ccswitch` (or `ccswitch help`) for usage. Every command is below.
| Command | Action |
| --- | --- |
| `ccswitch <name> [args...]` | Switch to `<name>`, then start `claude` (extra args passed through) |
| `ccswitch use <name>` | Switch without launching |
2026-07-08 17:17:01 +08:00
| `ccswitch add <name> [--force]` | Sign in to a new account (`claude auth login`) and save it as `<name>` |
| `ccswitch save <name> [--force]` | Snapshot the current account as `<name>` |
| `ccswitch isolate <name> [args...]` | Run a **concurrent** session isolated to `<name>` (own credential, shared memory) |
| `ccswitch isolate` | With no name, list existing isolated profiles |
| `ccswitch seed [dir]` | Seed the shared isolate memory from `~/.claude` (or `dir`) |
| `ccswitch search [scope...]` | Fuzzy-pick a past session (via `csx` + `fzf`) and resume it |
| `ccswitch list` | List saved profiles (`*` marks the active one) |
| `ccswitch current` | Show the active account |
| `ccswitch rm <name>` | Delete a saved profile |
| `ccswitch completions <shell>` | Print a completion script to stdout |
| `ccswitch help` | Show usage |
2026-07-08 17:17:01 +08:00
**Aliases:** `iso` = `isolate`, `s` = `search`, `ls` = `list`, `whoami` =
`current`, `remove`/`delete` = `rm`.
2026-07-08 17:17:01 +08:00
**`--force`** (on `add` and `save`) overwrites an existing profile of the same
name; without it, saving over an existing profile is refused.
2026-07-08 17:17:01 +08:00
Profile names may not collide with a reserved subcommand word (`save`, `add`,
`isolate`, `iso`, `seed`, `search`, `s`, `list`, `ls`, `current`, `whoami`,
`use`, `rm`, `remove`, `delete`, `help`).
### Environment
| Variable | Purpose | Default |
| --- | --- | --- |
| `CCSWITCH_HOME` | Where saved profiles live. | `accounts/` inside Claude Code's config dir |
| `CCSWITCH_ISOLATE_HOME` | Where isolated profiles + shared memory live. | `profiles/` inside Claude Code's config dir |
| `CCSWITCH_CREDENTIALS` | Credential backend. `file` (or `plaintext`) skips the macOS Keychain and uses the plaintext fallback only. | auto |
`ccswitch` also *reads* Claude Code's own variables so it always addresses the
same account Claude Code would — see [Multiple config
directories](#multiple-config-directories).
| Variable | Effect |
| --- | --- |
| `CLAUDE_CONFIG_DIR` | Moves `.claude.json`, the credential fallback, and the Keychain item. |
| `CLAUDE_SECURESTORAGE_CONFIG_DIR` | Moves only the credential store. |
| `CLAUDE_CODE_CUSTOM_OAUTH_URL` | Adds a `-custom-oauth` infix to both. |
2026-07-08 17:17:01 +08:00
---
## Shell completions
`ccswitch completions <shell>` prints a completion script to stdout for
`bash`, `zsh`, `fish`, `powershell` (and `elvish`). Install it once for your
shell:
**bash** — add to `~/.bashrc`:
```sh
source <(ccswitch completions bash)
```
2026-07-08 17:17:01 +08:00
**zsh** — write into a directory on your `$fpath` (before `compinit`):
```sh
ccswitch completions zsh > "${fpath[1]}/_ccswitch"
```
**fish**:
```fish
ccswitch completions fish > ~/.config/fish/completions/ccswitch.fish
```
**PowerShell** — add to your `$PROFILE`:
```powershell
ccswitch completions powershell | Out-String | Invoke-Expression
```
---
## Walkthrough
A complete run, from a fresh install to switching daily.
### 1. Save the account you're already signed into
2026-07-08 17:17:01 +08:00
```sh
claude auth status # confirm who you're logged in as
ccswitch save personal # snapshot the active account as "personal"
ccswitch list
# * personal you@gmail.com
```
The `*` marks the currently active account.
### 2. Add a second account
`ccswitch add` signs you in to another account and saves it in one step:
2026-07-08 17:17:01 +08:00
```sh
ccswitch add work # runs `claude auth login`, then saves it as "work"
```
Both are now saved:
2026-07-08 17:17:01 +08:00
```sh
ccswitch list
# personal you@gmail.com
# * work you@company.com (Acme)
```
### 3. Switch and work — the daily loop
2026-07-08 17:17:01 +08:00
```sh
ccswitch work # become "work" and start a claude session
ccswitch personal -c # become "personal", continue the last conversation
ccswitch use work # just switch, don't launch anything
ccswitch current # you@company.com (Acme)
```
One command becomes an account and drops you into a session. Switching also
re-snapshots the account you're leaving, so rotating tokens never go stale.
2026-07-08 17:17:01 +08:00
### Same login, multiple orgs
If one login (e.g. a Claude Team account) belongs to several organizations,
each org is a separate profile — Claude Code issues a distinct org-scoped token
per org. Save one after switching org inside Claude Code (`/login`):
2026-07-08 17:17:01 +08:00
```sh
ccswitch save org-a # while the first org is active
# switch org in Claude Code, then:
ccswitch save org-b
```
`ccswitch list` distinguishes them by account **and** organization, so the same
email can appear more than once with only the active org starred.
2026-07-08 17:17:01 +08:00
---
2026-07-08 17:17:01 +08:00
## Concurrent sessions (isolate + seed)
`ccswitch <name>` swaps a single machine-global account, so it is **sequential**
— one active account at a time. To run **two accounts at once** (two terminals),
use `ccswitch isolate`, which gives each profile its own
[`CLAUDE_CONFIG_DIR`](https://code.claude.com/docs/en/settings):
2026-07-08 17:17:01 +08:00
```sh
ccswitch seed # once: copy your ~/.claude memory + history into shared/
ccswitch isolate work # terminal 1 — sign in once as work
ccswitch isolate personal # terminal 2 — sign in once as personal, at the same time
```
Each profile keeps its **own credential and identity**, but memory and history
are **shared** — `isolate` symlinks `projects/` (session transcripts + the
`memory/` files), `history.jsonl`, and `CLAUDE.md` from each profile to a common
`shared/` directory. So both accounts see the same project memory and past
sessions while staying logged in as different users.
2026-07-08 17:17:01 +08:00
Layout (centralized under `~/.claude/profiles`, override with
`$CCSWITCH_ISOLATE_HOME`):
```
~/.claude/profiles/
├── shared/ # one real copy of the shared memory/history
│ ├── projects/
│ ├── history.jsonl
│ └── CLAUDE.md
├── work/ # CLAUDE_CONFIG_DIR for "work"
│ ├── projects -> ../shared/projects
│ ├── history.jsonl -> ../shared/history.jsonl
│ ├── CLAUDE.md -> ../shared/CLAUDE.md
│ ├── .claude.json # own account identity (NOT shared)
│ └── settings.json # own settings
└── personal/ # CLAUDE_CONFIG_DIR for "personal" (same shared links)
```
`ccswitch isolate` with no name lists existing isolated profiles. Existing
non-symlink files in a profile dir are left untouched (never clobbered).
2026-07-08 17:17:01 +08:00
**Seeding.** `shared/` starts empty — isolated profiles begin with fresh
memory. Carry your existing memory/history over once:
2026-07-08 17:17:01 +08:00
```sh
ccswitch seed # copies CLAUDE.md, history.jsonl and projects/ from ~/.claude
ccswitch seed <dir> # ... or from another config dir
```
Re-run `ccswitch seed` anytime to re-sync from the source (source wins on
2026-07-08 17:17:01 +08:00
same-named files). While `shared/` is empty, `ccswitch isolate` warns and asks
for confirmation before launching, so you don't start with no memory by
accident. The prompt stops once `shared/` has content.
2026-07-08 17:17:01 +08:00
> This pattern relies on `CLAUDE_CONFIG_DIR` (supported) plus symlinking of
> account-agnostic paths (a community pattern, not officially documented). Auth
> stays isolated; only memory/history is shared. Two sessions writing the same
> project's memory simultaneously can race — low risk in practice.
2026-07-08 17:17:01 +08:00
---
## Search past sessions
`ccswitch search` (alias `s`) is an optional bridge to
[`csx`](https://github.com/mysqto/csx), a local index of your AI-coding
sessions. It fuzzy-picks a session and resumes it in the tool that produced it:
```sh
ccswitch search # sessions for the active tool → fzf → resume
ccswitch search --repo payments # any csx scope flag passes straight through
ccswitch search --tool codex # pick from Codex sessions instead
```
It shells out to `csx sessions --json` (defaulting the scope to the tool
reported by `csx current`), previews each transcript with `csx show <id>` in
`fzf`, and resumes the pick — `claude --resume <id>` for Claude Code, `codex
resume <id>` for Codex. Requires `csx` and `fzf` on `PATH`. If either is
missing, every other `ccswitch` command still works — this subcommand just
prints a hint.
---
## Multiple config directories
`$CLAUDE_CONFIG_DIR` does not merely move `~/.claude`. It also moves
`.claude.json`, and — the part that is easy to miss — it **renames the Keychain
item**: Claude Code namespaces the service with the first eight hex characters
of the directory's SHA-256.
```text
unset → "Claude Code-credentials"
CLAUDE_CONFIG_DIR=/w → "Claude Code-credentials-<sha256(/w)[0..8]>"
```
So a tool that ignores the variable does not just read the wrong config file —
it reads and writes a *different account's* credential. `ccswitch` computes the
same service name, so every command addresses exactly the account Claude Code
would in the same environment.
This is not an exotic case: `ccswitch isolate` launches `claude` with
`CLAUDE_CONFIG_DIR` set to the isolate directory, so any `ccswitch` run from
inside an isolated session is affected.
Profile and isolate storage follow the config directory too, so a given
`$CLAUDE_CONFIG_DIR` gets its own set of profiles. Point `$CCSWITCH_HOME` at a
fixed path if you would rather share one set across all of them.
> Claude Code normalizes the directory to Unicode NFC before hashing.
> `ccswitch` hashes the path as given, which agrees for every ASCII path and in
> practice everything short of a decomposed non-ASCII path. If you have one,
> set `CCSWITCH_CREDENTIALS=file` or use an ASCII path.
---
## Profile storage
Profiles live in `$CCSWITCH_HOME` (default `accounts/` inside Claude Code's
config directory, i.e. `~/.claude/accounts`), one directory per profile:
```
~/.claude/accounts/work/
├── credentials.json # OAuth blob
2026-07-08 17:17:01 +08:00
└── account.json # oauthAccount + userID (+ keychain account attr)
```
Point `$CCSWITCH_HOME` at a synced folder or repo to move profiles between
2026-07-08 17:17:01 +08:00
machines.
2026-07-08 17:17:01 +08:00
### ⚠️ These files are secrets
`credentials.json` holds a live bearer token. If you sync `$CCSWITCH_HOME` with
git or a cloud folder, **encrypt it** — use a private repo with
2026-07-08 17:17:01 +08:00
[git-crypt](https://github.com/AGWA/git-crypt) /
[age](https://github.com/FiloSottile/age) /
[SOPS](https://github.com/getsops/sops), or a real secret manager. Never commit
plaintext tokens to a shared or public repo.
On macOS the token is hex-encoded and piped to `security -i` rather than passed
as a command-line argument, so it is not visible in `ps` to other users on the
machine while a switch runs. (A blob too large for `security`'s stdin limit
falls back to argv, exactly as Claude Code does.)
---
## Headless and SSH machines
Claude Code does not keep its credential in the macOS Keychain unconditionally.
Its store is a composite: it reads the Keychain first and falls back to
plaintext `~/.claude/.credentials.json`, and when a Keychain write fails it
moves the credential into that file instead. On a Mac you only ever reach over
SSH, the login Keychain cannot be unlocked without a GUI prompt — `security`
exits 36, `errSecInteractionNotAllowed` — so Claude Code is living entirely in
the plaintext file. Linux and Windows always use the file.
`ccswitch` mirrors that arbitration exactly, so the two never disagree about
which copy is authoritative:
- **Reading** — the Keychain wins. If it is empty *or* unreachable, the file is
read, so `ccswitch save` on a headless box snapshots the credential Claude
Code is actually using instead of reporting you as signed out.
- **Writing** — the Keychain is tried first and, on success, the plaintext file
is deleted: left in place it is a stale credential waiting to resurface the
next time the Keychain is unreachable. If the Keychain write fails, the file
is written and the Keychain item dropped, so a stale Keychain copy cannot
shadow the switch from a GUI session.
Without this, a switch over SSH left `~/.claude.json` naming the account you
switched *to* while the live token still belonged to the account you switched
*from* — which Claude Code reports as not logged in.
If every `security` call on a machine is a dead end anyway, set
`CCSWITCH_CREDENTIALS=file` to skip the Keychain entirely.
> On a Mac used **both** at the console and over SSH, expect the credential to
> migrate to the plaintext file the first time you switch over SSH, and to
> return to the Keychain the first time you switch at the console. That is
> Claude Code's own behavior; `ccswitch` keeps exactly one authoritative copy
> either way. `~/.claude/.credentials.json` is written `0600`, but it is a
> bearer token in plaintext — the same trade Claude Code makes.
2026-07-08 17:17:01 +08:00
---
## Notes
- Quit any running `claude` session before switching — an open session can
2026-07-08 17:17:01 +08:00
rewrite `~/.claude.json` on exit and clobber the swap. `ccswitch` warns when
it detects one.
- Access tokens expire, but the refresh token is restored too, so Claude Code
re-refreshes automatically after a switch.
- OAuth refresh tokens rotate on every use. To keep snapshots valid, `ccswitch`
re-captures the **outgoing profile** (its own credential only) on each switch,
so an account you actively use won't go stale.
- **Same login, multiple orgs:** a Claude Code OAuth token is bound
server-side to the organization it was minted under — org selection is *not*
purely client-side. Each org profile therefore needs its own token: run
`claude /login` and pick the org, then `ccswitch save <name>`. `ccswitch`
keeps each `(account, org)` profile's token separate and never copies one
org's token onto another. (If you use two orgs of the same login heavily, note
that Anthropic may rotate/invalidate the shared account's refresh token; if a
profile's token goes dead, re-`save` it after a fresh `claude /login`.)
2026-07-08 17:17:01 +08:00
---
## Development
```sh
cargo build
cargo test
cargo fmt
cargo clippy --all-targets --all-features -- -D warnings
```
Architecture, the ports-and-adapters layout, and the coverage discipline are
documented in [`AGENTS.md`](AGENTS.md).
## License
2026-07-08 17:17:01 +08:00
MIT — see [`LICENSE`](LICENSE). Copyright (c) 2026 Chen Lei.