Anatomy of Slash Commands
Anatomy of /commands and design patterns.

Design patterns for
/commandsin Claude Code β reusable, parameterized, auditable workflows triggered with one keystroke.
TL;DRβ
- A slash command is a Markdown file under
.claude/commands/that Claude treats as a reusable prompt. - Invoke as
/<filename>(e.g.,/review,/deploy). - Each command can declare arguments, permissions, aliases, examples, and a structured instructions body.
- They turn tribal knowledge into one-keystroke workflows.
- Best when each command is one job, well-documented, idempotent.
Why this mattersβ
Imagine typing make test instead of remembering the exact 6-step test pipeline. Slash commands are make for AI work β codify the playbook once, run it forever.
Bottom line: They're how you turn "ask Claude nicely" into "execute the company's playbook."
The Big Pictureβ
Bottom line: Type β load β render β execute β ship.
Command File Anatomyβ
A slash command file has three sections:
---
description: Run a full code review on the current branch
usage: /review [--base <branch>] [--scope <path>]
aliases: ["r", "code-review"]
arguments:
- name: base
description: Base branch to compare against
required: false
default: main
- name: scope
description: Optional path to limit review
required: false
default: .
example: /review --base develop --scope src/
permissions:
allow: ["Read", "Bash(git *)", "Bash(npm test)"]
deny: ["Write", "Bash(rm -rf *)"]
---
# Instructions
You are a senior code reviewer.
Follow the steps below carefully.
## Steps
1. Fetch {{base}} and compare with current branch.
2. Identify high risk changes (security, correctness, perf).
3. Check tests & coverage (run `npm test`).
4. Provide actionable suggestions with file:line refs.
## Output format
### Summary
### Risks & Issues
### Missing Tests
### Suggestions
## Checklist
- [ ] No secrets
- [ ] Tests updated
- [ ] Docs updated (if needed)
- [ ] All checks passed
## Notes
- Be concise and specific.
- Prefer small, safe changes.
| Section | Purpose |
|---|---|
| Frontmatter (YAML) | Metadata & configuration |
| Arguments | Parameters for reusability |
| Permissions | Sandbox rules scoped to this command |
| Body (Markdown) | The actual instructions Claude runs |
Arguments & Variablesβ
| Syntax | Meaning | Example |
|---|---|---|
{{var}} | Required variable | {{scope}} |
{{var?}} | Optional variable | {{scope?}} |
{{var=default}} | Default value | {{base=main}} |
{{var|a|b|c}} | Choose-from list | {{env|dev|stg|prod}} |
{{var: type}} | Typed variable | {{count: number}} |
$ENV_VAR | Environment variable | $GITHUB_TOKEN |
π‘ Tip: Always validate inputs in your instructions and handle missing values gracefully.
Built-in Variables (always available)β
| Variable | Description | Example |
|---|---|---|
{{workspace}} | Workspace root path | /Users/me/app |
{{cwd}} | Current working dir | /Users/me/app/src |
{{timestamp}} | ISO timestamp | 2026-05-12T10:30:00Z |
{{user}} | Current user name | alice |
{{branch}} | Current git branch | feature/login |
{{diff}} | Git diff vs base | (large diff text) |
{{os}} | Operating system | macos |
{{arch}} | CPU architecture | arm64 |
{{env:VAR}} | Env var (shortcut) | {{env:GITHUB_TOKEN}} |
Command Design Patternsβ
Pick the pattern that matches your intent:
| Pattern | Purpose | Examples |
|---|---|---|
| π― Action | Do something | /deploy, /lint, /test |
| π Information | Get information | /explain, /status, /logs |
| π οΈ Generation | Create new artifacts | /docs, /changelog, /pr |
| π Analysis | Inspect & evaluate | /review, /audit, /risk |
| βοΈ Refactor | Improve code | /refactor, /cleanup |
| π Workflow | Multi-step process | /release, /onboard |
Step-by-Step Setupβ
1. Create the commands directoryβ
mkdir -p .claude/commands
2. Create your first commandβ
cat > .claude/commands/review.md <<'EOF'
---
description: Review the current branch
usage: /review [--base <branch>]
arguments:
- name: base
default: main
permissions:
allow: ["Read", "Bash(git *)"]
---
# Instructions
Review changes vs `{{base}}`. Report risks, missing tests, and concrete fixes.
EOF
3. Verify it's discoverableβ
/help # lists available commands
/help review # shows details for one command
4. Run itβ
/review --base develop
5. Iterateβ
Refine the instructions after each real use; commit changes.
Variants for Different Team Settingsβ
π€ Solo developerβ
Keep commands minimal. One job each: /review, /test, /deploy.
π₯ Small team (2β10)β
- Commit
.claude/commands/so everyone shares the same shortcuts. - Add a
/helpindex command listing local conventions. - Standardize argument names (
--base,--scope,--env).
π’ Enterprise / regulatedβ
- Pin permissions tightly per command (no
allow: ["*"]). - Require code review for any command change.
- Add an audit hook logging every command invocation.
"hooks": {
"PostToolUse": [
{ "matcher": "manual:command",
"hooks": [{ "type": "command", "command": "./scripts/log-command.sh" }] }
]
}
- Sign commands and ship via a plugin (see Plugins anatomy).
π Open-source / communityβ
- Document every command in
CONTRIBUTING.md. - Include
/onboardas the first command new contributors run. - Avoid environment-specific commands; parameterize aggressively.
Bottom line: Same skeleton, escalating governance.
Anatomy of a Single Command β Zoom in on /reviewβ
---
description: Run a full code review on the current branch
usage: /review [--base <branch>] [--scope <path>]
aliases: ["r", "code-review"]
arguments:
- name: base
description: Base branch to compare against
required: false
default: main
- name: scope
description: Optional path to limit review
required: false
default: .
example: /review --base develop --scope src/
permissions:
allow: ["Read", "Bash(git *)", "Bash(npm test)"]
deny: ["Write", "Bash(rm -rf *)"]
---
# Instructions
You are a senior code reviewer.
Reference `CLAUDE.md` for project conventions.
## Steps
1. Run `git fetch origin {{base}}`.
2. Get diff: `git diff origin/{{base}}...HEAD -- {{scope}}`.
3. Categorize: π΄ Critical Β· π‘ Concern Β· π’ Suggestion.
4. Check tests: `npm test`.
5. Report findings with `file:line` references.
## Output format
### Summary
### Risks & Issues
### Missing Tests
### Suggestions
### Checklist
- [ ] No secrets
- [ ] Tests pass
- [ ] Docs updated
Common Patterns & Recipesβ
Pattern 1 β Pipeline commandβ
---
description: Pre-release checks
usage: /release [--env prod|staging]
---
Run in order, stop on first failure:
1. `npm run lint`
2. `npm test`
3. `npm run build`
4. `git diff main..HEAD`
5. Generate changelog summary.
Pattern 2 β Conditional logicβ
{{#if env == "prod"}}
β οΈ Require human approval before continuing.
{{/if}}
Pattern 3 β Delegate to a subagentβ
Use the `@code-reviewer` subagent on this diff.
After review, hand off to `@test-writer` for any missing tests.
Pattern 4 β Composable commandsβ
First run `/lint`, then `/test`, then `/changelog`.
Halt and report on any failure.
Pattern 5 β Interactive (ask first)β
Before running destructive steps, ask:
"Confirm deployment to {{env}}? (yes/no)"
Only continue on explicit "yes".
Pattern 6 β Self-documenting /helpβ
---
description: List project commands
usage: /help [command]
---
List every `.claude/commands/*.md` with its `description` field.
If `{{command?}}` given, print its full content.
Built-in Slash Commands (reference)β
| Command | Purpose |
|---|---|
/help | List all commands or get help on one |
/init | Bootstrap CLAUDE.md and .claude/ |
/clear | Reset session context |
/exit | Quit Claude Code |
/model | Switch model mid-session |
/mcp | List/inspect MCP servers |
/permissions | Inspect current permissions |
/config | View merged effective settings |
π‘ Tip: Don't reuse built-in names.
/init.mdwill be overshadowed.
Discovery & Usageβ
List available commandsβ
/help
Available commands:
/review Run a full code review
/test Write & run tests
/explain Explain code or concept
/refactor Refactor a module
/deploy Deploy to environment
/docs Generate documentation
Run with argumentsβ
/review --base develop --scope src/services
Command loaded: review
β Base: develop
β Scope: src/services
β Executingβ¦
β Done. See results below.
Permissions Quick Referenceβ
| β ALLOW (examples) | β DENY (examples) |
|---|---|
Read | Write |
Edit | WriteOutsideWorkspace |
Bash(git *) | Bash(rm -rf *) |
Bash(npm test) | Bash(sudo *) |
WebSearch | WebFetch |
MCP:github | MCP:* |
MCP:postgres | MCP:filesystem(write) |
π‘ Deny by default. Allow only what's needed.
Hook Integration (recommended)β
Log and enforce command usage via a PostToolUse hook:
"hooks": {
"PostToolUse": [
{
"matcher": "manual:command",
"hooks": [
{ "type": "command", "command": "./scripts/log-command.sh" }
]
}
]
}
log-command.sh:
#!/usr/bin/env bash
echo "$(date -Iseconds) | $USER | $CLAUDE_COMMAND_NAME | $CLAUDE_COMMAND_ARGS" \
>> .claude/logs/commands.log
Best Practices β β
- Verb-first names:
/review,/deploy,/explain. - Short, clear description (shown in
/help). - Accept arguments for flexibility, with sensible defaults.
- Provide examples that actually work.
- Define permissions explicitly; deny by default.
- Return structured output (sections, tables, checklists).
- Idempotent where possible β safe to re-run.
- Log important actions via hooks.
Anti-Patterns ββ
- Too many commands β hard to discover.
- Vague names like
/do-stuffor/run. - Long, unstructured instructions β Claude drifts.
- No examples or argument docs.
- Overly broad permissions (
allow: ["*"]). - Commands that do destructive actions by default (no
--confirm). - Duplicated logic across commands β extract a shared skill.
Troubleshootingβ
| Problem | Likely cause | Fix |
|---|---|---|
| Command not showing | File not in .claude/commands/ | Check filename & location |
| Arguments not working | Wrong template syntax | Use {{var}} or {{var=def}} |
| Permission denied | Missing allow rule | Add to permissions.allow |
| Hook not firing | Wrong matcher | Use exact tool/matcher |
| Output too long | No structure | Use sections & bullets |
| Conflicts with another cmd | Alias clash | Pick unique aliases |
| Variables not interpolated | Typo in {{name}} | Match arguments exactly |
Minimal Starter Commandβ
---
description: Describe what this does
usage: /mycmd <arg>
arguments:
- name: arg
description: The argument
required: true
example: /mycmd value
---
# Instructions
Do the thing. Be awesome.
## Output format
- Result
- Notes
π‘ Tip: Use
/helpto list,/help <command>for details, and create project-specific shortcuts.
Glossaryβ
- Slash command β A reusable prompt defined as a Markdown file under
.claude/commands/. - Frontmatter β YAML block at the top of the file describing metadata.
- Argument β A named input the command accepts.
- Built-in variable β A variable populated automatically (workspace, branch, etc.).
- Alias β A short alternate name for a command.
- Idempotent β Safe to run repeatedly with the same effect.
Further Readingβ
- Search: "Claude Code slash commands documentation"
- Search: "prompt templates with variables YAML frontmatter"
- Search: "awesome-claude-commands GitHub"
- Search: "reusable prompt design patterns"
πͺ Golden Rulesβ
- π― One job per command.
- π Name for humans (verb-first).
- π Document arguments clearly.
- π‘ Show examples that work.
- π‘οΈ Be safe by default (deny-first permissions).
- π Iterate based on feedback.
Remember: Commands are code. Design, test, document, and improve them continuously.