writing-rules
Original:🇺🇸 English
Translated
Create markdown-based behavioral rules preventing unwanted actions. create hookify rule, behavioral rule, prevent behavior, block command Use when: preventing dangerous commands, blocking debug commits, enforcing conventions DO NOT use when: hook scope (abstract:hook-scope-guide), SDK hooks (abstract:hook-authoring), evaluating hooks (abstract:hooks-eval).
3installs
Added on
NPX Install
npx skill4agent add athola/claude-night-market writing-rulesTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Table of Contents
- Overview
- Quick Start
- Rule File Format
- Frontmatter Fields
- Event Types
- Advanced Conditions
- Operators
- Field Reference
- Pattern Writing
- Regex Basics
- Examples
- Test Patterns
- Example Rules
- Block Destructive Commands
- Warn About Debug Code
- Require Tests
- Protect Production Files
- Management
- Related Skills
- Best Practices
Hookify Rule Writing Guide
When To Use
- Creating behavioral rules to prevent unwanted actions
- Defining persistent guardrails for Claude Code sessions
When NOT To Use
- Complex multi-step workflows - use agents instead
- One-time operations that do not need persistent behavioral 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 files.
.claude/hookify.{rule-name}.local.mdQuick Start
Create :
.claude/hookify.dangerous-rm.local.mdyaml
---
name: dangerous-rm
enabled: true
event: bash
pattern: rm\s+-rf
action: block
---
🛑 **Dangerous rm command detected!**
This command could delete important files.Verification: Run the command with flag to verify availability.
--helpThe rule activates immediately - no restart needed!
Rule File Format
Frontmatter Fields
name (required): Unique identifier (kebab-case)
enabled (required): or
event (required): , , , , or
action (optional): (default) or
pattern (simple): Regex pattern to match
truefalsebashfilestoppromptallwarnblockEvent Types
- bash: Bash tool commands
- file: Edit, Write, MultiEdit tools
- stop: When agent wants to stop
- prompt: User prompt submission
- all: All events
Advanced Conditions
For multiple field checks:
yaml
---
name: warn-env-edits
enabled: true
event: file
action: warn
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
🔐 **API key in .env file!**
Ensure file is in .gitignore.Operators
- : Pattern matching
regex_match - : Substring check
contains - : Exact match
equals - : Must NOT contain
not_contains - : Prefix check
starts_with - : Suffix check
ends_with
Field Reference
bash events:
file events: , , ,
prompt events:
stop events:
commandfile_pathnew_textold_textcontentuser_prompttranscriptPattern Writing
Regex Basics
- - whitespace
\s - - digit
\d - - word character
\w - - any character (use
.for literal dot)\. - - one or more
+ - - zero or more
* - - OR
|
Examples
rm\s+-rf → rm -rf
console\.log\( → console.log(
chmod\s+777 → chmod 777Test Patterns
bash
python3 -c "import re; print(re.search(r'pattern', 'text'))"Example Rules
Block Destructive Commands
yaml
---
name: block-destructive
enabled: true
event: bash
pattern: rm\s+-rf|dd\s+if=|mkfs
action: block
---
🛑 **Destructive operation blocked!**
Can cause data loss.Warn About Debug Code
yaml
---
name: warn-debug
enabled: true
event: file
pattern: console\.log\(|debugger;
action: warn
---
🐛 **Debug code detected!**
Remove before committing.Require Tests
yaml
---
name: require-tests
enabled: true
event: stop
action: warn
conditions:
- field: transcript
operator: not_contains
pattern: pytest|npm test
---
⚠️ **Tests not run!**
Please verify changes.Protect Production Files
yaml
---
name: protect-prod
enabled: true
event: file
action: block
conditions:
- field: file_path
operator: regex_match
pattern: /production/|\.prod\.
---
🚨 **Production file!**
Requires review.Management
Enable/Disable:
Edit file:
.local.mdenabled: falseDelete:
bash
rm .claude/hookify.my-rule.local.mdList:
bash
/hookify:listRelated Skills
- abstract:hook-scope-guide - Hook placement decisions
- abstract:hook-authoring - SDK hook development
- abstract:hooks-eval - Hook evaluation
Best Practices
- Start with simple patterns
- Test regex thoroughly
- Use clear, helpful messages
- Prefer warnings over blocks initially
- Name rules descriptively
- Document intent in messages
Troubleshooting
Common Issues
If a rule doesn't trigger, verify that the type matches the tool being used (e.g., use for command line tools). Check that the regex is valid and matches the target text by testing it with a short Python script. If you encounter permission errors when creating rule files in , ensure that the directory is writable by your user.
eventbashpattern.claude/