Claude Code Cheatsheet: Commands, Hooks & Subagents
The complete Claude Code reference: every slash command, keyboard shortcut, hook, subagent, and CLAUDE.md tip — with real examples for developers.
On this page
Most developers use Claude Code like a fancy autocomplete. They type a request, read the answer, and move on. That's maybe 20% of what the tool can do.
Claude Code is a terminal agent with full project access. It reads files, runs shell commands, commits to git, delegates work to parallel subagents, and enforces rules through lifecycle hooks. Once you know the full feature set, the way you work changes. This cheatsheet covers everything: every slash command, every shortcut, CLAUDE.md, hooks, subagents, and PR review — with working examples throughout.
What Is Claude Code and How Do You Start It
Claude Code is Anthropic's official CLI AI assistant. Unlike IDE-embedded tools, it runs in your terminal with full access to your project: it reads and writes files, executes shell commands, interacts with git, and connects to external services through MCP servers.
Installing it takes one command.
# Install globally
npm install -g @anthropic-ai/claude-code
# Navigate to your project
cd my-project
# Start a session
claude
# On first run, generate project config
/init/init reads your project structure and generates a CLAUDE.md file plus a .claude/ directory. That CLAUDE.md becomes Claude's instruction manual for every future session in that project. Get that file right and the rest of your sessions will be significantly more consistent.
Slash Commands: The Full Reference
Type / on an empty prompt and press Tab to see all available commands, including any custom ones you have defined. Here is the full built-in set.
| Command | What it does | When to use it |
|---|---|---|
| /init | Generates CLAUDE.md and the .claude/ folder | First run in a new project |
| /compact | Compresses conversation history to a short summary | Context window above 80% full |
| /rewind | Rolls back Claude's last change | Claude broke something, fast undo |
| /plan | Claude thinks through a plan without making changes | Before large refactors or risky edits |
| /goal | Set a finish condition — Claude works autonomously until met | Long tasks you want to walk away from |
| /context | Shows current context usage as a percentage | Check memory pressure mid-session |
| /hooks | Interactive setup for lifecycle hooks | Configure enforcement rules without editing JSON manually |
| /agents | Manage subagents for parallel task delegation | Split a large task across multiple Claude instances |
| /code-review | Review code at a specified effort level | Before merges, with effort: low | medium | high | max |
| /install-github-app | Connect Claude to GitHub for PR integration | One-time setup for PR review workflow |
| /clear | Clear conversation history entirely | Fresh start with no carry-over context |
| /help | Show available commands and usage | Getting oriented or checking syntax |
Four of these commands deserve a bit more explanation because they change how you run longer sessions.
/compact— Claude's context window has a fixed limit. When you hit 80%, older parts of your conversation get fuzzy./compactreplaces the full history with a dense summary so you can keep working without starting over. Use it proactively — don't wait until Claude starts forgetting things./rewind—Esc Escdoes the same thing interactively, but/rewindis useful in scripts or when you want to be deliberate. It rolls back the last file write or shell command Claude executed./plan— Makes Claude reason through a multi-step problem and show you the plan before touching anything. Essential before any refactor that spans more than two files./goal— Sets an autonomous objective. Claude works through the task step by step and stops when the goal condition is met. Pair this with hooks (covered below) to enforce safe execution boundaries.
Keyboard Shortcuts Worth Memorizing
| Shortcut | What It Does |
|---|---|
| Esc Esc | Undo Claude's last change — fastest way to recover from a bad edit |
| Ctrl+B | Send the current task to background and free your terminal for other work |
| Ctrl+V | Paste a screenshot directly into chat — Claude reads the image and responds |
| Ctrl+C | Cancel what Claude is doing right now — stops mid-execution |
| # text | Add a note to Claude's memory without sending a message to the model |
| @ path | Autocomplete a file path inline — quicker than typing full paths |
| ! command | Run a shell command and include its output in context automatically |
The ! prefix is underused. Typing !npm test pulls the test output directly into context so Claude can reason about failures without you copy-pasting. Ctrl+V for screenshots works especially well for sharing browser error states or design references.
CLAUDE.md: The File That Tells Claude Who It Is
CLAUDE.md is a Markdown file at the root of your project (or in .claude/) that Claude reads at the start of every session. It's your project's standing instructions: stack details, naming conventions, commands to run, rules to follow.
Here is a practical example for an API project.
# My API Project
## Stack
Node.js 22, Express, PostgreSQL, Drizzle ORM. No TypeScript.
## Commands
- Start: npm run dev
- Test: npm test
- Lint: npm run lint
## Rules
- Never commit .env files
- Ask before deleting any file
- Write JSDoc comments on all exported functionsKeep CLAUDE.md under 500 lines. When rule sets grow large, split them into separate files under .claude/rules/ and reference them from the main CLAUDE.md — Claude follows imported files too.
The Commands section matters most. Claude references it every time it needs to run a linter or test suite. If your project has non-standard script names, see our guide to npm scripts — consistent naming makes Claude significantly more reliable without extra instruction.
CLAUDE.md instructions are advisory. In practice, Claude follows them roughly 80% of the time. For rules that must never be broken — blocking dangerous shell commands, enforcing formatting — use hooks instead. CLAUDE.md is for preferences and context; hooks are for enforcement.
Hooks: Rules Claude Cannot Ignore
Hooks are shell scripts that run at fixed points in Claude's execution lifecycle. Unlike CLAUDE.md instructions, hooks are deterministic. If a hook exits with a non-zero status code, Claude stops and surfaces the error. There is no workaround.
There are five hook types.
PreToolUse— runs before Claude executes a tool. Use it to block dangerous commands before they happen.PostToolUse— runs after Claude executes a tool. Use it to trigger formatters, linters, or logging.SessionStart— runs when a session opens. Use it to load environment variables or run setup checks.SessionEnd— runs when a session closes. Use it for cleanup or summaries.Notification— runs when Claude is waiting for user input. Use it to ping a Slack channel or send a desktop notification.
Hooks live in .claude/settings.json. Here is a PostToolUse hook that runs Prettier automatically every time Claude writes a file.
{
"hooks": {
"PostToolUse": [{
"matcher": "Write",
"hooks": [{
"type": "command",
"command": "npx prettier --write $CLAUDE_TOOL_INPUT_FILE_PATH"
}]
}]
}
}The matcher field targets a specific tool — Write in this case. $CLAUDE_TOOL_INPUT_FILE_PATH is an environment variable Claude populates with the file path being written.
For safety-critical enforcement, PreToolUse is the right hook. This example blocks any rm -rf command before it executes.
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "echo $CLAUDE_TOOL_INPUT_COMMAND | grep -q 'rm -rf' && exit 1 || exit 0"
}]
}]
}
}When the hook exits with code 1, Claude halts and reports the failure. The file system stays untouched.
PostToolUse hooks complement Git commit hooks rather than replacing them. If your project already uses Husky and lint-staged for pre-commit enforcement, Claude Code hooks catch format issues at the AI write layer before code ever reaches a commit.
Custom Slash Commands vs. Skills: Which to Use
Claude Code lets you extend the slash command system in two ways. Custom commands are simple Markdown prompt files. Skills are structured, multi-file workflows that Claude can invoke automatically when the situation calls for them.
| Custom Command | Skill | |
|---|---|---|
| File location | .claude/commands/name.md | .claude/skills/name/SKILL.md |
| Supporting files | No — single file only | Yes — can include templates, examples, sub-prompts |
| Auto-triggered by Claude | No — you invoke it manually | Yes — Claude recognizes when to apply it |
| Best for | Simple one-off prompts: /standup, /pr-summary | Complex reusable workflows with multiple steps or files |
A custom command for a standup summary is just a Markdown file with a prompt. It shows up as /standup in the command list and runs that prompt verbatim when invoked. Create it by adding .claude/commands/standup.md with your prompt text inside.
A skill makes sense when your workflow has branching logic, depends on supporting templates, or is something Claude should recognize and apply on its own without you typing a command. The complexity threshold is real: if you find yourself maintaining a multi-paragraph custom command with conditional instructions, it belongs in a skill.
Subagents: Why Parallel Workers Change Everything
Context windows fill up. In a long session, the earlier parts of your conversation fade from Claude's working memory, and output quality drops. Subagents solve this by offloading work to separate Claude instances with clean, empty context.
Your main session delegates a task to a subagent. The subagent works in isolation, completes the task, and returns a summary. The main session only sees the summary — not the thousands of lines of intermediate output the subagent generated.
The bigger win is parallelism. Claude can run three subagents simultaneously. You can review the auth module, run API integration tests, and check a DB migration at the same time rather than sequentially. On tasks that would take 20 minutes in a single session, parallel subagents regularly cut that to 7.
Custom subagents let you pre-configure a Claude instance for a specific role. Define one in .claude/agents/name.md with its own model choice, allowed tools, and standing instructions. A dedicated test-runner subagent configured to only use lightweight models and only run read commands is cheaper and safer than an unconfigured general-purpose instance.
---
model: claude-haiku-3-5
tools: Bash, Read
---
# Test Runner Agent
You run tests and report results. Do not modify any files.
Always run: npm test -- --reporter=verbose
Summarize failures concisely — file name, test name, error message only.PR Review Setup
Claude Code integrates with GitHub for pull request review. Set it up once and you can run reviews directly from the terminal with findings posted as inline GitHub comments.
- 1
Install the GitHub app
Run
/install-github-appinside a Claude session and follow the prompts. This authorizes Claude to read PR diffs and post review comments on your behalf. - 2
Open a session linked to a PR
bashclaude --from-pr 42Replace 42 with your PR number. Claude loads the diff and PR context into its working memory automatically.
- 3
Run the review and post findings
bash/code-review high --commentThe
--commentflag posts findings as inline GitHub comments on the PR. Without it, results print to your terminal only. The effort level controls depth:lowis fast and high-confidence-only,maxis the deepest analysis and best reserved for pre-release reviews.
For CI integration, /autofix-pr triggers an automated fix attempt when CI fails. Claude reads the failure logs, patches the issue, and pushes a follow-up commit to the same branch.
| Effort level | Speed | Best for |
|---|---|---|
| low | Fast (~30 sec) | Quick sanity check, low-stakes changes |
| medium | Moderate (~90 sec) | Regular feature PRs |
| high | Slower (~3 min) | Sensitive logic, auth, payments |
| max | Slowest (~5+ min) | Pre-release, security-critical changes |
Quick Reference Card
| Category | Do This | Notes |
|---|---|---|
| Start | claude | Opens a session in the current folder |
| First run | /init | Generates CLAUDE.md and .claude/ |
| Context full | /compact | Compresses history — use at >80% context |
| Undo | Esc Esc or /rewind | Rolls back Claude's last change |
| Parallel work | /agents | Delegate to parallel subagents |
| PR review | claude --from-pr 42, then /code-review high --comment | Posts inline GitHub comments |
| Custom command | .claude/commands/name.md | Simple reusable prompt — shows up as /name |
| Skill | .claude/skills/name/SKILL.md | Complex reusable workflow with supporting files |
| Hook | Add to .claude/settings.json hooks section | Deterministic enforcement — cannot be overridden |
| Shell output in context | ! npm test | Runs command and pulls output into Claude's context |
| Paste screenshot | Ctrl+V | Claude reads the image and responds |
Frequently Asked Questions
What is the difference between CLAUDE.md and hooks in Claude Code?
CLAUDE.md is advisory. Claude reads it at session start and follows its instructions roughly 80% of the time. It's the right place for style preferences, stack context, and reminders.
Hooks are deterministic shell scripts. When a PreToolUse hook exits with a non-zero code, Claude stops — no exceptions, no model judgment involved. Use CLAUDE.md for guidance; use hooks for anything that must be enforced.
How do I see all available slash commands in Claude Code?
Type / on an empty prompt and press Tab. Claude shows all built-in commands plus any custom commands you have defined in .claude/commands/. The autocomplete also shows a brief description of each command so you do not have to memorize the list.
When should I use a subagent instead of the main Claude session?
Two situations call for subagents. First, when a task generates a lot of output (test logs, large file trees, grep results) that would fill context and make Claude less useful for the rest of your session. The subagent absorbs that output and returns a summary.
Second, when you have independent tasks that can run simultaneously. Code review, test run, and DB migration check do not depend on each other — run them in parallel with subagents and finish in a third of the time.
What does /compact do and when should I use it?
/compact replaces your full conversation history with a short summary. Claude's context window has a fixed token limit. As history grows, earlier context fades and response quality drops.
Use /compact when context usage passes 80% (check with /context), or whenever you notice Claude starting to forget decisions made earlier in the session. Running it proactively is better than waiting for degraded responses.
Can Claude Code work offline?
No. Claude Code requires an internet connection to reach Anthropic's API. All AI inference runs on Anthropic's servers. Your code stays local — only the conversation text and any file contents you share in a session are sent to the API.
How is Claude Code different from GitHub Copilot?
GitHub Copilot is an IDE autocomplete that suggests code as you type. Claude Code is a terminal agent that reads your full project, executes commands, makes multi-file changes, runs tests, and reviews PRs autonomously. They solve different problems.
If you are evaluating both on cost, see our breakdown of how Claude Code compares to Copilot on cost — especially relevant now that Copilot has moved to per-token billing. Claude Code's flat-rate plan tends to favor heavy agentic users.
| GitHub Copilot | Claude Code | |
|---|---|---|
| Primary interface | IDE (VS Code, JetBrains, etc.) | Terminal |
| Core strength | Inline code completions as you type | Multi-file edits, shell commands, full project reasoning |
| Agentic capability | Agent mode (per-token billing) | Native — runs tools, subagents, hooks |
| Billing model | Per AI Credit (token-based, post June 2026) | Flat-rate subscription |
| Project context | Open files + configured workspace | Full project tree, git history, shell environment |
Related Articles
GitHub Copilot Token Billing: Real Cost by Workflow (2026)
GitHub Copilot switched to AI Credits billing June 1. Code completions stay free. Here's what chat, agent mode, and code review cost per workflow — and when to switch.
Husky + Prettier + lint-staged Setup for Next.js
Set up Husky v9, Prettier, and lint-staged in your Next.js project. Step-by-step guide covering pre-commit hooks with the correct 2026 config.
How to Audit Your VS Code Extensions for Security
The GitHub breach happened through a VS Code extension. Here's how to check what you have installed and reduce your exposure in 10 minutes.