Hooks & Commands
Automate your workflow with hooks that run on events, and speed up common tasks with slash commands.
Event Hooks
Hooks are shell scripts or prompts that run automatically in response to tool usage. They enable automated workflows like formatting, linting, and testing.
PreToolUse
Runs before your coding assistant uses a tool (e.g., before writing a file)
Use: Validation, blocking dangerous operations
PostToolUse
Runs after your coding assistant uses a tool (e.g., after editing a file)
Use: Formatting, linting, running tests
SessionStart
Runs when a new session begins
Use: Loading context, showing status
Stop
Runs before the session ends
Use: Verification, cleanup tasks
Available Hooks
Automatically format files with Prettier after edits
Run ESLint after file changes
Run TypeScript compiler check after changes
Scan for security issues in code
Validate bash commands before execution
Automatically run related tests after code changes
Show git status when starting a session
Verify all tasks are complete before stopping
Run ESLint fix and Prettier format after every file change
Invoke test-coverage-agent before stopping to ensure all changes have tests
Check for security vulnerabilities when package.json changes
Validate commit message format follows conventional commits
Hook Configuration Example
Hooks are configured in settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npx prettier --write \"$file_path\""
},
{
"type": "command",
"command": "npx eslint \"$file_path\" --fix"
}
]
}
],
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/validate-bash.sh",
"timeout": 30
}
]
}
]
}
}Slash Commands
Slash commands are shortcuts for common tasks. Type them in your chat to quickly perform actions like code review, test generation, or creating commits.
/review[file-path]Review code for quality, security, and best practices
/test[file-path]Generate tests for the specified code
/docs[file-path]Generate documentation for the specified code
/optimize[file-path]Analyze and optimize code for performance
/security[file-path]Audit code for security vulnerabilities
/refactor[file-path]Refactor code for better structure and readability
/explain[file-path]Explain how the specified code works
/fix[issue-description]Fix a bug or issue in the code
/commitCreate a well-formatted git commit
/pr[base-branch]Create a pull request with proper description
Command Configuration Example
Commands are defined as Markdown files in .claude/commands/:
---
description: Review code for quality, security, and best practices
argument-hint: "[file-path]"
allowed-tools: Read, Grep, Glob, Bash
model: claude-sonnet-4-20250514
---
# Code Review
Review the specified code for:
- Code quality and readability
- Security vulnerabilities
- Performance issues
- Best practices adherence
Focus on: $ARGUMENTS
Provide feedback in this format:
- **Critical**: Issues that must be fixed
- **Warnings**: Issues that should be fixed
- **Suggestions**: Nice-to-have improvementsBest Practices
Hooks
- • Keep hooks fast (under 5 seconds)
- • Use non-blocking operations where possible
- • Handle errors gracefully
- • Don't block critical workflows
Commands
- • Use descriptive names
- • Provide clear argument hints
- • Limit tools to what's needed
- • Include example output format