NativeAIHub

Auto Format Hook

Runs Prettier on saved files after every edit, keeping formatting consistent without manual intervention.

Hook·2 sections·1 min read
codingdevops
Install Prompt

Paste this into Claude Code to set it up:

Add a PostToolUse hook to your ~/.claude/settings.json that runs Prettier on files after every Write or Edit tool use.

In your settings.json, add this under the "hooks" key:

"PostToolUse": [
  {
    "matcher": "Write|Edit",
    "command": "npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true"
  }
]

This ensures every file Claude writes or edits gets formatted automatically. The matcher targets both Write (new files) and Edit (modifications). The || true prevents Prettier errors on unsupported file types from blocking Claude's workflow.

01What It Does

Formatting that never drifts

Claude generates well formatted code, but it does not always match your project's exact Prettier configuration (tab width, trailing commas, quote style, print width). The Auto Format Hook closes that gap by running Prettier after every file change. You never need to ask Claude to "fix the formatting" because the hook handles it mechanically, every single time.
1

Claude writes or edits a file

The Write or Edit tool creates or modifies a file. The PostToolUse event fires as soon as the tool completes.

2

Prettier formats the file

The hook runs <code>prettier --write</code> on the changed file, applying your project's .prettierrc configuration automatically.

3

Claude sees the formatted result

On subsequent reads, Claude works with the properly formatted version. This prevents formatting drift from accumulating across multiple edits.

02When to Use

Automation saves more than time

If your CI pipeline rejects PRs with formatting issues, the Auto Format Hook eliminates that entire category of failure. Instead of running Prettier manually after a long coding session (and discovering 40 files need formatting), every file is formatted the moment it changes.

This hook is especially valuable during large refactors where Claude touches dozens of files in a single session. Without it, formatting inconsistencies accumulate silently and only surface during code review or CI. With it, each file is correct from the moment it is written.

If you also use the Auto Lint Hook, place the format hook first so Prettier runs before ESLint. This prevents the linter from flagging formatting issues that Prettier would have resolved.