Skip to main content
Bridge

October 9, 2026

How to Auto-Format Files Edited by Claude Code

Learn how to configure Claude Code to automatically run Prettier or any other formatter every time it edits or writes a file. By setting up a PostToolUse hook in your project settings, you can guarantee deterministic code formatting without relying on the LLM to remember.

← AI Playbook
Claude CodePrettierAutomationPlaybookHow-to
HyunKi··4 min read

Verified on October 9, 2026

How to Auto-Format Files Edited by Claude Code

You will configure Claude Code to format files with Prettier immediately after every modification. Prompting an agent in CLAUDE.md to format code relies on probabilistic instructions that fail under high context load. Setting up a PostToolUse hook in .claude/settings.json provides deterministic formatting by executing Prettier at the system level whenever Claude Code runs the Edit or Write tools, completely outside the model's reasoning loop.

Before you start

  • Claude Code installed on your development machine.
  • jq installed and accessible in your system PATH.
  • Prettier installed in your project (npm install --save-dev prettier).

How the hook runs

flowchart LR
  A[Claude Code calls Edit or Write] --> B[Tool updates target file]
  B --> C[PostToolUse hook fires]
  C --> D[Pipes JSON payload to stdin]
  D --> E[jq parses tool_input.file_path]
  E --> F[Prettier formats the edited file]

Step 1: Choose the configuration scope

Claude Code reads settings from three locations: ~/.claude/settings.json for global machine rules, .claude/settings.local.json for gitignored local overrides, and .claude/settings.json for repository-wide configuration. Use the project-level file so your entire team shares the same formatting hook.

mkdir -p .claude

Step 2: Add the PostToolUse hook configuration

Open .claude/settings.json and declare a PostToolUse entry with a matcher for file edits. When triggered, Claude Code pipes an event payload to stdin. Extract .tool_input.file_path using jq and pass it directly to Prettier.

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "FILE=$(jq -r '.tool_input.file_path // empty') && [ -n \"$FILE\" ] && npx prettier --write --ignore-unknown \"$FILE\" 2>/dev/null || true"
          }
        ]
      }
    ]
  }
}

Step 3 (optional): Delegate execution to a bash script

Inline shell scripts become fragile as logic grows. Move the formatting instructions into a dedicated script to guard against environments without Prettier and prevent failed hook executions on non-code assets.

Create .claude/hooks/prettier-formatter.sh:

#!/bin/bash
set -e

input=$(cat)
file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')

if [ -z "$file_path" ] || [ "$file_path" = "null" ]; then
  exit 0
fi

if [ -f "package.json" ] && grep -q '"prettier"' package.json; then
  npx prettier --write --ignore-unknown "$file_path" 2>/dev/null || true
fi

Make the script executable:

chmod +x .claude/hooks/prettier-formatter.sh

Point your .claude/settings.json hook command to the script:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "bash .claude/hooks/prettier-formatter.sh"
          }
        ]
      }
    ]
  }
}

Step 4: Verify hook discovery

Start a new Claude Code session in your terminal. Run the /hooks command to confirm that the project configuration registered correctly.

/hooks

Confirm that the output lists your PostToolUse hook sourced from .claude/settings.json.

Step 5: Test the formatting pipeline

Ask Claude Code to generate a file with inconsistent indentation or formatting to verify that the hook executes on disk.

Write a file named test.js with poorly formatted JavaScript code.

Inspect test.js after the operation finishes. Claude Code executes the Write tool, triggers the hook on the emitted path, and leaves the file formatted according to your repository rules.

Common mistakes

  • Running full repository checks. PostToolUse hooks run synchronously and block the agent turn until the command exits. Never run whole-project formatters or full test suites in a tool hook. Target only the extracted file path.
  • Incorrect tool matcher casing. Claude Code tool names are case-sensitive. Using lowercase "edit|write" fails to match any tool execution. Use "Edit|Write" or "Edit|MultiEdit|Write" to catch batched edits.
  • Silent failures on missing binaries. If jq is not found in your environment, shell hooks abort without surfacing visible errors in the prompt. Test your hook manually by piping a mock payload: echo '{"tool_input": {"file_path": "test.js"}}' | bash .claude/hooks/prettier-formatter.sh.

Related

Want to try Bridge?

Bridge ships early October 2026. Become a founding member and lock in founders pricing.

Become a founding member