writing-hookify-rules
anthropics/claude-plugins-official
Define pattern-matching rules to guide Claude's tool use with warnings and blocks.
What is writing-hookify-rules?
Hookify rules are markdown files with YAML frontmatter that trigger messages when Claude's bash commands, file edits, or prompts match specified patterns. Use them to enforce best practices, prevent dangerous operations, and provide contextual guidance during agent execution.
- Match bash commands, file edits, stop events, and user prompts using regex patterns or multi-condition logic
- Trigger warnings (allow operation) or blocks (prevent operation) when patterns match
- Display custom markdown messages with explanations, alternatives, and best practices
- Support simple regex patterns or advanced multi-condition rules with field operators
- Store rules in `.claude/hookify.{name}.local.md` files that are read dynamically on each tool use
- Enable/disable rules without deletion by toggling the `enabled` field
How to install writing-hookify-rules
npx skills add https://github.com/anthropics/claude-plugins-official --skill writing-hookify-rulesHow to use writing-hookify-rules
- 1.Create a `.claude/` directory in your project root if it doesn't exist
- 2.Write a new file named `.claude/hookify.{descriptive-name}.local.md` with YAML frontmatter and a markdown message body
- 3.Set required frontmatter fields: `name` (kebab-case identifier), `enabled` (true/false), `event` (bash|file|stop|prompt|all), and `pattern` (regex) or `conditions` (multi-field rules)
- 4.Test the regex pattern using Python (`python3 -c "import re; print(re.search(r'pattern', 'text'))"`) or regex101.com
- 5.Save the file and trigger the corresponding event (bash command, file edit, etc.) to verify the rule fires
- 6.Adjust the pattern or message as needed; changes take effect immediately on next tool use
Use cases
- Prevent dangerous bash commands like `rm -rf` or `chmod 777` from executing
- Warn when console.log or debug code is added to production files
- Block edits to sensitive files like `.env` or `.pem` unless explicitly confirmed
- Enforce completion checklists before the agent stops (e.g., tests must pass)
- Require API keys and credentials to be in `.gitignore` before committing
- Development teams using Claude Code or Cursor with coding agents
- Projects needing guardrails on automated code changes
- Teams enforcing security practices (credential handling, permission management)
- Codebases requiring pre-commit validation or testing workflows
writing-hookify-rules FAQ
`pattern` is a simple regex match for single-condition rules (e.g., `rm\s+-rf`). `conditions` is an array of multi-field rules with operators like `regex_match`, `contains`, `equals`, etc. Use `pattern` for simple cases and `conditions` for complex logic requiring multiple fields to match.
Add `action: block` to the frontmatter. `warn` (default) shows the message but allows the operation. `block` prevents bash commands or file edits from executing, or stops the session for stop events.
All rules must be in the `.claude/` directory at your project root, named `.claude/hookify.{descriptive-name}.local.md`. Add `.claude/*.local.md` to `.gitignore` to keep local rules out of version control.
Yes. Use Python: `python3 -c "import re; print(re.search(r'your_pattern', 'test text'))"` or test online at regex101.com (select Python flavor). Common pitfalls: too broad patterns (e.g., `log` matches 'login'), escaping issues, and forgetting to escape special characters like `.` and `()`.
Yes. Rules are read dynamically on the next tool use (bash command, file edit, etc.). No restart or reload is needed. You can toggle `enabled: false` to temporarily disable a rule without deleting it.
Full instructions (SKILL.md)
Source of truth, from anthropics/claude-plugins-official.
name: writing-hookify-rules description: This skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns. version: 0.1.0
Writing Hookify Rules
Overview
Hookify rules are markdown files with YAML frontmatter that define patterns to watch for and messages to show when those patterns match. Rules are stored in .claude/hookify.{rule-name}.local.md files.
Rule File Format
Basic Structure
---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: regex-pattern-here
---
Message to show Claude when this rule triggers.
Can include markdown formatting, warnings, suggestions, etc.
Frontmatter Fields
name (required): Unique identifier for the rule
- Use kebab-case:
warn-dangerous-rm,block-console-log - Be descriptive and action-oriented
- Start with verb: warn, prevent, block, require, check
enabled (required): Boolean to activate/deactivate
true: Rule is activefalse: Rule is disabled (won't trigger)- Can toggle without deleting rule
event (required): Which hook event to trigger on
bash: Bash tool commandsfile: Edit, Write, MultiEdit toolsstop: When agent wants to stopprompt: When user submits a promptall: All events
action (optional): What to do when rule matches
warn: Show message but allow operation (default)block: Prevent operation (PreToolUse) or stop session (Stop events)- If omitted, defaults to
warn
pattern (simple format): Regex pattern to match
- Used for simple single-condition rules
- Matches against command (bash) or new_text (file)
- Python regex syntax
Example:
event: bash
pattern: rm\s+-rf
Advanced Format (Multiple Conditions)
For complex rules with multiple conditions:
---
name: warn-env-file-edits
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
You're adding an API key to a .env file. Ensure this file is in .gitignore!
Condition fields:
field: Which field to check- For bash:
command - For file:
file_path,new_text,old_text,content
- For bash:
operator: How to matchregex_match: Regex pattern matchingcontains: Substring checkequals: Exact matchnot_contains: Substring must NOT be presentstarts_with: Prefix checkends_with: Suffix check
pattern: Pattern or string to match
All conditions must match for rule to trigger.
Message Body
The markdown content after frontmatter is shown to Claude when the rule triggers.
Good messages:
- Explain what was detected
- Explain why it's problematic
- Suggest alternatives or best practices
- Use formatting for clarity (bold, lists, etc.)
Example:
⚠️ **Console.log detected!**
You're adding console.log to production code.
**Why this matters:**
- Debug logs shouldn't ship to production
- Console.log can expose sensitive data
- Impacts browser performance
**Alternatives:**
- Use a proper logging library
- Remove before committing
- Use conditional debug builds
Event Type Guide
bash Events
Match Bash command patterns:
---
event: bash
pattern: sudo\s+|rm\s+-rf|chmod\s+777
---
Dangerous command detected!
Common patterns:
- Dangerous commands:
rm\s+-rf,dd\s+if=,mkfs - Privilege escalation:
sudo\s+,su\s+ - Permission issues:
chmod\s+777,chown\s+root
file Events
Match Edit/Write/MultiEdit operations:
---
event: file
pattern: console\.log\(|eval\(|innerHTML\s*=
---
Potentially problematic code pattern detected!
Match on different fields:
---
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.tsx?$
- field: new_text
operator: regex_match
pattern: console\.log\(
---
Console.log in TypeScript file!
Common patterns:
- Debug code:
console\.log\(,debugger,print\( - Security risks:
eval\(,innerHTML\s*=,dangerouslySetInnerHTML - Sensitive files:
\.env$,credentials,\.pem$ - Generated files:
node_modules/,dist/,build/
stop Events
Match when agent wants to stop (completion checks):
---
event: stop
pattern: .*
---
Before stopping, verify:
- [ ] Tests were run
- [ ] Build succeeded
- [ ] Documentation updated
Use for:
- Reminders about required steps
- Completion checklists
- Process enforcement
prompt Events
Match user prompt content (advanced):
---
event: prompt
conditions:
- field: user_prompt
operator: contains
pattern: deploy to production
---
Production deployment checklist:
- [ ] Tests passing?
- [ ] Reviewed by team?
- [ ] Monitoring ready?
Pattern Writing Tips
Regex Basics
Literal characters: Most characters match themselves
rmmatches "rm"console.logmatches "console.log"
Special characters need escaping:
.(any char) →\.(literal dot)()→\(\)(literal parens)[]→\[\](literal brackets)
Common metacharacters:
\s- whitespace (space, tab, newline)\d- digit (0-9)\w- word character (a-z, A-Z, 0-9, _).- any character+- one or more*- zero or more?- zero or one|- OR
Examples:
rm\s+-rf Matches: rm -rf, rm -rf
console\.log\( Matches: console.log(
(eval|exec)\( Matches: eval( or exec(
chmod\s+777 Matches: chmod 777, chmod 777
API_KEY\s*= Matches: API_KEY=, API_KEY =
Testing Patterns
Test regex patterns before using:
python3 -c "import re; print(re.search(r'your_pattern', 'test text'))"
Or use online regex testers (regex101.com with Python flavor).
Common Pitfalls
Too broad:
pattern: log # Matches "log", "login", "dialog", "catalog"
Better: console\.log\(|logger\.
Too specific:
pattern: rm -rf /tmp # Only matches exact path
Better: rm\s+-rf
Escaping issues:
- YAML quoted strings:
"pattern"requires double backslashes\\s - YAML unquoted:
pattern: \sworks as-is - Recommendation: Use unquoted patterns in YAML
File Organization
Location: All rules in .claude/ directory
Naming: .claude/hookify.{descriptive-name}.local.md
Gitignore: Add .claude/*.local.md to .gitignore
Good names:
hookify.dangerous-rm.local.mdhookify.console-log.local.mdhookify.require-tests.local.mdhookify.sensitive-files.local.md
Bad names:
hookify.rule1.local.md(not descriptive)hookify.md(missing .local)danger.local.md(missing hookify prefix)
Workflow
Creating a Rule
- Identify unwanted behavior
- Determine which tool is involved (Bash, Edit, etc.)
- Choose event type (bash, file, stop, etc.)
- Write regex pattern
- Create
.claude/hookify.{name}.local.mdfile in project root - Test immediately - rules are read dynamically on next tool use
Refining a Rule
- Edit the
.local.mdfile - Adjust pattern or message
- Test immediately - changes take effect on next tool use
Disabling a Rule
Temporary: Set enabled: false in frontmatter
Permanent: Delete the .local.md file
Examples
See ${CLAUDE_PLUGIN_ROOT}/examples/ for complete examples:
dangerous-rm.local.md- Block dangerous rm commandsconsole-log-warning.local.md- Warn about console.logsensitive-files-warning.local.md- Warn about editing .env files
Quick Reference
Minimum viable rule:
---
name: my-rule
enabled: true
event: bash
pattern: dangerous_command
---
Warning message here
Rule with conditions:
---
name: my-rule
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.ts$
- field: new_text
operator: contains
pattern: any
---
Warning message
Event types:
bash- Bash commandsfile- File editsstop- Completion checksprompt- User inputall- All events
Field options:
- Bash:
command - File:
file_path,new_text,old_text,content - Prompt:
user_prompt
Operators:
regex_match,contains,equals,not_contains,starts_with,ends_with
Related skills
More from anthropics/claude-plugins-official and the wider catalog.

frontend-design
Distinctive visual design guidance for building UIs with intentional aesthetic choices, not templated defaults.

claude-md-improver
Audit and improve CLAUDE.md files to optimize Claude Code's project context.

claude-automation-recommender
Analyze a codebase and recommend Claude Code automations (hooks, subagents, skills, plugins, MCP servers). Use when user asks for automation recommendations, wants to optimize their Claude Code setup, mentions improving Claude Code workflows, asks how to first set up Claude Code for a project, or wants to know what Claude Code features they should use.

playground
Build self-contained interactive HTML playgrounds with live preview and copyable prompt output.

skill-creator
Create, iterate, and optimize AI agent skills with built-in testing and performance measurement.

agent-development
Create autonomous agents for Claude Code plugins with system prompts, triggering conditions, and configuration.