Skip to content
eWiki The practical AI reference

AI Tools

How to Configure Claude Code Hooks to Format Files After Edits

A thick stack of white papers bound together by blue and dark gray straps on a pale surface.

Claude Code hooks can run a formatter automatically after successful file edits. Configure a PostToolUse hook that matches Edit or Write, extracts the changed file’s path from the hook input, and passes that path to the formatter.

What Claude Code Hooks Do After a File Edit

Claude Code hooks run commands at selected lifecycle events. A hook configuration combines an event name, an optional matcher, and a command. For post-edit formatting, the documented workflow uses PostToolUse because this event occurs after a tool completes successfully.

The matcher Edit|Write limits the hook to Claude Code operations that edit an existing file or write a file. When one of those operations succeeds, the hook receives structured JSON input. That input includes fields such as hook_event_name, tool_name, and tool_input; tool_input contains the arguments supplied to the triggering tool, including the relevant file path.

This makes the hook deterministic: instead of relying on an instruction that Claude should remember to format each change, the configured command runs whenever the matching event occurs.

A blue tool lifts a gray rectangular block from a grid of black and gray blocks on a white surface.

How to Configure a PostToolUse Formatting Hook

Add a PostToolUse entry to the appropriate Claude Code settings file. The provider-documented Prettier configuration uses this structure:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
          }
        ]
      }
    ]
  }
}

The PostToolUse event determines when the hook is considered. The Edit|Write matcher determines which successful tool operations trigger it. The command then reads the hook’s JSON input from standard input. jq -r '.tool_input.file_path' selects the edited path, and xargs passes that value to npx prettier --write.

For a team-wide configuration, the hook can be placed in .claude/settings.json and checked into the repository. If the command invokes a script stored in the project, official Academy guidance recommends using CLAUDE_PROJECT_DIR when referring to that script. Before sharing the configuration, review the formatter command and confirm that repository-level automation is the intended scope.

This is a documented configuration pattern, not a claim that the command was independently executed or tested for this article.

Hands holding a small metal hook with a blue accent above neat stacks of blank white cards.

Automated Hooks vs. Manual Formatting

An automated hook is preferable when the same formatter should run reliably after every matching Claude Code edit. It removes the repeated step of entering a formatting command and avoids depending on a prompt or project instruction being followed each time. A repository-level configuration can also give contributors the same hook behavior.

Manual formatting remains a reasonable editorial choice when formatting is occasional or when a person should review each situation before running the command. It requires more deliberate action but preserves per-run control. This frequency-and-control distinction is practical guidance; the documented product distinction is that a matching hook executes deterministically at its configured event.

Consider the scope as well as convenience. A personal workflow may call for an individual settings choice, while a team that wants consistent behavior can share .claude/settings.json. If every successful Edit or Write operation should lead directly to formatting, automation fits the requirement. If formatting should happen only after selective review, run the formatter manually.

Conclusion

Claude Code formatting hooks are best suited to repeatable rules that should run after every matching successful edit; manual commands offer more control for occasional or review-sensitive work. Official guidance identifies Prettier, Black, and gofmt as formatter examples for PostToolUse automation.

Choose the appropriate personal or repository scope, review the command, and then add the documented PostToolUse structure with the Edit|Write matcher to the corresponding Claude Code settings file.

Disclosures and limitations

  • This article was created with AI assistance and is based on the cited Claude Code documentation. The described workflow is not presented as an independent hands-on test.
  • Tool mentions are informational and do not constitute a product ranking or purchasing recommendation.

Sources

Improve this guide

Report a problem or suggest the next question

Submissions enter a private editorial queue. They are reviewed and deduplicated before any public update or new page is created.