Claude Code Cheat Sheet
import { Aside, Card, CardGrid } from ‘@astrojs/starlight/components’;
Quick Reference — All Commands
Section titled “Quick Reference — All Commands”CLI Commands (Terminal)
Section titled “CLI Commands (Terminal)”| Command | Purpose | Example |
|---|---|---|
claude | Start interactive REPL | claude |
claude -p "prompt" | One-shot mode | claude -p "explain this error" |
claude --model sonnet | Start with specific model | claude --model opus |
claude --version | Check installed version | claude --version |
cat file | claude -p "..." | Pipe mode — feed file content | cat log.txt | claude -p "summarize" |
claude config | Manage configuration | claude config |
claude -c / claude --continue | Continue most recent conversation | claude -c |
claude -r / claude --resume | Resume specific session by ID/name | claude -r "auth-refactor" |
claude update | Update to latest version | claude update |
claude mcp | Configure MCP servers | claude mcp add my-server |
claude doctor | Diagnose installation issues | claude doctor |
claude -p --output-format json | JSON output for automation | claude -p --output-format json "list files" |
claude -p --max-turns 3 | Limit agentic turns | claude -p --max-turns 3 "fix lint" |
claude -p --max-budget-usd 5 | Set dollar spending limit | claude -p --max-budget-usd 5 "refactor" |
claude --allowedTools "Read" "Bash(git *)" | Restrict tool access | claude --allowedTools "Read" |
claude --system-prompt "..." | Custom system prompt | claude --system-prompt "You are a reviewer" |
Slash Commands (Inside REPL)
Section titled “Slash Commands (Inside REPL)”| Command | What It Does | When to Use |
|---|---|---|
/help | List all available commands | Forgot syntax? Start here |
/init | Create CLAUDE.md for project | First time in a new project |
/compact | Compress conversation history | Context at 50-70% — frees 30-70% tokens |
/compact [focus] | Compact with focus instruction | /compact Preserve the auth decisions |
/clear | Erase ALL context, start fresh | Switching to unrelated project |
/cost | Show token usage and estimated cost | Every 20-30 minutes to track spending |
/model | Switch AI model mid-session | Change between Haiku/Sonnet/Opus |
/status | Show version, model, account info | Quick session overview |
/context | Visual context usage grid | See how full your context window is |
/memory | Edit CLAUDE.md memory files | Update project memory without leaving session |
/resume | Resume session or show picker | Return to a previous conversation |
/doctor | Check installation health | Diagnose issues with your setup |
/terminal-setup | Install terminal keybindings | Enable Shift+Enter for multi-line input |
/vim | Enable vim mode | For vim users |
/bug | Report a bug | Send bug report to Anthropic |
/export | Export conversation to file | Save session for later reference |
/exit or Ctrl+C | Exit session | Done working |
Keyboard Shortcuts
Section titled “Keyboard Shortcuts”| Shortcut | Action |
|---|---|
Escape | Cancel current generation |
Escape × 2 | Rewind — show message selector to undo |
Ctrl+C | Interrupt / exit |
Ctrl+V | Paste image from clipboard (macOS: Ctrl+V, NOT Cmd+V) |
Ctrl+G | Open prompt in external editor ($EDITOR) |
Ctrl+L | Clear terminal screen |
Ctrl+O | Toggle verbose output |
Shift+Tab | Cycle permission modes (Normal → Auto-Accept → Plan) |
Option+T / Alt+T | Toggle extended thinking |
Option+P / Alt+P | Switch model |
Tab | Accept prompt suggestion |
Up / Down | Navigate prompt history |
Input Shortcuts
Section titled “Input Shortcuts”| Method | How |
|---|---|
| Multi-line input | \ + Enter, or Shift+Enter (after /terminal-setup) |
| Bash mode | ! npm test — run shell commands without AI interpretation |
| File reference | @src/utils/auth.js — autocomplete with Tab |
| Paste image | Ctrl+V (clipboard screenshot) |
Mode Selection Guide
Section titled “Mode Selection Guide”| Scenario | Mode | Command |
|---|---|---|
| Multi-turn debugging | REPL | claude |
| Quick question | One-shot | claude -p "..." |
| Review a diff | Pipe | git diff | claude -p "review" |
| Process a log file | Pipe | cat errors.log | claude -p "analyze" |
| CI/CD integration | One-shot | Script with -p flag |
| Learning / exploring | REPL | claude |
Model Selection
Section titled “Model Selection”| Model | Cost | Best For |
|---|---|---|
| Haiku | $ | Formatting, typos, simple edits, quick answers |
| Sonnet | $$ | Features, debugging, code review, documentation |
| Opus | $$$ | Architecture, complex debugging, security, novel problems |
Best Practices for CLAUDE.md
Section titled “Best Practices for CLAUDE.md”The 6 Essential Sections
Section titled “The 6 Essential Sections”Every project CLAUDE.md should include these sections:
# Project: [Name]
## 1. Project OverviewTech stack: Node.js 20, Express, PostgreSQL, RedisArchitecture: Monorepo with packages/ directory
## 2. Architecture Rules- Routes → Services → Repositories → Database- All business logic in services, never in routes- Shared types in packages/shared/
## 3. Coding Conventions- Files: kebab-case (user-service.ts)- Classes: PascalCase (UserService)- Functions: camelCase (getUserById)- Error handling: Promise<Result<T, AppError>>- Imports: Absolute via @/ prefix
## 4. Commandsnpm run dev # Start dev server (port 3000)npm test # Run all testsnpm run lint # ESLint + Prettier checknpm run db:migrate # Run database migrations
## 5. Constraints (DO NOT)- Do NOT use `any` type — always define proper types- Do NOT install dependencies without asking first- Do NOT put business logic in route handlers- Do NOT commit .env files- Do NOT modify database schema without migration
## 6. Context (Tribal Knowledge)- Redis keys MUST include version prefix: v1:user:123- JWT expires in 15 min, refresh token in 7 days- Legacy /api/v1 routes maintained for mobile app compatibility- Payment gateway sandbox mode uses TEST_ prefix keysCLAUDE.md Size Guide
Section titled “CLAUDE.md Size Guide”| Size | Tokens | Quality |
|---|---|---|
| 300-500 words | ~400-650 | Minimal — misses edge cases |
| 500-800 words | ~650-1000 | Sweet spot for most projects |
| 800-1200 words | ~1000-1500 | Comprehensive — large projects |
| 1200+ words | 1500+ | Too large — diminishing returns |
File Hierarchy
Section titled “File Hierarchy”| Location | Scope | Priority |
|---|---|---|
~/.claude/CLAUDE.md | Global (all projects) | Lowest |
./CLAUDE.md | Project root | Main — use this |
./src/CLAUDE.md | Directory-level | Highest (overrides root) |
File Patterns Guide
Section titled “File Patterns Guide”.gitignore — Protect Secrets
Section titled “.gitignore — Protect Secrets”# Secrets — NEVER commit.env.env.local.env.*.local
# Dependenciesnode_modules/vendor/.venv/
# Build outputdist/build/.next/
# OS files.DS_StoreThumbs.db.env.example Pattern — Safe for Claude
Section titled “.env.example Pattern — Safe for Claude”# .env.example — committed to git, safe for Claude to readDATABASE_URL=postgresql://user:password@localhost:5432/dbnameREDIS_URL=redis://:password@localhost:6379API_KEY=your_api_key_hereSTRIPE_KEY=sk_test_placeholder
# .env — real secrets, gitignored, NEVER read by Claude# DATABASE_URL=postgresql://realuser:s3cr3t@prod.db:5432/prodPermission Quick Reference
Section titled “Permission Quick Reference”| Command Category | Action |
|---|---|
ls, pwd, cat, head, tail | Approve freely — read-only |
grep, find | Approve freely — search only |
git status, git log, git diff | Approve freely — read-only git |
git commit -m "..." | Review message, then approve |
npm install <package> | Verify package name first |
File writes in src/ | Verify path and content |
rm, git push --force | Always deny unless intentional |
curl, wget | Always deny — data exfiltration risk |
Operations on ~/.ssh/, ~/.aws/ | Always deny — sensitive directories |
Secret Scanner Setup (Gitleaks)
Section titled “Secret Scanner Setup (Gitleaks)”# Installbrew install gitleaks
# Add pre-commit hookcat > .git/hooks/pre-commit << 'HOOK'#!/bin/bashgitleaks protect --staged --verboseif [ $? -ne 0 ]; then echo "SECRETS DETECTED! Commit blocked." exit 1fiecho "No secrets detected."exit 0HOOKchmod +x .git/hooks/pre-commitContext Management
Section titled “Context Management”Token Cost Estimates
Section titled “Token Cost Estimates”| Content Type | Tokens per 1000 chars |
|---|---|
| English prose | ~250 |
| Source code | ~400 |
| JSON data | ~400 |
| Small file (<5KB) | 1,000-2,000 total |
| Medium file (5-20KB) | 2,000-5,000 total |
| Large file (>50KB) | 10,000+ total |
Context Health Monitor
Section titled “Context Health Monitor”| Level | Quality | Action |
|---|---|---|
| 0-50% | Excellent | Continue working |
| 50-70% | Good | Plan to /compact soon |
| 70-85% | Degrading | Run /compact now |
| 85-95% | Poor | Must compact or clear |
| 95%+ | Unreliable | /clear and restart |
Power Combos
Section titled “Power Combos”| Combo | Workflow | When |
|---|---|---|
/cost → /compact | Check → compress | Routine maintenance (every 30 min) |
/clear → /init | Reset → configure | Starting new project |
/compact → work → /cost | Compress → continue → verify | Long sessions |
Workflow Quick Starts
Section titled “Workflow Quick Starts”New Project Setup
Section titled “New Project Setup”cd my-projectclaude # Start session/init # Create CLAUDE.md# Edit CLAUDE.md with 6 sections above/cost # Check baseline usageLong Session Protocol
Section titled “Long Session Protocol”# Every 20-30 minutes:/cost # Check context level
# At 50-70% context:/compact # Free space, keep task context
# Switching to unrelated task:/clear # Full resetSafe Code Review
Section titled “Safe Code Review”git diff main | claude -p "Review this diff for bugs, security issues, and style"Context-Efficient Exploration
Section titled “Context-Efficient Exploration”# Step 1: Overview first"Show me the directory structure of src/"
# Step 2: Targeted reading"Show me function signatures in src/auth/"
# Step 3: Deep dive only where needed"Read src/auth/login.ts — I need to understand the token flow"