Part 5: Custom Commands and Sub-Agents

Slash commands are reusable prompts that automate workflows.

Creating Commands

Commands live in .claude/commands/ as markdown files:

.claude/commands/fix-types.md:

---
description: Fix TypeScript errors
---

Run `npx tsc --noEmit` and fix any type errors.
For each error:
1. Identify the root cause
2. Fix with minimal changes
3. Verify the fix compiles

After fixing all errors, run the check again to confirm.

Use it:

/fix-types

Benefits of Commands

Benefit Description
Workflow efficiency One word instead of paragraph prompts
Team sharing Check into git, everyone gets them
Parameterization Use $ARGUMENTS for dynamic input
Orchestration Commands can spawn sub-agents

Sub-Agents

Sub-agents run in isolated context windows — they don't pollute your main conversation.

"Each sub-agent operates in its own isolated context window. This means it can focus on a specific task without getting 'polluted' by the main conversation."

Global Commands Library

Add frequently-used commands to your global config:

## Global Commands

Store these in ~/.claude/commands/ for use in ALL projects:

### /new-project
Creates new project with all scaffolding rules applied.

### /security-check
Scans for secrets, validates .gitignore, checks .env handling.

### /pre-commit
Runs all quality gates before committing.

### /docs-lookup
Spawns sub-agent with Context7 to research documentation.

Example Command: Security Check

Create ~/.claude/commands/security-check.md:

---
description: Scan for security issues before commit
---

Perform a security audit:

1. Check .gitignore exists and contains:
   - .env
   - .env.*
   - node_modules/
   - Any secret files

2. Scan staged files for:
   - Hardcoded API keys
   - Passwords in strings
   - Private keys
   - AWS credentials

3. Verify .env.example exists (if .env exists)

4. Report findings with severity levels.

Example Command: Documentation Lookup

Create ~/.claude/commands/docs.md:

---
description: Look up documentation for a library
---

Using Context7, research documentation for: $ARGUMENTS

Return a concise summary of:
1. Basic usage
2. Common patterns
3. Any gotchas or breaking changes

Usage:

/docs React useOptimistic hook