Creating Claude Code Slash Commands
Overview
Slash commands are Markdown files with optional YAML frontmatter that define reusable prompts.
File Locations
| Type | Location | Scope |
|---|---|---|
| Project commands | .claude/commands/ | Shared with team via version control |
| Personal commands | ~/.claude/commands/ | Private, available in all projects |
Command name derives from filename: optimize.md → /optimize
Template
markdown
--- allowed-tools: Tool1, Tool2(pattern:*) argument-hint: [expected-input] description: Brief description shown in /help --- ## Context [Dynamic content providing current state] ## Constraints [Rules and limitations] ## Task [Clear, specific instructions]
Frontmatter Fields
| Field | Purpose | Default |
|---|---|---|
allowed-tools | Tools the command can use | Inherits from session |
argument-hint | Hint shown in autocomplete | None |
description | One-line description for /help | First line of content |
model | Model to use (e.g., claude-sonnet-4-20250514) | Inherits from session |
context | Set to fork for isolated sub-agent | Inline |
agent | Agent type when context: fork | general-purpose |
Arguments
$ARGUMENTS - All Input
Use for free-form or variable-length input:
markdown
Search the codebase for: $ARGUMENTS
Usage: /search auth flow → $ARGUMENTS = "auth flow"
$1, $2, $3 - Positional
Use for distinct, ordered parameters:
markdown
Create component `$1` with variant `$2`.
Usage: /component Button primary → $1 = "Button", $2 = "primary"
Dynamic Content
Bash Execution: !backticks
markdown
- **Branch**: !`git branch --show-current` - **Status**: !`git status --short`
File References: @path
markdown
@package.json @src/config.ts
Tool Permission Patterns
| Pattern | Meaning |
|---|---|
Read | Allow all file reads |
Edit | Allow all file edits |
Bash(git:*) | Allow git commands only |
Bash(npm test:*) | Allow npm test variations |
Read, Grep, Glob | Multiple read-only tools |
Best Practices
- •Always include
description- Makes commands discoverable - •Use
allowed-toolsfor safety - Restrict to necessary tools only - •Use
argument-hint- Helps users understand expected input - •Be specific with Bash -
Bash(git status:*)safer thanBash - •Handle errors in bash - Use
|| echo "fallback"
Workflow
- •Ask where to create the command (project or personal)
- •Ask for the command name and purpose
- •Determine required tools and arguments
- •Create the file with appropriate frontmatter and prompt content
- •Test with
chezmoi catif in chezmoi repo, or verify file exists