🪄Hook

Auto-format on edit

PostEditFile hook that runs the right formatter per file type. Keeps the working tree tidy automatically.

Surfaces
hook · trigger
Complexity
beginner
Trigger
hook
Est. tokens
0

What It Does

After every Claude Edit or Write, the PostEditFile hook runs the project's formatter for that file type. JavaScript and TypeScript get Prettier; Python gets Ruff. No tokens consumed — the hook is a shell command, not a Claude call.

When It Triggers

  • PostEditFile — automatic, not natural language
  • Any Edit or Write tool call producing a file matching the matcher regex

SKILL.md

markdown
---
name: auto-format-hook
description: PostEditFile hook that runs the project's formatter after Claude edits a file. Keeps the working tree clean without manual intervention.
---

# Auto-Format on Edit

## Install
Place this skill next to a `settings.json` hook entry (see files).

## How It Works
Claude Code fires PostEditFile after every Edit/Write. The hook reads the
edited file path and runs the appropriate formatter.

settings.json

json
{
  "hooks": {
    "PostEditFile": [
      {
        "matcher": ".*\\.(ts|tsx|js|jsx)$",
        "command": "npx prettier --write \"$CLAUDE_EDITED_FILE\""
      },
      {
        "matcher": ".*\\.py$",
        "command": "ruff format \"$CLAUDE_EDITED_FILE\""
      }
    ]
  }
}

Gotchas

  • Match the CLAUDE_EDITED_FILE env var — other names (file, $1) do not exist.
  • A broken formatter blocks progress. Ensure the command is installed before enabling.
  • Hook output appears in the transcript — keep commands quiet (--log-level=error).

Before you continue

We use analytics cookies to understand how the documentation is used and improve the experience. Privacy Policy.