Claude Hookbook

Hooks / formatting

Prettier after edit

Runs Prettier on any JS/TS/JSON/CSS/Markdown file right after Claude edits it.

PostToolUseEdit|Writelow riskneeds manual check

What it does

Fires on PostToolUse for Edit and Write. A small script extracts the edited file path from the hook JSON, gates on web-file extensions, and runs prettier --write when Prettier is available. Files Claude did not touch are never formatted.

When it runs

EventPostToolUse
MatcherEdit|Write
Scopeproject
Platformsmacos, linux
Timeout30s
Requiresjq prettier
Failure modeobserve
Verified againstClaude Code hooks reference, event/matcher schema checked 2026-09-10
Test statusneeds manual check - run the test steps below before relying on it

Config

Merge this into .claude/settings.json - or use the generator to combine hooks.

{
  "hooks": {
    "PostToolUse": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/prettier-after-edit.sh",
            "timeout": 30
          }
        ],
        "matcher": "Edit|Write"
      }
    ]
  }
}

Script

Save as .claude/hooks/prettier-after-edit.sh and chmod +x it.

#!/bin/bash
# prettier-after-edit: run after Claude edits a file. Reads hook JSON from stdin.
set -u
file=$(jq -r '.tool_input.file_path // empty')
[ -z "$file" ] && exit 0
[ -f "$file" ] || exit 0
case "$file" in
*.js|*.jsx|*.ts|*.tsx|*.json|*.css|*.md) ;;
  *) exit 0 ;;
esac
if ! command -v prettier >/dev/null 2>&1; then
  exit 0  # formatter not installed; stay silent
fi
prettier --write "$file" >/dev/null 2>&1
exit 0

Install

  1. Save the script to .claude/hooks/prettier-after-edit.sh and run: chmod +x .claude/hooks/prettier-after-edit.sh
  2. Install the formatter (prettier) and make sure it is on your PATH, plus jq.
  3. Add the config fragment to .claude/settings.json (or use the generator).

Test it

Sources

Related hooks

ESLint after JS/TS edit

Runs eslint --fix on JavaScript and TypeScript files after Claude edits them, and warns (never blocks) on remaining problems.

PostToolUseEdit|Writereview riskneeds manual check

Typecheck after TypeScript changes

Runs tsc --noEmit in the background after Claude edits TypeScript, so type errors surface even when tests don't cover the file.

PostToolUseEdit|Writereview riskneeds manual check

Pro Pack (later)

Framework bundles, cross-platform scripts, tested blocking policies, and a test harness. Free recipes and the generator stay free.

Waitlist opens soon.