Claude Code
Claude Code cheat sheet covering CLI commands, configuration options, slash commands, and practical usage examples for AI-assisted coding.
Other AI Sheets
Sign in to mark items as known and track your progress.
Sign inInstallation & Setup
Getting started with Claude Code
Installation Methods
Multiple ways to install Claude Code
# NPM Installation (recommended)
npm install -g @anthropic-ai/claude-code
# Native Installer (macOS/Linux/WSL)
curl -fsSL claude.ai/install.sh | bash
# Windows PowerShell
irm https://claude.ai/install.ps1 | iex
# Verify installation
claude --version
# Requirements: Node.js 18+Basic Usage
Starting and using Claude Code
# Start interactive session
claude
# Run one-time task
claude "refactor this function to use async/await"
# Continue most recent conversation
claude -c
claude --continue
# Resume and pick previous conversation
claude -r
claude --resume
# Non-interactive mode
claude --print "explain this codebase"
# Start in Plan Mode
claude --permission-mode plan
# Create a Git commit (ask in a session, or use the /commit slash command)
claude "commit my staged changes with a conventional message"// EXAMPLE: Refactoring a callback to async/await
$ claude "refactor this callback to use async/await"
// EXAMPLE: Continuing previous work
$ claude -c
> "Let's add error handling to that function"
// EXAMPLE: Creating a commit
$ claude commit
> Claude analyzes changes and creates commit messageEssential Commands
Core slash commands in Claude Code
Session Management
Control your Claude Code session
# Authentication
/login # Log in to your account
/logout # Sign out completely
# Session control
/exit # Exit Claude Code (also: exit, Ctrl+C)
/clear # Clear conversation history
/help # Show available commands
# Context management
/compact # Reduce context size when near limit// EXAMPLE: Managing a long session
> "Help me build a REST API"
[... extensive work ...]
> /compact // Reduce context when getting full
> "Now add authentication"
// EXAMPLE: Starting fresh
> /clear
> "Let's work on a different feature"Configuration Commands
Customize Claude Code behavior
# Configuration access
/config # Open configuration menu
/permissions # Allow tools to run without approval
/hooks # Configure hooks
/output-style # Access output styles menu
# Output styles
/output-style:new # Create new output style
# Advanced
/agents # List available subagents
/doctor # Check installation health
/bug # Report issues to AnthropicCustom Commands
Create your own slash commands
# Custom command locations:
~/.claude/commands/ # User-level commands
.claude/commands/ # Project-level commands
# Example: optimize.md
---
name: optimize
description: Optimize performance
---
Find and optimize performance bottlenecks
# Example with arguments: fix-issue.md
---
name: fix-issue
description: Fix GitHub issue
---
Fix issue #$ARGUMENTS from our GitHub repo
# Use custom commands:
/optimize
/fix-issue 123// EXAMPLE: Creating a test runner command
// File: ~/.claude/commands/test.md
---
name: test
description: Run tests for a specific file
---
Run the test suite for $ARGUMENTS and fix any failures
// Usage:
> /test auth.service.tsConfiguration Files
Understanding Claude Code configuration
Settings Files
Configuration file hierarchy
# User-level settings
~/.claude/settings.json # Global configuration
~/.claude.json # User state (projects, MCP, etc.)
~/.claude/.credentials.json # Auth token (macOS uses Keychain)
# Project-level settings
.claude/settings.json # Project configuration
.claude/settings.local.json # Local overrides (gitignored)
CLAUDE.md # Project instructions
# Directory structure
.claude/
├── settings.json # Project config
├── settings.local.json # Local overrides
├── commands/ # Custom commands
├── agents/ # Custom subagents
├── output-styles/ # Custom styles
└── hooks/ # Hook scriptsPermissions Configuration
Control tool approval requirements
# .claude/settings.json
{
"permissions": {
"defaultMode": "acceptEdits" // default | acceptEdits | plan | bypassPermissions
}
}
# Permission modes:
# default - Standard prompting; asks before sensitive actions
# acceptEdits - Auto-accept file edits, still prompt for other tools
# plan - Read-only planning mode, no writes/execution
# bypassPermissions - Skip all permission prompts (use with caution)
# Per-tool permissions via /permissions command:
/permissions
# Then select tools to auto-approveCLAUDE.md File
Project-specific instructions
# Create CLAUDE.md in project root
# Example content:
# Project Overview
This is a Next.js e-commerce application...
## Code Style
- Use TypeScript strict mode
- Prefer async/await over callbacks
- Follow ESLint configuration
## Important Notes
- Never modify the payments module
- Always run tests before committing
- Use pnpm instead of npm
## Architecture
/src/api - API routes
/src/components - React components
/src/lib - Utility functionsOutput Styles
Customize Claude Code responses
Built-in Styles
Available output styles
# Access output styles menu
/output-style
# Built-in styles:
- Default # Standard Claude Code behavior
- Explanatory # More detailed explanations
- Learning # Educational, step-by-step
# Switch styles during session:
/output-style
# Select from menu
# Create new style:
/output-style:newCustom Output Styles
Create your own response styles
# Location: ~/.claude/output-styles/concise.md
---
name: Concise
description: Brief, to-the-point responses
---
You are a concise coding assistant.
- Give brief explanations
- Minimize commentary
- Focus on code solutions
- Use bullet points for clarity
# Location: .claude/output-styles/verbose.md
---
name: Verbose
description: Detailed explanations
---
Provide comprehensive explanations:
- Explain reasoning behind decisions
- Include alternative approaches
- Add detailed comments in code
- Discuss trade-offs// EXAMPLE: Creating a "Security Focus" style
// File: ~/.claude/output-styles/security.md
---
name: Security Focus
description: Emphasize security considerations
---
Always consider security implications:
- Check for SQL injection vulnerabilities
- Validate all user inputs
- Never log sensitive data
- Use environment variables for secretsHooks System
Automate and customize behavior
Hook Events
Available hook trigger points
# Hook events:
PreToolUse # Before tool execution
PostToolUse # After tool execution
UserPromptSubmit # When user sends message
Notification # System notifications
Stop # When stopping execution
SubagentStop # When subagent stops
PreCompact # Before context compaction
SessionStart # Session begins
SessionEnd # Session ends
# Configure via:
/hooks # Interactive configuration
# Or edit ~/.claude/settings.json directlyHook Configuration
Setting up hooks in settings.json
# ~/.claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "jq -r '\"Running: \" + .tool_input.command' >> ~/.claude/log.txt"
}
]
}
],
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": "python3 ~/.claude/hooks/validate_prompt.py"
}
]
}
]
}
}// EXAMPLE: Log all bash commands
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "date >> ~/.claude/bash.log && echo '{{tool_input.command}}' >> ~/.claude/bash.log"
}]
}]
}
}Hook Scripts
Creating custom hook scripts
#!/usr/bin/env python3
# ~/.claude/hooks/log_commands.py
import sys
import json
from datetime import datetime
# Read input from stdin
data = json.loads(sys.stdin.read())
# Log command details
with open('command_log.txt', 'a') as f:
f.write(f"{datetime.now()}: {data['tool_input']['command']}\n")
# Return 0 to allow, non-zero to block
sys.exit(0)
# Make executable:
chmod +x ~/.claude/hooks/log_commands.pyAdvanced Features
Power user capabilities
Subagents
Specialized agents for complex tasks
# List available subagents
/agents
# Built-in subagents:
- general-purpose # General tasks
- statusline-setup # Configure status line
- output-style-setup # Create output styles
# Custom subagent: .claude/agents/tester.md
---
name: tester
description: Run comprehensive tests
tools: Bash, Read, Write
---
Run all test suites and fix any failures
# Subagents enable:
- Parallel processing
- Specialized workflows
- Complex multi-step operations// EXAMPLE: Complex refactoring with subagents
> "Refactor the entire authentication system to use OAuth"
Claude: I'll use multiple subagents for this:
- Agent 1: Update database schema
- Agent 2: Refactor backend API
- Agent 3: Update frontend components
- Agent 4: Update tests
[Agents work in parallel]MCP Integration
Model Context Protocol for external data
# MCP enables access to:
- Databases
- APIs
- Documentation
- External tools
# Configuration in .mcp.json at the project root:
{
"mcpServers": {
"database": {
"type": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://..."]
}
}
}
# Or add one from the CLI: claude mcp add
# Then in Claude:
"Query the users table for active accounts"
"Check the API documentation for endpoints"CI/CD Integration
Use Claude Code in automation
# GitHub Actions example
name: Claude Review
on: [pull_request]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install -g @anthropic-ai/claude-code
- run: |
claude --print "Review this PR and suggest improvements" > review.md
# GitLab CI example
claude-review:
script:
- npm install -g @anthropic-ai/claude-code
- claude --print "Check for security issues"
# Pre-commit hook
#!/bin/bash
claude --print "Review staged changes for issues"Troubleshooting
Common issues and solutions
Diagnostic Commands
Debug and fix issues
# Check installation health
/doctor
# Report bugs to Anthropic
/bug
# Environment variables for debugging
USE_BUILTIN_RIPGREP=0 # Disable built-in ripgrep
# Common fixes:
# Permission denied
sudo npm install -g @anthropic-ai/claude-code
# Command not found
export PATH="$PATH:~/.local/bin"
# WSL issues
# Check .wslconfig for proper settingsContext Management
Handle context size limits
# When hitting context limits:
# 1. Use /compact command
/compact
# Reduces context while preserving important info
# 2. Clear and start fresh
/clear
# Completely resets conversation
# 3. Use focused requests
# Instead of: "Fix all the issues"
# Use: "Fix the TypeScript error in auth.ts"
# 4. Break large tasks
# Split into smaller, focused sessions