• Projects
  • Service
  • About
  • branding.bz
  • Podcast
  • Tips
  • FAQ
  • Recruit
  • Download
  • Contact
  • branding.bz(ブランド構築SaaS)
  • DESIGN NOW(デザインメディア)
  • X
  • LinkedIn
  • Spotify
  • Facebook

213-0011 神奈川県川崎市高津区久本3-6-7-303

© 2026 ID INC. All rights reserved

claude-skills/スキル
SKILLClaude Codeproductivity

writing-hookify-rules

プラグイン
hookify
ソース
GitHub で見る ↗
説明

次のような場合に使用: ユーザーが「hookify ルール(自動実行ルール)を作成する」「フック ルールを書く」「hookify を設定する」「hookify ルールを追加する」といった依頼をしたり、hookify ルールの文法やパターンについてのガイダンスが必要な場合。

原文を表示

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.

ユースケース
  • hookify ルールを作成するとき
  • フック ルールを書くとき
  • hookify を設定するとき
  • hookify ルールを追加するとき
  • hookify ルールの文法やパターンについてのガイダンスが必要な場面
本文(日本語訳)

Hookifyルール(ガイドラインの作成方法)

概要

Hookifyルールはマークダウン形式のファイルで、YAMLフロントマター(冒頭のメタデータ)を含みます。特定のパターンを監視し、それが見つかったときにメッセージを表示する仕組みです。ルールは .claude/hookify.{ルール名}.local.md というファイルに保存されます。

ルールファイルの形式

基本構造

---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: 正規表現パターン
---

このルールが発動したときにClaudeに表示するメッセージです。
マークダウン形式、警告、提案など含められます。

フロントマター(ファイル冒頭のメタデータ)の項目

name(必須): ルールの一意な識別子

  • ケバブケース(単語をハイフンで繋ぐ形式)で書く: warn-dangerous-rm、block-console-log
  • わかりやすく、動作中心の名前を付ける
  • 動詞で始める: warn(警告)、prevent(防止)、block(遮断)、require(要求)、check(確認)

enabled(必須): ルールを有効/無効にする

  • true: ルールは有効です
  • false: ルール無効(実行されません)
  • ファイルを削除せず切り替えられます

event(必須): どのタイミングでルールを実行するか

  • bash: Bashコマンド実行時
  • file: ファイル編集・作成・一括編集時
  • stop: エージェント(自動処理)が終了する時
  • prompt: ユーザーがメッセージを送信した時
  • all: すべてのタイミング

action(任意): ルール発動時の動作

  • warn: メッセージ表示だが操作は許可(デフォルト)
  • block: 操作を阻止(Bashやファイル編集時)またはセッション終了(Stop時)
  • 省略時は warn になります

pattern(シンプル形式): パターンマッチング用の正規表現

  • シンプルな単一条件ルール用
  • Bashコマンド、またはファイル内容にマッチ
  • Python正規表現の構文に対応

例:

event: bash
pattern: rm\s+-rf

高度な形式(複数条件)

複雑な条件が必要な場合:

---
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
---

.envファイルにAPIキーを追加しようとしています。
このファイルが .gitignore に登録されていることを確認してください!

条件内の項目:

  • field: チェック対象のフィールド

    • Bash時: command
    • ファイル時: file_path(ファイルパス)、new_text(新しい内容)、old_text(元の内容)、content(全体内容)
  • operator: マッチング方式

    • regex_match: 正規表現マッチ
    • contains: 文字列を含む
    • equals: 完全一致
    • not_contains: 文字列を含まない
    • starts_with: 指定で始まる
    • ends_with: 指定で終わる
  • pattern: マッチング対象のパターンまたは文字列

すべての条件が一致してはじめてルールが発動します。

メッセージ本文

フロントマターの下のマークダウン内容は、ルール発動時にClaudeに表示されます。

良いメッセージの特徴:

  • 何が検出されたか説明する
  • なぜそれが問題か説明する
  • 代替案やベストプラクティスを提案する
  • 見出しやリストで見やすくする

例:

⚠️ **console.log を検出しました!**

本番環境向けのコードに console.log が追加されています。

**なぜ問題か:**
- デバッグログは本番環境にあってはいけません
- console.log は機密情報を露出させる可能性があります
- ブラウザのパフォーマンスに悪影響を与えます

**代わりの方法:**
- 専用のロギングライブラリを使用する
- コミット前に削除する
- 条件付きのデバッグビルドを使う

イベントタイプ別ガイド

bash イベント

Bashコマンドのパターンにマッチ:

---
event: bash
pattern: sudo\s+|rm\s+-rf|chmod\s+777
---

危険なコマンドが検出されました!

よくあるパターン:

  • 危険なコマンド: rm\s+-rf(危険な削除)、dd\s+if=(ディスク操作)、mkfs(フォーマット)
  • 権限昇格: sudo\s+(スーパーユーザー実行)、su\s+(ユーザー切り替え)
  • パーミッション問題: chmod\s+777(全員読み書き実行可)、chown\s+root(所有者変更)

file イベント

ファイル編集・作成・一括編集のパターンにマッチ:

---
event: file
pattern: console\.log\(|eval\(|innerHTML\s*=
---

問題の可能性があるコードパターンが検出されました!

異なるフィールドでマッチ:

---
event: file
conditions:
  - field: file_path
    operator: regex_match
    pattern: \.tsx?$
  - field: new_text
    operator: regex_match
    pattern: console\.log\(
---

TypeScriptファイル内に console.log があります!

よくあるパターン:

  • デバッグ用コード: console\.log\(、debugger、print\(
  • セキュリティリスク: eval\((動的実行)、innerHTML\s*=(HTML直接挿入)、dangerouslySetInnerHTML(危険なHTML設定)
  • 機密ファイル: \.env$(環境変数ファイル)、credentials(認証情報)、\.pem$(秘密鍵)
  • 自動生成ファイル: node_modules/(外部ライブラリ)、dist/(ビルド出力)、build/(ビルド出力)

stop イベント

エージェント終了時の完了確認パターン:

---
event: stop
pattern: .*
---

終了する前に確認してください:
- [ ] テストを実行した
- [ ] ビルドが成功した
- [ ] ドキュメントを更新した

用途:

  • 必須ステップの確認
  • 完了チェックリスト
  • プロセス強制

prompt イベント

ユーザーのメッセージ内容にマッチ(応用的な使用):

---
event: prompt
conditions:
  - field: user_prompt
    operator: contains
    pattern: deploy to production
---

本番環境へのデプロイチェックリスト:
- [ ] テストが通過している?
- [ ] チームでレビューした?
- [ ] 監視(モニタリング)準備完了?

パターン記述のコツ

正規表現の基本

通常の文字: そのままマッチ

  • rm は "rm" にマッチ
  • console.log は "console.log" にマッチ

特殊文字はエスケープ(文字の前に \ を付ける):

  • .(任意の1文字)→ \.(ピリオド)
  • ( ) → \( \)(括弧)
  • [ ] → \[ \](角括弧)

よく使う記号:

  • \s - 空白(スペース、タブ、改行)
  • \d - 数字(0〜9)
  • \w - 単語文字(a〜z、A〜Z、0〜9、アンダースコア)
  • . - 任意の1文字
  • + - 1個以上
  • * - 0個以上
  • ? - 0個または1個
  • | - または

例:

rm\s+-rf         マッチ: rm -rf、rm  -rf
console\.log\(   マッチ: console.log(
(eval|exec)\(    マッチ: eval( または exec(
chmod\s+777      マッチ: chmod 777、chmod  777
API_KEY\s*=      マッチ: API_KEY=、API_KEY =

パターンのテスト

使う前に正規表現をテスト:

python3 -c "import re; print(re.search(r'パターン', 'テスト文字列'))"

またはオンライン正規表現ツール(regex101.com で Python 方式を選択)

よくある間違い

パターンが広すぎる:

pattern: log    # "log", "login", "dialog", "catalog" すべてマッチ

改善: console\.log\(|logger\.

パターンが具体的すぎる:

pattern: rm -rf /tmp  # この正確なパスだけマッチ

改善: rm\s+-rf

エスケープの問題:

  • YAML の引用文字列内: "pattern" はバックスラッシュを二重にする \\s
  • YAML 非引用: pattern: \s そのまま使える
  • 推奨: YAML で引用なしでパターン記述

ファイル構成

場所: .claude/ ディレクトリ内すべてのルール 名前: .claude/hookify.{説明的な名前}.local.md 除外設定: .gitignore に .claude/*.local.md を追加

良い名前:

  • hookify.dangerous-rm.local.md
  • hookify.console-log.local.md
  • hookify.require-tests.local.md
  • hookify.sensitive-files.local.md

悪い名前:

  • hookify.rule1.local.md(説明的でない)
  • hookify.md(.local が無い)
  • danger.local.md(hookify プレフィックス無し)

ワークフロー

ルール作成の流れ

  1. 避けたい動作を特定する
  2. 関連するツール(Bash、ファイル編集など)を判断
  3. イベントタイプを選ぶ(bash、file、stop など)
  4. 正規表現パターンを書く
  5. プロジェクトルートに .claude/hookify.{名前}.local.md を作成
  6. 即テスト: ルールは次のツール使用時に自動読み込みされます

ルール修正の流れ

  1. .local.md ファイルを編集
  2. パターンまたはメッセージを調整
  3. 即テスト: 変更は次のツール使用時に反映されます

ルール無効化

一時的に: フロントマターで enabled: false に設定 完全削除: .local.md ファイルを削除

例

${CLAUDE_PLUGIN_ROOT}/examples/ に完全な例があります:

  • dangerous-rm.local.md - 危険な rm コマンドをブロック
  • console-log-warning.local.md - console.log について警告
  • sensitive-files-warning.local.md - .env ファイル編集について警告

クイックリファレンス

最小限のルール:

---
name: my-rule
enabled: true
event: bash
pattern: dangerous_command
---

警告メッセージをここに書く

条件付きルール:

---
name: my-rule
enabled: true
event: file
conditions:
  - field: file_path
    operator: regex_match
    pattern: \.ts$
  - field: new_text
    operator: contains
    pattern: any
---

警告メッセージ

イベントタイプ:

  • bash - Bash コマンド
  • file - ファイル編集
  • stop - 完了確認
  • prompt - ユーザー入力
  • all - すべてのイベント

フィールドオプション:

  • Bash: command
  • ファイル: file_path、new_text、old_text、content
  • Prompt: user_prompt

演算子:

  • regex_match、contains、equals、not_contains、starts_with、ends_with
原文(English)を表示

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 active
  • false: Rule is disabled (won't trigger)
  • Can toggle without deleting rule

event (required): Which hook event to trigger on

  • bash: Bash tool commands
  • file: Edit, Write, MultiEdit tools
  • stop: When agent wants to stop
  • prompt: When user submits a prompt
  • all: 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
  • operator: How to match
    • regex_match: Regex pattern matching
    • contains: Substring check
    • equals: Exact match
    • not_contains: Substring must NOT be present
    • starts_with: Prefix check
    • ends_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

  • rm matches "rm"
  • console.log matches "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: \s works 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.md
  • hookify.console-log.local.md
  • hookify.require-tests.local.md
  • hookify.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

  1. Identify unwanted behavior
  2. Determine which tool is involved (Bash, Edit, etc.)
  3. Choose event type (bash, file, stop, etc.)
  4. Write regex pattern
  5. Create .claude/hookify.{name}.local.md file in project root
  6. Test immediately - rules are read dynamically on next tool use

Refining a Rule

  1. Edit the .local.md file
  2. Adjust pattern or message
  3. 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 commands
  • console-log-warning.local.md - Warn about console.log
  • sensitive-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 commands
  • file - File edits
  • stop - Completion checks
  • prompt - User input
  • all - 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

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。