Could we help you? Please click the banners. We are young and desperately need the money
If you find yourself typing the same prompts over and over in Claude Code—"Review this code for security issues," "Generate tests for this function," "Optimize this query"—there's a better way.
Custom commands let you turn repetitive prompts into reusable slash commands. Instead of typing out a detailed security checklist every time, you just type /security. Instead of explaining your testing requirements repeatedly, you type /test.
In this guide, I'll show you how custom commands work, how to create them, and how they can streamline your Claude Code workflow.
Custom commands are user-defined slash commands that execute pre-written prompts. They're simple Markdown files that Claude Code automatically loads and makes available as shortcuts.
Think of them as prompt templates you can invoke with a single command.
The mechanics are simple:
That's it. No complex configuration needed.
You have two options:
Location: ~/.claude/commands/
Use for commands you want available in all projects—personal coding standards, general workflows, language-specific patterns.
Location: .claude/commands/ (in your project root)
Use for commands specific to one project—team standards, project workflows, domain-specific prompts. These can be committed to version control so your whole team uses them.
# For global commands
mkdir -p ~/.claude/commands
# For project-specific commands
mkdir -p .claude/commands
Let's create a simple code review command:
nano ~/.claude/commands/review.md
Perform a code review focusing on:
- Security vulnerabilities
- Performance issues
- Code quality and maintainability
- Best practices violations
- Potential bugs
For each issue found, explain:
1. What the problem is
2. Why it matters
3. How to fix it with a code example
claude
# In your conversation
/review src/UserController.php
Claude will execute your full code review prompt on the specified file.
File: ~/.claude/commands/security.md
Review this code for security vulnerabilities:
- SQL injection risks
- XSS (Cross-Site Scripting)
- CSRF protection
- Authentication issues
- Input validation
- Sensitive data exposure
For each vulnerability:
1. Severity level (Critical/High/Medium/Low)
2. Specific code location
3. Explanation of the risk
4. Fix with code example
Usage: /security src/auth/login.php
File: ~/.claude/commands/test.md
Generate comprehensive tests for this code:
**Coverage:**
- Happy path scenarios
- Edge cases
- Error conditions
- Boundary values
**Structure:**
- Descriptive test names
- Arrange-Act-Assert pattern
- Mock external dependencies
- Clear assertions
Use PHPUnit for PHP, Jest for JavaScript.
Aim for 80%+ coverage.
Usage: /test src/services/PaymentService.php
File: ~/.claude/commands/optimize.md
Analyze and optimize this code for performance:
**Check for:**
- Inefficient algorithms
- N+1 query problems
- Missing database indexes
- Unnecessary computations
- Memory leaks
**Provide:**
1. Performance bottlenecks identified
2. Optimized version of the code
3. Expected performance improvement
4. Before/after complexity analysis
Show specific improvements with code examples.
Usage: /optimize src/queries/UserRepository.php
File: .claude/commands/wp-security.md
Review this WordPress code for security issues:
**WordPress-Specific Checks:**
- Nonce verification (wp_verify_nonce)
- Capability checks (current_user_can)
- Data escaping (esc_html, esc_attr, esc_url)
- SQL injection prevention (wpdb->prepare)
- Direct file access protection
Provide fixes using WordPress best practices and proper functions.
Usage: /wp-security wp-content/plugins/my-plugin/class-admin.php
/help
This shows all built-in commands, your global commands, project-specific commands, and any MCP commands.
# First, review the code
/review src/UserController.php
# Then optimize it
/optimize src/UserController.php
# Finally, generate tests
/test src/UserController.php
# Review all PHP files in auth directory
/security src/auth/*.php
❌ Vague:
Review this code.
✅ Specific:
Review this code for:
- SQL injection vulnerabilities
- Missing error handling
- Performance bottlenecks
Provide specific fixes with code examples.
Tell Claude how you want the response structured:
Provide your analysis in this format:
## Issues Found
1. [Issue name]
- Location: [file:line]
- Problem: [description]
- Fix: [code example]
## Summary
- Total issues: X
- Critical: X, High: X, Medium: X
Help Claude understand your environment:
You are reviewing a Laravel application that:
- Uses PHP 8.2
- Follows PSR-12 standards
- Uses service/repository pattern
Review with these requirements in mind.
For project-specific commands, commit them to git so your team uses the same standards:
# .gitignore
# Include project commands
!.claude/commands/
# But ignore personal settings
.claude/settings.local.json
For a large collection of commands, organize by category:
~/.claude/commands/
├── security.md
├── test.md
├── optimize.md
├── review.md
└── refactor.md
Or by technology:
~/.claude/commands/
├── wordpress/
│ ├── security.md
│ └── performance.md
├── laravel/
│ ├── controller.md
│ └── migration.md
└── javascript/
├── react.md
└── optimize.md
# Review code quality
/review src/NewFeature.php
# Check security
/security src/NewFeature.php
# Generate tests
/test src/NewFeature.php
# Identify bottlenecks
/optimize src/services/DataProcessor.php
# Review database queries
/optimize database/queries.sql
Create a /debug command:
Help me debug this issue:
1. Analyze the error or unexpected behavior
2. Identify likely root causes
3. Suggest debugging steps
4. Provide potential fixes
Be systematic and thorough.
Project-specific commands are perfect for ensuring team consistency:
# Create project commands
mkdir -p .claude/commands
# Add team standards
echo "Review following our team standards..." > .claude/commands/team-review.md
# Commit to repository
git add .claude/commands/
git commit -m "Add team Claude Code commands"
git push
Now everyone on your team uses the same review criteria, testing standards, and code patterns.
Custom commands transform Claude Code from a general AI assistant into a tool tailored to your specific workflow. Instead of repeating yourself, you build a library of commands that capture your standards and preferences.
Key takeaways:
~/.claude/commands/ or .claude/commands/Start by creating 3-4 commands for your most common tasks. As you use Claude Code more, you'll naturally discover which prompts you repeat and can turn them into commands.
Your productivity with Claude Code isn't just about the AI—it's about how well you've customized it to your needs.