Claude Code Custom Slash Commands

How to create custom slash commands in Claude Code using CLAUDE.md and the .claude/commands/ directory. Covers command syntax, arguments, template variables, and real-world examples.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Custom slash commands let you create project-specific or personal shortcuts in Claude Code. Instead of typing long prompts repeatedly, you define them once as a Markdown file and invoke them with /command-name.

Creating your first command

# Project command (shared with team via git):
mkdir -p .claude/commands
cat > .claude/commands/review-pr.md << 'EOF'
Review the staged changes for this PR.

Check for:
- Logic errors and edge cases
- Missing error handling
- Security issues (injection, auth bypass, secrets in code)
- Test coverage for new code paths
- Naming consistency with the existing codebase

Output: bullet list of issues (if any), then a one-line verdict.
EOF

# Now use it in Claude Code:
# /review-pr

Command types

TypeLocationScopeVersion controlled
Project command.claude/commands/ (project root)This project onlyYes (commit to git)
User command~/.claude/commands/All your projectsNo (personal)

Using $ARGUMENTS

# .claude/commands/explain.md
Explain the following code in plain English, targeting a developer
who is new to this codebase:

$ARGUMENTS

Focus on: what it does, why it exists, and any non-obvious decisions.

# Usage: /explain path/to/file.py
# Or: /explain "the retry logic in src/api.ts around line 140"

Using shell output with ! prefix

# .claude/commands/commit-msg.md
Write a concise git commit message for the staged changes below.

Format: imperative mood, under 72 chars, no period at end.
Only output the message — no explanation, no quotes.

!git diff --staged

# This command runs git diff --staged and injects the output
# Usage: /commit-msg → Claude writes the commit message based on your actual staged diff

Real-world command examples

# .claude/commands/test-this.md
Write unit tests for the function or class described below.
Use the project's testing framework (check package.json / pytest.ini).
Follow the pattern in existing test files.

$ARGUMENTS

# .claude/commands/security-check.md
Review the following for OWASP Top 10 vulnerabilities.
Pay special attention to: SQL injection, XSS, auth bypass, secrets in code.

!git diff HEAD~1
Current branch: !git branch --show-current

# .claude/commands/changelog.md
Generate a changelog entry for the changes since the last tag.

!git log $(git describe --tags --abbrev=0)..HEAD --oneline

Format as Keep a Changelog (https://keepachangelog.com):
## [Unreleased]
### Added / Changed / Fixed / Deprecated / Removed / Security

Namespaced commands

# Use subdirectories to namespace commands:
.claude/commands/
  db/
    migrate.md      → /db:migrate
    seed.md         → /db:seed
    rollback.md     → /db:rollback
  review/
    security.md     → /review:security
    perf.md         → /review:perf
    api.md          → /review:api

Tips for effective commands

TipWhy
Use !git diff --staged in commit/review commandsGrounds the prompt in actual code, not description
Specify output format explicitlyPrevents verbose prose when you want a list or single line
Add project context via CLAUDE.md, not per-commandKeeps commands short; CLAUDE.md already loaded in session
Commit project commands to gitTeam shares the same shortcuts; onboarding gets /review-pr on day 1
Keep user commands for personal taste~/.claude/commands/ for formatting preferences, personal workflows

Combining commands with CLAUDE.md

# CLAUDE.md sets persistent context:
# - Language: TypeScript strict mode
# - Tests: Jest, always run npm test before declaring done
# - PR reviews: check for missing types, any escape hatches

# Commands use that context automatically:
# /review-pr → Claude already knows your conventions from CLAUDE.md
# /test-this → Claude picks Jest patterns from CLAUDE.md

# This means your commands stay short — no need to repeat
# "use TypeScript", "use Jest" in every command file.

For a full CLAUDE.md reference, see claude-code-memory-guide. For the complete list of built-in slash commands, see claude-code-slash-commands.

Frequently asked questions

How do I create a custom slash command in Claude Code?
Create a Markdown file in the `.claude/commands/` directory in your project root (or `~/.claude/commands/` for global commands). The filename becomes the command name — e.g., `.claude/commands/review-pr.md` creates `/review-pr`. The file content is the prompt that runs when you type the command.
What is the difference between project commands and user commands?
Project commands live in `.claude/commands/` relative to your project root and are shared with the team via git. User commands live in `~/.claude/commands/` and are personal — they're available in all your projects but not committed to any repo.
Can custom commands accept arguments?
Yes. Use `$ARGUMENTS` as a placeholder in your command file. When you run `/my-command some text here`, Claude Code substitutes `$ARGUMENTS` with `some text here`. You can use `$ARGUMENTS` multiple times in the same prompt.
Can I use shell commands inside a custom slash command?
Yes — wrap shell commands in `!` prefix lines. For example, a line starting with `!git diff --staged` runs the command and includes its output in the prompt. This lets commands capture dynamic context like current git status, test output, or file contents.
Where does CLAUDE.md fit relative to custom commands?
CLAUDE.md defines persistent project instructions that apply to every Claude Code session. Custom slash commands are one-shot prompts you trigger manually. They're complementary: CLAUDE.md ensures Claude always follows your conventions; commands give you reusable actions for specific workflows.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript