custom-slash-commands
Comprehensive guide to creating and using custom slash commands in Claude Code.
Overview
Custom slash commands allow you to create reusable prompts that can be invoked quickly with a simple syntax. Commands are markdown files that get expanded into full prompts when executed.
Invocation Pattern:
/<command-name> [arguments]File Structure & Locations
Project-Level Commands
.claude/commands/command-name.md.claude/commands/category/command-name.md # Organized in subdirectories- Shared with team members via version control
- Project-specific workflows
Personal Commands
~/.claude/commands/command-name.md~/.claude/commands/category/command-name.md- Available across all projects
- Personal preferences and workflows
Key Points:
- Filename (without
.md) becomes the command name - Subdirectories organize commands without affecting invocation
optimize.md→/optimizecommand
Basic Command Creation
Simple Command:
Review the current changes and provide feedback on:1. Code quality and best practices2. Potential bugs or edge cases3. Performance considerations4. Test coverageUsage: /review
Arguments
Capture All Arguments with $ARGUMENTS
Use $ARGUMENTS as a placeholder for all arguments passed to the command:
Find and fix issue #$ARGUMENTS. Follow these steps:1. Understand the issue described in the ticket2. Locate the relevant code3. Implement a solution4. Add appropriate tests5. Prepare a PR descriptionUsage: /fix-issue 123 → replaces $ARGUMENTS with “123”
Positional Arguments
Access individual arguments using $1, $2, $3, etc.:
Review PR #$1 with priority $2 and assign to $3
Focus areas:- Code quality- Test coverage- Documentation updatesUsage: /review-pr 456 high @engineer
Benefits:
- Use arguments in different parts of the command
- Provide default values
- More granular control
Advanced Features
Frontmatter Configuration
Commands support optional YAML frontmatter for metadata and configuration:
---description: Bootstrap project architecture documentationmodel: claude-sonnet-4-5-20250929allowed-tools: Bash(git status:*), Bash(git log:*)argument-hint: [project-type]disable-model-invocation: false---
Initialize complete architecture documentation for this $1 project:
1. Analyze entire codebase for technology stack2. Generate C4 architecture documentation3. Create ADRs for discovered technology decisions4. Validate that architectural entities are documentedFrontmatter Fields
| Field | Purpose | Example |
|---|---|---|
description | Brief command summary shown in autocomplete | Bootstrap project documentation |
argument-hint | Display expected parameters | [project-type] or <issue-number> |
model | Override default model selection | claude-sonnet-4-5-20250929 |
allowed-tools | Specify permitted tools with restrictions | Bash(git status:*) |
disable-model-invocation | Prevent programmatic execution | true or false |
Bash Integration
Execute shell commands within slash commands using the ! prefix:
---description: Create feature branch and initial commitallowed-tools: Bash(git checkout:*), Bash(git commit:*)argument-hint: <feature-name>---
Create a new feature branch for: $ARGUMENTS
!git checkout -b feature/$ARGUMENTS!git commit --allow-empty -m "feat: Initialize $ARGUMENTS feature"
Branch created! Ready to start development.Important: Must declare allowed Bash commands in frontmatter for security.
File References
Include file contents using the @ prefix:
---description: Analyze component for optimization opportunities---
Analyze this component for performance improvements:
@src/components/$ARGUMENTS.tsx
Focus on:- Unnecessary re-renders- Memoization opportunities- Bundle size impactUsage: /analyze-component Header includes contents of src/components/Header.tsx
Command Organization Patterns
By Workflow Type
.claude/commands/├── dev/│ ├── fix-issue.md│ ├── review-pr.md│ └── refactor.md├── docs/│ ├── bootstrap.md│ ├── update-readme.md│ └── generate-api-docs.md└── testing/ ├── generate-tests.md └── run-coverage.mdBy Technology
.claude/commands/├── react/│ ├── create-component.md│ └── optimize-component.md├── node/│ ├── add-endpoint.md│ └── update-deps.md└── python/ ├── add-test.md └── profile-function.mdPlugin & MCP Commands
Plugin Commands
Format: /plugin-name:command-name
- Automatically discovered when plugins installed
- Provided by plugin authors
MCP Commands
Format: /mcp__<server-name>__<prompt-name>
- Dynamically exposed by MCP servers
- Follow MCP protocol conventions
Example: /mcp__temporal-bridge__search_personal
SlashCommand Tool
Claude can programmatically invoke slash commands during conversations using the SlashCommand tool:
// Claude's internal tool usageSlashCommand("/review-pr 123")Permission Rules:
- Exact match:
SlashCommand:/review-pr - Prefix-based:
SlashCommand:/review-pr:*
Built-in Commands (Reference)
Claude Code provides ~25 built-in commands:
Conversation Control
/clear- Clear conversation history/compact- Compact conversation to save tokens/rewind- Rewind to previous state
Configuration
/config- View/edit configuration/status- Show current status/model- Change model/permissions- Manage permissions
Development Tools
/review- Code review mode/sandbox- Sandbox environment/terminal-setup- Terminal configuration
Account Management
/login- Authenticate/logout- Sign out
Utilities
/help- Show help/cost- Show API costs/usage- Show usage stats/doctor- Diagnose issues
Best Practices
Command Design
- Single Responsibility: Each command should do one thing well
- Clear Naming: Use descriptive, action-oriented names
- Good Defaults: Minimize required arguments when possible
- Documentation: Include clear descriptions in frontmatter
Argument Usage
- Use
$ARGUMENTSfor single, flexible inputs - Use positional
$1,$2, etc. for structured multi-part inputs - Always provide
argument-hintin frontmatter for discoverability
Organization
- Group related commands in subdirectories
- Use consistent naming conventions
- Keep project commands focused on team workflows
- Use personal commands for individual preferences
Security
- Always specify
allowed-toolswhen using Bash - Be specific about allowed commands (use patterns like
git status:*) - Avoid exposing sensitive information in version-controlled commands
Example Commands
Bootstrap Architecture Documentation
---description: Bootstrap project architecture documentationmodel: claude-sonnet-4-5-20250929argument-hint: [project-type]---
Initialize complete architecture documentation for this project:
1. **Analyze entire codebase** for technology stack, patterns, and components2. **Generate C4 documentation** (Context, Container, Component, Code levels)3. **Create ADRs** for discovered technology and architectural decisions4. **Validate** that architectural entities are searchable
Project type: $1
Use proper YAML frontmatter with component_type, c4_layer, and status values.Fix GitHub Issue
---description: Find and fix a GitHub issue end-to-endargument-hint: <issue-number>allowed-tools: Bash(git:*)---
Find and fix issue #$ARGUMENTS:
1. **Understand** the issue from the GitHub ticket2. **Locate** the relevant code files3. **Implement** a solution following our coding standards4. **Add tests** to prevent regression5. **Commit** with conventional commit message6. **Prepare** PR description with testing steps
!git branch feature/issue-$ARGUMENTS!git checkout feature/issue-$ARGUMENTSGenerate Component Tests
---description: Generate comprehensive tests for a componentargument-hint: <component-path>---
Generate tests for the component at: $ARGUMENTS
Include:- **Unit tests** for all exported functions- **Integration tests** for component behavior- **Edge cases** and error scenarios- **Snapshot tests** for UI components- **Accessibility tests** where applicable
Use existing test patterns from the codebase.@$ARGUMENTSCode Translation
---description: Translate code between languagesargument-hint: <from-language> <to-language>---
Translate the following code from $1 to $2:
$3
Ensure:- Idiomatic patterns for target language- Appropriate type safety- Performance considerations- Comments explaining non-obvious conversionsComparison: Slash Commands vs Agents
| Feature | Slash Commands | Custom Agents |
|---|---|---|
| Invocation | /command arg | @agent-name prompt |
| Discovery | Built-in autocomplete | Manual naming |
| Consistency | Always same behavior | Varies by natural language |
| Arguments | Structured ($1, $2) | Natural language parsing |
| Help | Description in autocomplete | Read agent file |
| Best For | Repeatable workflows | Complex, nuanced tasks |
Recommendation: Use slash commands for standard, repeatable workflows. Use agents for complex scenarios requiring natural language flexibility.
Resources
Official Documentation:
Getting Started:
- Create
.claude/commands/directory in your project - Add a simple
.mdfile (e.g.,hello.mdwith content “Say hello!”) - Type
/in Claude Code to see your command - Invoke with
/hello
Last Updated: 2025-11-05 Claude Code Version: Latest (Sonnet 4.5 Compatible)