agentleFS
VersoXBTVersoXBT/claude-initial-setup/skills/hooks/pre-post-hooks/SKILL.md

pre-post-hooks

Skill4 starsChanged 9 months agoMITView on GitHub

Guide for configuring PreToolUse, PostToolUse, and Stop hooks in Claude Code. Use when the user asks about hooks, wants to run commands before/after tool calls, needs auto-formatting, auto-linting, validation gates, or mentions settings.json hook config.

  • Commits and pushes

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 4 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use pipe | for multiple matchers:

    line 86
  2. Distinctive:

    Run checks or formatting after tool execution without blocking:

    line 127
  3. Distinctive:

    Slow hooks on hot paths: Do not run expensive commands (full test suite, large builds) in PostToolUse on every Edit.

    line 160
  4. Distinctive:

    Blocking reads: Do not use PreToolUse to block Read or Grep calls.

    line 161
  5. Very specific:

    Hardcoded paths: Use $CLAUDE_FILE_PATH instead of hardcoding file paths.

    line 163
  6. Distinctive:

    Blocking: Only PreToolUse can block (non-zero exit code)

    line 176

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Pre/Post Hooks

Claude Code hooks are shell commands that execute automatically in response to tool events. They enable validation, formatting, and guardrails without manual intervention.

When to Use

  • User wants to run a command before or after a tool executes
  • User asks about PreToolUse, PostToolUse, or Stop hooks
  • User wants to add validation gates or blockers
  • User needs to configure hooks in settings.json
  • User mentions auto-running commands on file edit, git push, or session end

Core Patterns

Hook Types and Lifecycle

PreToolUse  --> Tool Executes --> PostToolUse
                                       |
                                  (session ends)
                                       |
                                     Stop
  • PreToolUse: Runs before a tool call. Can block execution by returning a non-zero exit code.
  • PostToolUse: Runs after a tool call completes. Used for side effects like formatting or checks.
  • Stop: Runs when the agent session ends. Used for final audits or cleanup.

Hook Configuration in settings.json

Hooks live in ~/.claude/settings.json under the hooks key:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "echo 'About to write a file'",
        "description": "Log before file writes"
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Edit",
        "command": "npx prettier --write \"$CLAUDE_FILE_PATH\"",
        "description": "Auto-format after editing JS/TS files"
      }
    ],
    "Stop": [
      {
        "command": "echo 'Session ended'",
        "description": "Log session end"
      }
    ]
  }
}

Matcher Patterns

The matcher field filters which tool triggers the hook. It matches against tool names:

{
  "matcher": "Edit",
  "command": "..."
}

Common tool names to match:

  • Write — file creation
  • Edit — file modification
  • Bash — shell command execution
  • Read — file reading
  • Glob — file search
  • Grep — content search

Use pipe | for multiple matchers:

{
  "matcher": "Write|Edit",
  "command": "npx prettier --write \"$CLAUDE_FILE_PATH\""
}

Omit matcher entirely to match ALL tool calls (useful for Stop hooks).

Environment Variables in Hooks

Hooks receive context via environment variables:

VariableDescription
CLAUDE_FILE_PATHThe file path being operated on
CLAUDE_TOOL_NAMEName of the tool being called
CLAUDE_SESSION_IDCurrent session identifier

Blocking with PreToolUse

A PreToolUse hook that exits non-zero blocks the tool from executing:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "command": "echo \"$CLAUDE_FILE_PATH\" | grep -qv '\\.md$' || (echo 'BLOCKED: Do not create markdown files' && exit 1)",
        "description": "Block creation of .md files"
      }
    ]
  }
}

PostToolUse Side Effects

Run checks or formatting after tool execution without blocking:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit",
        "command": "if echo \"$CLAUDE_FILE_PATH\" | grep -q '\\.ts$'; then npx tsc --noEmit \"$CLAUDE_FILE_PATH\" 2>&1 | head -20; fi",
        "description": "Type-check TypeScript files after edit"
      }
    ]
  }
}

Stop Hook for Final Audit

{
  "hooks": {
    "Stop": [
      {
        "command": "git diff --cached --name-only | xargs grep -l 'console.log' 2>/dev/null && echo 'WARNING: console.log found in staged files'",
        "description": "Audit staged files for console.log before session ends"
      }
    ]
  }
}

Anti-Patterns

  • Slow hooks on hot paths: Do not run expensive commands (full test suite, large builds) in PostToolUse on every Edit. It blocks the agent loop. Use targeted, fast checks instead.
  • Blocking reads: Do not use PreToolUse to block Read or Grep calls. These are non-destructive and blocking them disrupts exploration.
  • Missing error handling: Hook commands that fail silently hide problems. Always include meaningful error messages in blocking hooks.
  • Hardcoded paths: Use $CLAUDE_FILE_PATH instead of hardcoding file paths. Hooks should be project-agnostic.
  • Too many hooks: Each hook adds latency to every matching tool call. Keep the hook list focused on high-value checks.

Quick Reference

Hook TypeTimingCan Block?Use Case
PreToolUseBefore toolYes (exit 1)Validation, gates, warnings
PostToolUseAfter toolNoFormatting, type-checking, linting
StopSession endNoFinal audits, cleanup

Config location: ~/.claude/settings.json > hooks Matcher syntax: Tool name string, pipe-separated for multiple Blocking: Only PreToolUse can block (non-zero exit code)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.