Menü schliessen
Created: December 5th 2025
Categories: Artificial intelligence (AI),  Claude code,  IT Development
Author: Milos Jevtic

Automate Repetitive Prompts with Claude Code Custom Commands

Introduction: Automate Your Repetitive Prompts

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.

What Are Custom Commands?

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 Benefits

  • Save time - No more retyping the same detailed prompts
  • Consistency - Same standards applied every time
  • Team alignment - Share commands across your team
  • Customization - Tailor prompts to your specific needs

How Custom Commands Work

The mechanics are simple:

  1. Create a Markdown file with your prompt
  2. Place it in the commands directory
  3. Use it by typing /filename (without .md extension)

That's it. No complex configuration needed.

Where to Create Commands

You have two options:

1. Global Commands

Location: ~/.claude/commands/

Use for commands you want available in all projects—personal coding standards, general workflows, language-specific patterns.

2. Project-Specific Commands

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.

Creating Your First Command

Step 1: Create the Directory

# For global commands
mkdir -p ~/.claude/commands

# For project-specific commands  
mkdir -p .claude/commands

Step 2: Create a Command File

Let's create a simple code review command:

nano ~/.claude/commands/review.md

Step 3: Write Your Prompt

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

Step 4: Use It

claude

# In your conversation
/review src/UserController.php

Claude will execute your full code review prompt on the specified file.

Command Examples

Example 1: Security Review

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

Example 2: Test Generation

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

Example 3: Performance Optimization

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

Example 4: WordPress-Specific Command

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

Using Commands Effectively

List All Available Commands

/help

This shows all built-in commands, your global commands, project-specific commands, and any MCP commands.

Chain Multiple Commands

# First, review the code
/review src/UserController.php

# Then optimize it
/optimize src/UserController.php

# Finally, generate tests
/test src/UserController.php

Use with Multiple Files

# Review all PHP files in auth directory
/security src/auth/*.php

Best Practices

1. Be Specific

❌ Vague:

Review this code.

✅ Specific:

Review this code for:
- SQL injection vulnerabilities
- Missing error handling
- Performance bottlenecks

Provide specific fixes with code examples.

2. Define Output Format

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

3. Include Context

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.

4. Version Control Project Commands

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

Organizing Your Commands

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

Common Use Cases

Code Review Workflow

# Review code quality
/review src/NewFeature.php

# Check security
/security src/NewFeature.php

# Generate tests
/test src/NewFeature.php

Performance Optimization

# Identify bottlenecks
/optimize src/services/DataProcessor.php

# Review database queries
/optimize database/queries.sql

Bug Fixing

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.

Sharing Commands with Your Team

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.

Conclusion

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:

  • Commands are simple Markdown files in ~/.claude/commands/ or .claude/commands/
  • Use global commands for personal standards, project commands for team standards
  • Be specific in your prompts for better results
  • Chain commands together for complex workflows
  • Share project commands via version control

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.