ccswitch: multi-account switcher for Claude Code
Cross-platform fish plugin to save, list, and switch between multiple Claude Code accounts (including the same login across different orgs), then launch a session with the chosen profile. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
5 files changed
+402
No files matched your search
@@ -0,0 +1,3 @@
|
||||
# ide related
|
||||
.idea
|
||||
.vscode
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2026 Chen Lei
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,112 @@
|
||||
# ccswitch
|
||||
|
||||
A tiny [fish](https://fishshell.com) plugin to switch between multiple
|
||||
[Claude Code](https://claude.com/claude-code) accounts — personal, work,
|
||||
different orgs — and jump straight into a session.
|
||||
|
||||
```fish
|
||||
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
|
||||
ccswitch list # see every saved profile (* = active)
|
||||
```
|
||||
|
||||
## How it works
|
||||
|
||||
An account is two things that must travel together:
|
||||
|
||||
1. **OAuth tokens** — the real credential. On macOS these live in the login
|
||||
Keychain (`Claude Code-credentials`); on Linux/others in
|
||||
`~/.claude/.credentials.json`. `ccswitch` picks the right backend per platform.
|
||||
2. **Identity** — the `oauthAccount` object and `userID` inside `~/.claude.json`,
|
||||
which Claude Code validates the token against.
|
||||
|
||||
`ccswitch save <name>` snapshots both into a plain profile directory. Switching
|
||||
restores the credential into the platform store and splices only `oauthAccount`
|
||||
and `userID` back into `~/.claude.json` (backing it up first — the rest of that
|
||||
file is shared and left untouched). Because a profile is just two JSON files, it
|
||||
is portable across machines and operating systems.
|
||||
|
||||
## Install
|
||||
|
||||
With [fisher](https://github.com/jorgebucaran/fisher):
|
||||
|
||||
```fish
|
||||
fisher install mysqto/ccswitch
|
||||
```
|
||||
|
||||
Requires [`jq`](https://jqlang.github.io/jq/).
|
||||
|
||||
## Usage
|
||||
|
||||
| Command | Action |
|
||||
| --- | --- |
|
||||
| `ccswitch <name> [args...]` | Switch to `<name>`, then start `claude` (extra args passed through) |
|
||||
| `ccswitch use <name>` | Switch without launching |
|
||||
| `ccswitch save <name>` | Save the current account as `<name>` |
|
||||
| `ccswitch list` / `ls` | List profiles (`*` marks the active one) |
|
||||
| `ccswitch current` / `whoami` | Show the active account |
|
||||
| `ccswitch rm <name>` | Delete a profile |
|
||||
|
||||
Typical first run:
|
||||
|
||||
```fish
|
||||
claude # log in as your first account
|
||||
ccswitch save personal
|
||||
claude /logout # or log out via the app, then log in as the other account
|
||||
ccswitch save work
|
||||
ccswitch work # from now on, switch freely
|
||||
```
|
||||
|
||||
### 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`):
|
||||
|
||||
```fish
|
||||
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.
|
||||
|
||||
## Profile storage
|
||||
|
||||
Profiles live in `$CCSWITCH_HOME` (default `~/.claude/accounts`), one directory
|
||||
per profile:
|
||||
|
||||
```
|
||||
~/.claude/accounts/work/
|
||||
├── credentials.json # OAuth blob
|
||||
└── account.json # oauthAccount + userID
|
||||
```
|
||||
|
||||
Point `$CCSWITCH_HOME` at a synced folder or repo to move profiles between
|
||||
machines:
|
||||
|
||||
```fish
|
||||
set -Ux CCSWITCH_HOME ~/vault/claude-accounts
|
||||
```
|
||||
|
||||
## ⚠️ 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
|
||||
[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.
|
||||
|
||||
## Notes
|
||||
|
||||
- Quit any running `claude` session before switching — an open session can
|
||||
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.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
@@ -0,0 +1,26 @@
|
||||
function __ccswitch_profiles -d "list saved ccswitch profile names"
|
||||
set -l home
|
||||
if set -q CCSWITCH_HOME
|
||||
set home $CCSWITCH_HOME
|
||||
else
|
||||
set home "$HOME/.claude/accounts"
|
||||
end
|
||||
test -d "$home"; or return
|
||||
for dir in "$home"/*/
|
||||
test -f "$dir/account.json"; and basename "$dir"
|
||||
end
|
||||
end
|
||||
|
||||
complete -c ccswitch -f
|
||||
|
||||
# subcommands and bare profile names (first token only)
|
||||
complete -c ccswitch -n __fish_use_subcommand -a save -d "save current account"
|
||||
complete -c ccswitch -n __fish_use_subcommand -a use -d "switch without launching"
|
||||
complete -c ccswitch -n __fish_use_subcommand -a list -d "list profiles"
|
||||
complete -c ccswitch -n __fish_use_subcommand -a current -d "show active account"
|
||||
complete -c ccswitch -n __fish_use_subcommand -a rm -d "delete a profile"
|
||||
complete -c ccswitch -n __fish_use_subcommand -a help -d "show help"
|
||||
complete -c ccswitch -n __fish_use_subcommand -a "(__ccswitch_profiles)" -d "switch + launch"
|
||||
|
||||
# profile names as the argument to use/rm
|
||||
complete -c ccswitch -n "__fish_seen_subcommand_from use rm remove delete" -a "(__ccswitch_profiles)" -d profile
|
||||
@@ -0,0 +1,240 @@
|
||||
function ccswitch -d "switch between multiple Claude Code accounts"
|
||||
set -l home (__ccswitch_home)
|
||||
set -l config "$HOME/.claude.json"
|
||||
|
||||
set -l cmd $argv[1]
|
||||
switch "$cmd"
|
||||
case save
|
||||
__ccswitch_save "$home" "$config" $argv[2]
|
||||
case list ls
|
||||
__ccswitch_list "$home" "$config"
|
||||
case current whoami
|
||||
__ccswitch_current "$config"
|
||||
case use
|
||||
__ccswitch_use "$home" "$config" $argv[2]
|
||||
case rm remove delete
|
||||
__ccswitch_rm "$home" $argv[2]
|
||||
case '' -h --help help
|
||||
__ccswitch_help
|
||||
case '*'
|
||||
# bare profile name -> switch to it, then launch a claude session
|
||||
if test -d "$home/$cmd"
|
||||
__ccswitch_use "$home" "$config" "$cmd"
|
||||
and command claude $argv[2..-1]
|
||||
else
|
||||
echo "ccswitch: unknown command or profile '$cmd'" >&2
|
||||
echo "run 'ccswitch help' for usage, 'ccswitch list' to see profiles" >&2
|
||||
return 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function __ccswitch_home -d "resolve the profile store directory"
|
||||
if set -q CCSWITCH_HOME
|
||||
echo $CCSWITCH_HOME
|
||||
else
|
||||
echo "$HOME/.claude/accounts"
|
||||
end
|
||||
end
|
||||
|
||||
function __ccswitch_cred_read -d "print the current Claude Code OAuth credential blob"
|
||||
switch (uname)
|
||||
case Darwin
|
||||
security find-generic-password -s "Claude Code-credentials" -w 2>/dev/null
|
||||
case '*'
|
||||
if test -f "$HOME/.claude/.credentials.json"
|
||||
cat "$HOME/.claude/.credentials.json"
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function __ccswitch_cred_acct -d "print the keychain account attribute (macOS only)"
|
||||
security find-generic-password -s "Claude Code-credentials" -g 2>&1 \
|
||||
| string replace -rf '^.*"acct"<blob>="(.*)"$' '$1'
|
||||
end
|
||||
|
||||
function __ccswitch_cred_write -d "write an OAuth credential blob into the platform store"
|
||||
set -l blob $argv[1]
|
||||
set -l acct $argv[2]
|
||||
switch (uname)
|
||||
case Darwin
|
||||
test -z "$acct"; and set acct "$USER"
|
||||
security delete-generic-password -s "Claude Code-credentials" >/dev/null 2>&1
|
||||
security add-generic-password -U -a "$acct" -s "Claude Code-credentials" -w "$blob"
|
||||
case '*'
|
||||
mkdir -p "$HOME/.claude"
|
||||
printf '%s' "$blob" >"$HOME/.claude/.credentials.json"
|
||||
chmod 600 "$HOME/.claude/.credentials.json"
|
||||
end
|
||||
end
|
||||
|
||||
function __ccswitch_save -d "snapshot the current account into a profile"
|
||||
set -l home $argv[1]
|
||||
set -l config $argv[2]
|
||||
set -l name $argv[3]
|
||||
|
||||
if test -z "$name"
|
||||
echo "usage: ccswitch save <name>" >&2
|
||||
return 1
|
||||
end
|
||||
if contains -- "$name" save list ls current whoami use rm remove delete help
|
||||
echo "ccswitch: '$name' is a reserved word, pick another profile name" >&2
|
||||
return 1
|
||||
end
|
||||
if not test -f "$config"
|
||||
echo "ccswitch: $config not found — is Claude Code installed?" >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
set -l blob (__ccswitch_cred_read | string collect)
|
||||
if test -z "$blob"
|
||||
echo "ccswitch: no active credential found — log in with 'claude' first" >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
set -l dir "$home/$name"
|
||||
mkdir -p "$dir"
|
||||
chmod 700 "$home" "$dir"
|
||||
|
||||
printf '%s' "$blob" >"$dir/credentials.json"
|
||||
chmod 600 "$dir/credentials.json"
|
||||
|
||||
set -l acct ""
|
||||
test (uname) = Darwin; and set acct (__ccswitch_cred_acct)
|
||||
jq --arg acct "$acct" '{oauthAccount, userID, keychain_account: $acct}' "$config" >"$dir/account.json"
|
||||
chmod 600 "$dir/account.json"
|
||||
|
||||
set -l email (jq -r '.oauthAccount.emailAddress // "unknown"' "$dir/account.json")
|
||||
echo "saved profile '$name' ($email)"
|
||||
end
|
||||
|
||||
function __ccswitch_use -d "restore a profile as the active account"
|
||||
set -l home $argv[1]
|
||||
set -l config $argv[2]
|
||||
set -l name $argv[3]
|
||||
|
||||
if test -z "$name"
|
||||
echo "usage: ccswitch use <name>" >&2
|
||||
return 1
|
||||
end
|
||||
set -l dir "$home/$name"
|
||||
if not test -d "$dir"
|
||||
echo "ccswitch: profile '$name' not found (ccswitch list)" >&2
|
||||
return 1
|
||||
end
|
||||
if not test -f "$dir/credentials.json"; or not test -f "$dir/account.json"
|
||||
echo "ccswitch: profile '$name' is incomplete" >&2
|
||||
return 1
|
||||
end
|
||||
if not test -f "$config"
|
||||
echo "ccswitch: $config not found" >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
if command -sq pgrep; and pgrep -x claude >/dev/null 2>&1
|
||||
echo "ccswitch: warning — a running 'claude' may overwrite ~/.claude.json on exit; quit it first" >&2
|
||||
end
|
||||
|
||||
# restore the credential into the platform store
|
||||
set -l blob (cat "$dir/credentials.json" | string collect)
|
||||
set -l acct (jq -r '.keychain_account // ""' "$dir/account.json")
|
||||
if not __ccswitch_cred_write "$blob" "$acct"
|
||||
echo "ccswitch: failed to write credential" >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
# splice identity into ~/.claude.json (back it up first, never rewrite wholesale)
|
||||
cp "$config" "$config.ccswitch.bak"
|
||||
set -l tmp (mktemp)
|
||||
if jq --slurpfile a "$dir/account.json" \
|
||||
'.oauthAccount = $a[0].oauthAccount | .userID = $a[0].userID' \
|
||||
"$config" >"$tmp"
|
||||
mv "$tmp" "$config"
|
||||
chmod 600 "$config"
|
||||
else
|
||||
rm -f "$tmp"
|
||||
mv "$config.ccswitch.bak" "$config"
|
||||
echo "ccswitch: failed to patch $config (restored from backup)" >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
set -l email (jq -r '.oauthAccount.emailAddress // "unknown"' "$dir/account.json")
|
||||
echo "switched to '$name' ($email)"
|
||||
end
|
||||
|
||||
function __ccswitch_list -d "list saved profiles"
|
||||
set -l home $argv[1]
|
||||
set -l config $argv[2]
|
||||
|
||||
# a profile is identified by account *and* org (same login can span orgs)
|
||||
set -l active_key ""
|
||||
test -f "$config"; and set active_key (jq -r '(.oauthAccount.accountUuid // "") + "|" + (.oauthAccount.organizationUuid // "")' "$config")
|
||||
|
||||
set -l found 0
|
||||
if test -d "$home"
|
||||
for dir in "$home"/*/
|
||||
test -f "$dir/account.json"; or continue
|
||||
set found 1
|
||||
set -l name (basename "$dir")
|
||||
set -l email (jq -r '.oauthAccount.emailAddress // "unknown"' "$dir/account.json")
|
||||
set -l org (jq -r '.oauthAccount.organizationName // ""' "$dir/account.json")
|
||||
set -l key (jq -r '(.oauthAccount.accountUuid // "") + "|" + (.oauthAccount.organizationUuid // "")' "$dir/account.json")
|
||||
set -l mark " "
|
||||
test "$key" != "|"; and test "$key" = "$active_key"; and set mark "* "
|
||||
if test -n "$org"
|
||||
printf '%s%-16s %s (%s)\n' "$mark" "$name" "$email" "$org"
|
||||
else
|
||||
printf '%s%-16s %s\n' "$mark" "$name" "$email"
|
||||
end
|
||||
end
|
||||
end
|
||||
test $found -eq 0; and echo "no profiles yet — save one with 'ccswitch save <name>'"
|
||||
end
|
||||
|
||||
function __ccswitch_current -d "show the active account"
|
||||
set -l config $argv[1]
|
||||
if not test -f "$config"
|
||||
echo "ccswitch: $config not found" >&2
|
||||
return 1
|
||||
end
|
||||
set -l email (jq -r '.oauthAccount.emailAddress // "unknown"' "$config")
|
||||
set -l org (jq -r '.oauthAccount.organizationName // ""' "$config")
|
||||
if test -n "$org"
|
||||
echo "$email ($org)"
|
||||
else
|
||||
echo "$email"
|
||||
end
|
||||
end
|
||||
|
||||
function __ccswitch_rm -d "delete a saved profile"
|
||||
set -l home $argv[1]
|
||||
set -l name $argv[2]
|
||||
if test -z "$name"
|
||||
echo "usage: ccswitch rm <name>" >&2
|
||||
return 1
|
||||
end
|
||||
set -l dir "$home/$name"
|
||||
if not test -d "$dir"
|
||||
echo "ccswitch: profile '$name' not found" >&2
|
||||
return 1
|
||||
end
|
||||
rm -rf "$dir"
|
||||
echo "removed profile '$name'"
|
||||
end
|
||||
|
||||
function __ccswitch_help -d "show ccswitch usage"
|
||||
echo "ccswitch — switch between multiple Claude Code accounts"
|
||||
echo
|
||||
echo "usage:"
|
||||
echo " ccswitch <name> [args...] switch to <name> and start a claude session"
|
||||
echo " ccswitch use <name> switch to <name> without launching claude"
|
||||
echo " ccswitch save <name> save the current account as <name>"
|
||||
echo " ccswitch list | ls list saved profiles (* marks the active one)"
|
||||
echo " ccswitch current | whoami show the active account"
|
||||
echo " ccswitch rm <name> delete a saved profile"
|
||||
echo " ccswitch help show this help"
|
||||
echo
|
||||
echo "profiles live in \$CCSWITCH_HOME (default ~/.claude/accounts)."
|
||||
end
|
||||
Reference in new issue
Block a user