Claude Code
Printed from:
Complete Claude Code Cheatsheet
Claude Code is Anthropic's official terminal coding agent — a peer of OpenAI Codex CLI, Aider, and Cursor — that reads/writes files in your project, runs shell commands, talks to Claude (currently Opus 4.6 / Sonnet 4.6 / Haiku 4.5), and supports MCP servers, custom slash commands, sub-agents, and hooks.
Table of Contents
- Installation
- Authentication
- Daily Use
- Slash Commands
- Keyboard Shortcuts
- Headless / Non-interactive Mode
- Permission Modes
CLAUDE.md(Project Memory)- Settings (
settings.json) - Custom Slash Commands
- Sub-agents
- Hooks
- MCP Servers
- Plugins
- IDE Integrations
- GitHub Actions / CI
- Models
- Cost & Context Management
- Troubleshooting
- Quick Reference
Installation
123456789101112# npm (cross-platform)
npm install -g @anthropic-ai/claude-code
# Homebrew
brew install --cask claude-code
# Direct binaries: https://github.com/anthropics/claude-code/releases
# Verify
claude --version
claude doctor # diagnose env, auth, dependencies
Requires Node 18+ for the npm path. Works on macOS, Linux, and Windows (WSL2 recommended on Windows).
Authentication
1234567891011121314151617181920# Anthropic API key (default)
export ANTHROPIC_API_KEY=sk-ant-...
# Or interactive OAuth (Claude Pro/Max plan subscribers)
claude login
claude logout
# Vertex AI
export CLAUDE_CODE_USE_VERTEX=1
export CLOUD_ML_REGION=us-east5
export ANTHROPIC_VERTEX_PROJECT_ID=my-gcp-project
# Amazon Bedrock
export CLAUDE_CODE_USE_BEDROCK=1
export AWS_REGION=us-west-2
# OpenAI-compatible / proxy endpoints
export ANTHROPIC_BASE_URL=https://my-proxy.example.com
export ANTHROPIC_AUTH_TOKEN=...
Daily Use
123456789101112cd ~/projects/my-app
claude # start REPL in current dir
claude "explain the auth flow" # seed an initial prompt
claude --continue # resume the last session
claude --resume # interactive session picker
claude --resume <session-id>
claude --cd ~/other-repo # work elsewhere
claude --model sonnet # pick model
claude --model opus
claude --model haiku
claude --add-dir ../shared # extra dirs the agent may read/write
Inside the REPL
Type a prompt and hit Enter. Use:
@pathto attach a file or directory!commandto run a shell command directly (no agent)#textto append a memory note toCLAUDE.md/commandfor slash commands (see below)
Slash Commands
123456789101112131415161718192021/help show help /init generate CLAUDE.md for this repo /clear clear chat (frees context) /compact [focus] summarise older turns; keep focus topic /cost show token + dollar usage so far /model [name] switch model in this session /permissions manage allow/deny rules /config edit settings /login (re-)login /logout /agents list / scaffold sub-agents /hooks list / configure hooks /mcp list / install MCP servers /plugin manage plugins /ide connect to your IDE (VS Code / JetBrains) /install-github-app install the GitHub PR/Issues app /release-notes what's new in Claude Code /bug file a bug /doctor run diagnostics /exit quit
User-defined commands appear as /your-command once you've added them — see Custom Slash Commands.
Keyboard Shortcuts
123456789101112Enter submit Shift+Enter newline (or paste-newline auto-detected) Esc interrupt the current model call Esc Esc undo the last user message Ctrl+C cancel current step; press twice to quit Ctrl+D quit Ctrl+L clear screen (keeps history) Ctrl+R reverse-search history Tab autocomplete file paths, slash commands, agent names Up / Down history Ctrl+J pop-out the multi-line editor in $EDITOR
Headless / Non-interactive Mode
For scripting and CI:
123456789101112131415161718192021# Single-shot prompt (prints final answer, exits)
claude -p "summarise CHANGELOG.md"
claude --print "lint and fix"
# Read prompt from stdin
git diff --cached | claude -p "Write a PR description with risk + tests."
# JSON output for tooling
claude -p "describe repo" --output-format json | jq .
claude -p "..." --output-format stream-json # NDJSON streaming
# Constrain tools / dirs in CI
claude -p "Update CHANGELOG for $TAG" \
--permission-mode acceptEdits \
--allowed-tools "Read,Edit,Write,Bash(git diff:*)" \
--add-dir docs
# Continue / resume in headless mode
claude -p --continue "now bump the version"
claude -p --resume <session-id> "..."
Exit codes: 0 success, non-zero on error.
Permission Modes
Claude Code asks before touching anything by default. Modes:
| Mode | Behaviour |
|---|---|
default | Ask before edits and shell. |
acceptEdits | Auto-approve file edits; still ask for shell. |
plan | Read-only "planning" mode — no edits, no shell. Useful first pass. |
bypassPermissions | Auto-approve everything. Dangerous; use in sandboxes only. |
1234claude --permission-mode plan
claude --permission-mode acceptEdits
claude --dangerously-skip-permissions # = bypassPermissions
Allow/deny rules (persisted via /permissions):
123Allow: Edit, Write, Read, Bash(git status:*), Bash(npm test:*) Deny: Bash(rm:*), WebFetch
CLAUDE.md (Project Memory)
Claude Code reads memory files automatically:
1234./CLAUDE.md project-level ./CLAUDE.local.md project-level, gitignored ~/.claude/CLAUDE.md global (all projects)
Bootstrap one with /init. Keep it short and specific.
123456789101112# CLAUDE.md
- Use pnpm, not npm.
- All new code requires vitest tests next to source in __tests__/.
- Don't touch /vendor, /generated, or the migrations folder.
- Style: functional React, Tailwind, no class components.
- Commit format: Conventional Commits.
## Build & test
- `pnpm dev` to run locally
- `pnpm test --run` for non-watch tests
- `pnpm lint && pnpm typecheck` before commit
Append a memory mid-conversation with #:
12> # always run `pnpm typecheck` after edits
Also respects .gitignore and an optional .claudeignore for paths Claude should not read.
Settings (settings.json)
1234~/.claude/settings.json user ./.claude/settings.json project (commit this) ./.claude/settings.local.json project, gitignored
123456789101112131415161718192021222324252627282930313233343536{
"model": "claude-sonnet-4-6",
"permissions": {
"allow": [
"Edit",
"Write",
"Read",
"Bash(git diff:*)",
"Bash(pnpm test:*)",
"Bash(pnpm lint:*)"
],
"deny": [ "Bash(rm:*)", "WebFetch" ],
"defaultMode": "acceptEdits"
},
"env": {
"NODE_ENV": "development"
},
"hooks": {
"PostToolUse": [
{ "matcher": "Edit|Write",
"hooks": [{ "type": "command", "command": "pnpm exec prettier --write \"$CLAUDE_FILE_PATHS\"" }] }
]
},
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${env:GITHUB_TOKEN}" }
}
},
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.sh"
}
}
Custom Slash Commands
Put markdown files in .claude/commands/ (project) or ~/.claude/commands/ (user). The filename becomes the command name.
12# .claude/commands/release-notes.md
12345678---
description: Draft release notes from staged commits.
allowed-tools: ["Bash(git log:*)", "Bash(git diff:*)", "Read"]
argument-hint: <version>
---
Look at the staged commits and write release notes for version $ARGUMENTS.
Use Conventional Commits to group features / fixes / chores.
Then in the REPL:
12/release-notes v1.4.0
Arguments are expanded via $ARGUMENTS (or $1, $2, …). Front-matter allowed-tools constrains what the model can call during that command.
Namespacing: subdirs become prefixes (.claude/commands/db/migrate.md → /db:migrate).
Sub-agents
Sub-agents are specialised personas with their own system prompt and tool allowlist. Store them in .claude/agents/<name>.md (project) or ~/.claude/agents/<name>.md (user).
123456789101112---
name: reviewer
description: Read-only code reviewer. Use after staging changes.
tools: ["Read", "Bash(git diff:*)", "Bash(git log:*)"]
model: opus
---
You are a senior staff engineer. Review the staged diff and produce:
1. A summary in 3 bullets.
2. Risk assessment.
3. Concrete suggestions with file:line refs.
Do not modify files.
Invoke:
12> use the reviewer agent on the staged diff
Or programmatically:
12claude -p --agent reviewer "Review the staged diff"
Manage with /agents.
Hooks
Hooks run shell commands on lifecycle events.
| Event | Fires when |
|---|---|
PreToolUse | Before a tool call (you can block it). |
PostToolUse | After a tool call succeeds. |
UserPromptSubmit | Each user message. |
SessionStart | Once per session. |
SessionEnd | When the session ends. |
Notification | On notifications (e.g. needs attention). |
Stop | When Claude finishes responding. |
SubagentStop | When a sub-agent finishes. |
1234567891011121314151617// .claude/settings.json
{
"hooks": {
"PostToolUse": [
{ "matcher": "Edit|Write",
"hooks": [{ "type": "command", "command": "pnpm exec prettier --write \"$CLAUDE_FILE_PATHS\"" }] }
],
"PreToolUse": [
{ "matcher": "Bash",
"hooks": [{ "type": "command", "command": "~/.claude/guard-rm.sh" }] }
],
"SessionEnd": [
{ "hooks": [{ "type": "command", "command": "git status -s | head" }] }
]
}
}
A hook with a non-zero exit blocks the action; stdout is shown to Claude. Useful env vars in hooks: $CLAUDE_FILE_PATHS, $CLAUDE_TOOL_NAME, $CLAUDE_SESSION_ID.
MCP Servers
Claude Code is an MCP client. Two ways to register servers:
Per-project, in .claude/settings.json
123456789101112131415161718{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/me/projects"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_..." }
},
"postgres": {
"command": "uvx",
"args": ["mcp-server-postgres", "postgresql://localhost/mydb"]
}
}
}
From the CLI
123456claude mcp add github npx -- -y @modelcontextprotocol/server-github claude mcp add --scope user github npx -- -y @modelcontextprotocol/server-github claude mcp list claude mcp remove github claude mcp restart github
In the REPL:
12/mcp list servers and tool inventories
Claude Code can also expose itself as an MCP server for other clients:
12claude mcp serve
Plugins
Plugins are installable bundles of slash commands, sub-agents, hooks, and MCP servers.
12345claude plugin install <repo-url-or-marketplace-id>
claude plugin list
claude plugin update
claude plugin remove <id>
Or via the REPL: /plugin.
Plugin layout (a plugin is just a repo or folder):
1234567891011my-plugin/ ├── plugin.json # name, description, version, entry points ├── commands/ │ └── deploy.md ├── agents/ │ └── reviewer.md ├── hooks/ │ └── settings.json └── mcp/ └── server.js
IDE Integrations
12/ide # connect to a running IDE (VS Code / JetBrains)
Or install:
- VS Code: marketplace extension "Claude Code".
- JetBrains: plugin marketplace search "Claude Code".
IDE integration adds: diff view for proposed edits, click-to-open file refs, selection-as-context, and a side-panel REPL.
GitHub Actions / CI
Install the GitHub App from the REPL:
12/install-github-app
Then in a workflow:
12345678910111213141516171819name: claude-review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: anthropics/claude-code-action@v1
with:
mode: review
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
allowed_tools: "Read,Bash(git diff:*)"
Headless from any CI:
1234- run: npm i -g @anthropic-ai/claude-code
- env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude -p --permission-mode acceptEdits \
--allowed-tools "Read,Edit,Write,Bash(git diff:*)" \
"Generate release notes for ${{ github.ref_name }}"
Models
12345678claude --model opus
claude --model sonnet
claude --model haiku
# Or pin an exact ID:
claude --model claude-opus-4-6
claude --model claude-sonnet-4-6
claude --model claude-haiku-4-5-20251001
Picks:
- Opus 4.6 — deepest reasoning, biggest refactors.
- Sonnet 4.6 — balanced default for most coding work.
- Haiku 4.5 — fastest/cheapest; great for mechanical edits.
/model switches mid-session.
Cost & Context Management
1234/cost totals for this session (input/output tokens, $) /compact <focus> summarise older turns, keep focus topic in view /clear reset chat history
Best practices:
- Use
--permission-mode planfor the first exploration pass. - Use
/compactonce a session gets long, instead of/clear, to keep continuity. - Add a
.claudeignoreto skip large generated dirs. - Reach for Haiku for mechanical edits; Opus when you need real design thinking.
Troubleshooting
123456789101112131415161718192021222324claude doctor # diagnostics
# Auth
claude logout && claude login
unset ANTHROPIC_API_KEY # if conflicting with OAuth
# Hanging request
# press Esc to interrupt the current model call
# Tool was denied unexpectedly
/permissions # inspect allow/deny rules
# MCP server not showing up
claude mcp list
claude mcp restart <name>
tail -f ~/.claude/logs/claude-code-*.log
# Sub-agent never invoked
/agents # is it listed? matching keyword in prompt?
# Reset everything
rm -rf ~/.claude
claude login
Logs: ~/.claude/logs/. Session transcripts: ~/.claude/sessions/.
Quick Reference
1234567891011121314151617181920212223242526# Daily
claude
claude "do X"
claude --continue
claude --resume
# Headless
claude -p "do X"
claude -p --permission-mode acceptEdits "do X"
claude -p --output-format json "do X" | jq
# Project
/init # create CLAUDE.md
$EDITOR CLAUDE.md
$EDITOR .claude/settings.json
$EDITOR .claude/commands/<name>.md
$EDITOR .claude/agents/<name>.md
# MCP / plugins
claude mcp add ...
claude plugin install ...
# Diagnostics
claude doctor
claude --version
Tip: keep a tight
CLAUDE.mdand a couple of project-scoped slash commands (e.g./test,/release-notes). It pays for itself within a week.
Continue Learning
Discover more cheatsheets to boost your productivity