← All Cheatsheets

AI

Claude Code

CLI commands · Slash commands · Memory · Hooks · MCP · Best practices

Getting Started

# Install

npm install -g @anthropic-ai/claude-code

# Launch in project directory

claude

# One-shot command

claude -p "explain this codebase"

# Continue last session

claude --continue

Config file~/.claude/settings.json
Project memoryCLAUDE.md in repo root
AuthANTHROPIC_API_KEY env var
/
Slash Commands
/helpShow all available commands
/clearClear conversation context
/compactCompress context to save tokens
/memoryView and edit memory files
/initCreate CLAUDE.md for the project
/reviewReview recent changes
/costShow token usage and cost
/modelSwitch model mid-session
/doctorCheck environment health
/bugReport a bug to Anthropic
🧠
Memory System

CLAUDE.md (Project)

Place in repo root. Auto-loaded every session. Store architecture decisions, coding standards, common commands.

~/.claude/CLAUDE.md (User)

Global preferences across all projects. Preferred languages, style, tools.

# CLAUDE.md example

## Stack

- TypeScript, Next.js 14, Tailwind

## Commands

- Build: npm run build

- Test: npm test --run

## Standards

- No default exports except pages

🚀
CLI Flags
FlagShortDescription
--print-pNon-interactive: print response and exit
--continue-cResume the most recent conversation
--resume <id>Resume a specific conversation by ID
--model <name>Specify model (claude-opus-4, sonnet-4, etc.)
--output-formatjson | text | stream-json for scripting
--max-turns <n>Limit agentic loop iterations
--allowedToolsComma-separated list of permitted tools
--no-toolsDisable all tools (pure chat mode)
--dangerously-skip-permissionsSkip all permission prompts (CI use)
🔌
MCP — Model Context Protocol
MCP servers extend Claude Code with custom tools — databases, APIs, internal services.

# Add MCP server

claude mcp add my-server

# List MCP servers

claude mcp list

# Config location

.claude/settings.json

Scopelocal / user / project
Transportstdio or SSE
Auto-approveautoApprove: ['tool_name']
🪝
Hooks

Automate actions triggered by Claude Code events. Defined in .claude/hooks/

PreToolUseRun before any tool call — validate, log, block
PostToolUseRun after tool call — audit, notify
StopRun when agent finishes — run tests, format
NotificationTriggered on agent notifications

# Hook runs npm test after agent stops

{"hooks": {"Stop": [{"type": "command", "command": "npm test"}]}}

🔒
Permissions & Trust
Claude Code asks permission before writing files, running commands, or accessing the network. Configure trust levels carefully.
DefaultPrompt for each sensitive action
Trust projectAuto-approve tools in this repo
Trust allAuto-approve everything (use with care)
Review diffs before approveUse --allowedToolsAudit hooksPin model version
Best Practices
Write a good CLAUDE.md: Include stack, commands, conventions, and gotchas. Claude reads it every session.
Use /compact often: Compress context when working on long tasks to avoid hitting limits.
Be specific in tasks: Give Claude a clear goal, acceptance criteria, and constraints upfront.
Commit frequently: Commit before and after Claude makes changes so you can easily revert.
Use headless mode for CI: claude -p "run tests and fix failures" --dangerously-skip-permissions
Chain with pipes: cat error.log | claude -p "explain this error"
Use --output-format json: Parse Claude output programmatically in scripts.
Custom slash commands: Add .claude/commands/my-cmd.md for project-specific shortcuts.