Lesson completed!
-

Custom Slash Commands: /commit, /review, /test + Templates

Custom Slash Commands

Claude Code comes with built-in commands like /help and /clear. But the real power is in creating your own commands.

Built-in vs Custom

Built-in Commands

Those that come with Claude Code:

/help       # View help
/clear      # Clean context
/init       # Create CLAUDE.md
/context    # View token usage
/cost       # View costs
/compact    # Summarize history

Custom Commands

Those you create for your workflow:

/commit     # Automatic semantic commit
/review     # Code review of changes
/test       # Run tests and fix failures
/deploy     # Deploy to production
/docs       # Update documentation

Create Your First Custom Command

Custom commands live in .claude/commands/ in your project.

Step 1: Create the Directory

mkdir -p .claude/commands

Step 2: Create the Command File

Each command is a Markdown file:

touch .claude/commands/commit.md

Step 3: Write the Command

# /commit
 
Analyze git changes and create a semantic commit message.
 
## Instructions
 
1. Run `git status` to see changes
2. Run `git diff --staged` if there are staged changes
3. Analyze the type of change (feat, fix, docs, refactor, etc.)
4. Generate a commit message following conventional commits
5. Execute the commit
 
## Message Format
 
```
type(scope): description
 
[optional body]
```
 
Valid types: feat, fix, docs, style, refactor, test, chore
 
## Example
 
If I added a login function:
```
feat(auth): add login functionality
 
- Add login form component
- Implement authentication logic
- Add session management
```

Step 4: Use the Command

/commit

Claude reads the file, follows the instructions, and executes everything automatically.

The Power of $ARGUMENTS

You can pass arguments to your commands using $ARGUMENTS:

# /review
 
Review the code in $ARGUMENTS looking for:
 
1. Potential bugs
2. Performance improvements
3. Code clarity
4. Missing tests
 
Generate a report with findings and suggestions.

Usage:

/review src/components/LoginForm.tsx

Claude receives src/components/LoginForm.tsx as $ARGUMENTS.

My Most Used Commands

/commit - Semantic Commits

# /commit
 
Create a semantic commit of current changes.
 
1. Run `git status` and `git diff`
2. Determine type: feat|fix|docs|refactor|test|chore
3. Write clear, concise message
4. Run `git add .` and `git commit`
 
If there are many changes, suggest splitting into smaller commits.

/review - Quick Code Review

# /review
 
Review $ARGUMENTS like a senior developer.
 
Look for:
- Bugs and unhandled edge cases
- Performance issues
- Repeated code
- Missing error handling
- Refactoring opportunities
 
Format: List of issues ordered by severity (HIGH/MEDIUM/LOW).

/test - Automatic Testing

# /test
 
Run tests and automatically fix failures.
 
1. Run `npm run test`
2. If there are failures, analyze the error
3. Determine if the bug is in code or test
4. Fix the problem
5. Run tests again
6. Repeat until all pass

/pr - Create Pull Request

# /pr
 
Create a pull request for the current branch.
 
1. Make sure you're on a feature branch (not main)
2. Run `git log main..HEAD` to see commits
3. Generate descriptive title
4. Generate description with:
   - Summary of changes
   - Type of change (feature/bugfix/refactor)
   - Testing done
5. Run `gh pr create`

/fix - Fix Error

# /fix
 
Analyze the error in $ARGUMENTS and fix it.
 
1. Read the complete error message
2. Find the file and line where it occurs
3. Analyze root cause
4. Implement the fix
5. Verify the error doesn't return
 
If error is unclear, add temporary logs for debugging.

Command Organization

Per Project

Store project-specific commands in:

my-project/
└── .claude/
    └── commands/
        ├── commit.md
        ├── review.md
        ├── test.md
        └── deploy.md

Global (All Projects)

Store commands you use everywhere in:

~/.claude/
└── commands/
    ├── commit.md      # Same command for all projects
    ├── explain.md
    └── refactor.md

Tips for Effective Commands

1. Be Specific

Bad:

# /review
Review the code.

Good:

# /review
Review the code looking for:
1. Null pointer exceptions
2. Memory leaks
3. Race conditions
4. Missing input validation

2. Define the Output

# /analyze
 
Analyze $ARGUMENTS and generate a report with:
 
## Complexity
- Cyclomatic complexity of each function
 
## Dependencies
- External imports
- Internal imports
 
## Risks
- Parts of code that can break
 
Format: Markdown with clear sections.

3. Include Examples

# /rename
 
Rename $ARGUMENTS following project conventions.
 
## Conventions
- Components: PascalCase (LoginForm.tsx)
- Hooks: camelCase with use prefix (useAuth.ts)
- Utils: camelCase (formatDate.ts)
 
## Example
`/rename user-profile` → UserProfile.tsx

Mistakes That Cost Me Hours

Mistake 1: Commands Too Generic

The error: I created a /do-everything command that did too many things. Claude got confused about what to do first.

The cost: 30 minutes of back and forth clarifying what I wanted.

The lesson: One command = one specific task. Better to have 10 simple commands than 1 complex one.

Mistake 2: Not Defining Output

The error: My /analyze command didn't specify output format. Every time I used it, Claude generated a different format.

The cost: Time wasted reformatting outputs.

The solution: Always specify the exact format you want.

Challenge: Try This

Before moving to the next lesson:

  • Create the .claude/commands/ directory in your project
  • Create a basic /commit command
  • Run /commit and verify it works
  • Create a /review command with $ARGUMENTS

Estimated time: 10 minutes

Next Step

Commands are invoked manually. Skills activate automatically based on context.