Creating Custom Slash Commands
Slash commands are user-invoked shortcuts that expand into full prompts. They're perfect for repetitive tasks.
Command Locations
# Global commands (available everywhere)
~/.claude/commands/
# Project-specific commands
.claude/commands/Creating Your First Command
1. Create the commands directory
mkdir -p .claude/commands2. Create a command file
# .claude/commands/test.md
touch .claude/commands/test.md3. Add command content
Run the test suite for the current project.
Steps:
1. Identify the test framework being used
2. Run all tests
3. If any fail, analyze the failures and suggest fixes
4. Summarize the results4. Use the command
/test
Command Examples
Code Review Command
<!-- .claude/commands/review.md -->
Review the staged changes for:
- Code quality issues
- Potential bugs
- Security vulnerabilities
- Performance concerns
Format as a structured review with severity levels.Documentation Command
<!-- .claude/commands/document.md -->
Generate documentation for $ARGUMENTS.
Include:
- Function/component purpose
- Parameters with types
- Return values
- Usage examplesCommit Command
<!-- .claude/commands/commit.md -->
Create a conventional commit for the staged changes.
1. Analyze what changed
2. Determine the commit type (feat/fix/docs/refactor/test)
3. Write a clear, concise message
4. Include scope if applicableUsing Arguments
Commands can accept arguments via $ARGUMENTS:
<!-- .claude/commands/explain.md -->
Explain $ARGUMENTS in simple terms.
Provide:
- What it does
- Why it's useful
- Example usageUsage: /explain the useCallback hook
Referencing Files
Include specific files with curly braces:
<!-- .claude/commands/optimize.md -->
Analyze {src/utils/helpers.ts} for optimization opportunities.Organization Tips
.claude/commands/
├── commit.md # Git operations
├── review.md # Code review
├── test.md # Testing
├── document.md # Documentation
└── refactor.md # Code improvements
Best Practices
- Single responsibility: Each command should do one thing well
- Clear instructions: Be specific about expected output format
- Use variables:
$ARGUMENTSfor flexibility - Include context: Reference relevant files when needed
- Test thoroughly: Run commands in different scenarios