Skip to content

Menu

Claude Code Cheat Sheet: CLI Flags, Slash Commands and Config

Checked against Claude Code 2.1.332 on 28 November 2026. Each section links to the official docs it was checked against.

A quick reference for developers who use Claude Code every day and keep forgetting which flag or file does what. It was checked against Claude Code 2.1.332, the latest npm release on 28 November 2026 (published 24 November). Claude Code ships several releases a week, so check claude --version and the version notes below before you rely on a newer flag.

When to use what

Run plain claude for interactive work and claude -p for anything a script or CI job calls. Print mode skips the trust dialog and the UI, so pair it with --output-format json when another program reads the result, and with --max-turns or --max-budget-usd so a confused run cannot loop forever.

For instructions, use CLAUDE.md when you want Claude to follow a rule and a hook when the rule must always hold. Claude reads CLAUDE.md as guidance and can still drift from it; a PreToolUse hook that exits 2 actually blocks the tool call. Keep each CLAUDE.md under about 200 lines as the docs suggest, and move rules that apply to one folder into .claude/rules/ with a paths list so they load only when needed.

For permissions, put team rules in .claude/settings.json, your own exceptions in .claude/settings.local.json, and global taste in ~/.claude/settings.json. Lists such as permissions.allow merge across those files, while single values follow the precedence order: managed, then --settings, then local, project and user. Reach for --dangerously-skip-permissions only inside a throwaway container; acceptEdits or auto mode cover most of the same ground with far less risk. For MCP servers, claude mcp add is quicker than hand-editing JSON, and the MCP config cheat sheet covers the file formats if you need to share them with other clients.

We ran npm view @anthropic-ai/claude-code version to confirm 2.1.332; the commands themselves are from the CLI reference and were not run for this sheet.

claude "explain this repo"

Open an interactive session that starts with your prompt

From the docs, not run by us
claude -p "query"

Print mode: answer once and exit, for scripts and CI

From the docs, not run by us
cat logs.txt | claude -p "explain"

Pipe a file in as context for a one-shot answer

From the docs, not run by us
claude -c

Continue the most recent conversation in this directory

From the docs, not run by us
claude -r "auth-refactor" "finish this PR"

Resume a session by ID or name and send it a new prompt

From the docs, not run by us
claude -n "my-feature"

Name the session so --resume my-feature can find it later

From the docs, not run by us
claude -w feature-auth

Start in an isolated git worktree under .claude/worktrees/feature-auth

From the docs, not run by us
claude update / claude install stable

Update to the latest release, or reinstall the native binary on the stable channel

From the docs, not run by us

--max-turns, --max-budget-usd and --json-schema work in print mode only. --json-schema takes a JSON Schema; build one from a sample object with our JSON Schema generator.

--model opus

Pick the model by alias (sonnet, opus, haiku, fable) or full name for this session

From the docs, not run by us
--permission-mode plan

Start in a mode: default, acceptEdits, plan, auto, dontAsk or bypassPermissions

From the docs, not run by us
--allowedTools "Bash(npm run test *)" "Read"

Let matching tool calls run without a prompt

From the docs, not run by us
--disallowedTools "mcp__*"

Remove every MCP tool from Claude's context; a scoped rule like Bash(rm *) denies only matching calls

From the docs, not run by us
--add-dir ../shared-lib

Give Claude file access to another directory (most of its .claude/ config is not picked up)

From the docs, not run by us
-p --output-format json

Return the result as JSON; stream-json emits events as they happen

From the docs, not run by us
-p --max-turns 3 --max-budget-usd 2

Cap agentic turns and estimated API spend for an unattended run

From the docs, not run by us
--append-system-prompt "Always use TypeScript"

Add to the default system prompt; --system-prompt replaces all of it

From the docs, not run by us
--mcp-config ./ci-mcp.json --strict-mcp-config

Load only the MCP servers in that file, ignoring .mcp.json and ~/.claude.json

From the docs, not run by us
--effort high

Set effort for the session: low, medium, high, xhigh, max or ultracode

From the docs, not run by us
-p --json-schema '<schema>'

Get validated JSON output that matches your schema when the run finishes

Try it: JSON Schema ValidatorFrom the docs, not run by us
/init

Write a starter CLAUDE.md for the project from what Claude finds in the repo

From the docs, not run by us
/memory

Edit CLAUDE.md files and turn auto memory on or off

From the docs, not run by us
/compact focus on the API changes

Summarise the conversation to free context, keeping what your instruction names

From the docs, not run by us
/clear

Start a fresh conversation with empty context; the old one stays in /resume

From the docs, not run by us
/context

Show context usage as a grid, with hints about what is eating it

From the docs, not run by us
/rewind

Roll the conversation and/or file edits back to an earlier checkpoint (aliases /undo, /checkpoint)

From the docs, not run by us
/model /effort high

Switch model (saved as your default) or change effort mid-session

From the docs, not run by us
/permissions

View and edit allow, ask and deny rules by scope

From the docs, not run by us
/plan fix the auth bug

Enter plan mode and start planning that task right away

From the docs, not run by us
/usage

Show session cost and plan limits (/cost is an alias)

From the docs, not run by us
/btw what does this regex do?

Ask a side question without adding it to the main conversation

From the docs, not run by us
/status

Show version, model, account and which settings files loaded

From the docs, not run by us

Settings files are strict JSON: a trailing comma or // comment turns into a Settings Error at the next start. Run the file through the JSON formatter if you are unsure. CLAUDE.md details come from the memory docs.

./CLAUDE.md or ./.claude/CLAUDE.md

Project instructions every teammate gets; files above the working directory load at launch, subfolder files load on demand

From the docs, not run by us
~/.claude/CLAUDE.md ./CLAUDE.local.md

Your instructions for all projects, and your private notes for this one (gitignore the local file)

From the docs, not run by us
@docs/architecture.md

Import another file from CLAUDE.md; relative to the importing file, up to four hops deep

From the docs, not run by us
.claude/rules/api.md with `paths:` frontmatter

Rules that load only when Claude touches matching files

From the docs, not run by us
~/.claude/settings.json

User settings for every project: default model, theme, personal permission rules

From the docs, not run by us
.claude/settings.json / .claude/settings.local.json

Shared team settings to commit, and your personal overrides that stay out of git

From the docs, not run by us
"permissions": {"allow": ["Bash(npm run lint)"], "deny": ["Read(./.env)"]}

Allow one command without prompts and block reading the env file

Try it: JSON FormatterFrom the docs, not run by us
.claude/agents/ ~/.claude/agents/

Where project and personal subagent definitions live

From the docs, not run by us
~/.claude.json

State Claude Code writes itself: account, local and user MCP servers, trust decisions; do not hand-edit

From the docs, not run by us

Exit code 1 is not a block. Only exit 2 stops a blockable action (with stderr as the reason); any other non-zero code is reported and the action goes ahead.

"hooks": {"PreToolUse": [{"matcher": "Bash", "hooks": [{"type": "command", "command": "..."}]}]}

Run a script before every Bash call; event, then matcher group, then handlers

From the docs, not run by us
exit 2

Block the tool call from a PreToolUse hook and feed stderr back to Claude as the reason

From the docs, not run by us
PostToolUse with matcher "Edit|Write"

Run a formatter or linter after Claude edits a file

From the docs, not run by us
SessionStart / Stop / Notification

Load context at start, check work when Claude finishes, or alert you when it needs input

From the docs, not run by us
jq -r '.tool_input.command'

Read the Bash command a hook received as JSON on stdin

From the docs, not run by us
/hooks

Browse configured hooks read-only, with the file each one came from

From the docs, not run by us
claude mcp add --transport http notion https://mcp.notion.com/mcp

Add a remote Streamable HTTP server in local scope

From the docs, not run by us
claude mcp add -s project fs -- npx -y @modelcontextprotocol/server-filesystem .

Add a stdio server to the committed .mcp.json for the whole team

From the docs, not run by us
claude mcp list

Show every configured MCP server and whether it connects

From the docs, not run by us
claude mcp login <name>

Run a server's OAuth sign-in from the shell; add --no-browser over SSH

From the docs, not run by us
/mcp

Check status, sign in, and enable or disable servers from inside a session

From the docs, not run by us

Worked example: Block edits to .env files with a hook, then prove it works

A CLAUDE.md line saying "never touch .env" is a request. This hook makes it a rule. Save the script as .claude/hooks/protect-env.sh and make it executable:

#!/usr/bin/env bash
file=$(jq -r '.tool_input.file_path // empty')
case "$file" in
  *.env|*.env.*) echo "Blocked: $file holds secrets" >&2; exit 2 ;;
esac
exit 0

Register it in .claude/settings.json so the whole team gets it:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{ "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/protect-env.sh" }]
      }
    ]
  }
}

Test the script without Claude by piping in the JSON a hook receives:

$ echo '{"tool_input":{"file_path":"/app/.env"}}' | .claude/hooks/protect-env.sh; echo "exit=$?"
Blocked: /app/.env holds secrets
exit=2
$ echo '{"tool_input":{"file_path":"/app/src/x.ts"}}' | .claude/hooks/protect-env.sh; echo "exit=$?"
exit=0

We checked the jq filter locally (it prints /app/.env for the first input); the exit codes shown follow from the script and the hooks docs, since our sandbox would not run shell scripts. Exit 2 is what matters: Claude Code stops the Edit or Write call and shows Claude the stderr line, so it explains the refusal and moves on. If you had used exit 1, the docs say the edit would go ahead with only a hook error notice. Settings files reload on save, so /hooks should list the new entry right away. The jq filter is a plain field lookup; our JSONPath evaluator is handy for working out paths in bigger hook payloads.

Common mistakes

  • Using exit 1 in a hook meant to enforce a policy.

    Exit with 2. Claude Code treats 1 like any other non-blocking error, logs it and lets the tool call run.

  • Setting "defaultMode": "bypassPermissions" in the committed .claude/settings.json and wondering why it does nothing.

    Since 2.1.257 that value only applies from user or managed settings. Pass --permission-mode for one session, or set it in ~/.claude/settings.json if you really mean it.

  • Growing one CLAUDE.md past several hundred lines.

    Long files use more context and get followed less. Keep the root file short and move folder-specific rules into .claude/rules/ with a paths list.

  • Running claude -p in CI with no limits.

    Add --max-turns and --max-budget-usd, and use --output-format json so the pipeline can read the result and fail cleanly.

  • Adding a personal MCP server with -s project and committing a token in .mcp.json.

    Use the default local scope or -s user for personal servers. In the project file, reference secrets as ${VAR} and keep the value in your shell.

  • Adding a trailing comma to settings.json.

    Settings files are strict JSON, so the whole file fails with a Settings Error. Run /status after editing, and claude doctor to see which entries were rejected.