Skip to content

Make Claude Code check its own edits with hooks

A hook is a command Claude Code runs by itself at a set moment, like after every edit. Set up one that tidies each file it touches, check it works, and know how to turn it off.

By Nick

4 min read

Coding tools Intermediate
On this page
Time
15 to 25 minutes the first time
Cost
Nothing extra; it runs on your own computer
Level
Intermediate

Before you start

You can ask Claude Code to tidy every file it touches. It will, most of the time. A hook makes it every time, because your computer runs the command, not the agent’s memory.

1. Pick one boring chore

Good first hooks are chores with one right answer: format the file, check it for obvious mistakes, play a sound when Claude needs you. Bad first hooks make decisions or touch anything outside the project.

This guide uses formatting. If your project already uses Prettier (a common tool that tidies code layout), every edit Claude makes will come out tidied. Practise on a throwaway copy of the project, committed in Git, so you can see exactly what changed.

2. Add the hook to the project settings

Hooks live in settings files. There are three you will meet:

  • .claude/settings.json in the project: shared with anyone who has the project. Use this one.
  • .claude/settings.local.json in the project: just for you, kept out of Git.
  • ~/.claude/settings.json in your home folder: applies to every project.

Open or create .claude/settings.json in the project and add this, which is the example from Anthropic’s own documentation:

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

In plain English: after (PostToolUse) Claude uses its Edit or Write tool, read which file it touched, and run Prettier on that file. The command needs jq, a small tool for reading this kind of data. If you do not have it, ask Claude Code to check and install it.

If the file already has other settings in it, ask Claude Code to merge this in for you and show you the result. Do not paste a second pair of outer braces.

3. Read what you just allowed

A hook runs with every permission your user account has. Anthropic’s documentation is blunt about it: hooks “can modify, delete, or access any files your user account can access.”

So read the command. This one reads a file name and runs a formatter on it. That is all. Claude Code also holds back project hooks until you accept the “trust this folder” prompt, which is one more reason to only trust folders you know.

4. Check it is there, then test it

Type /hooks inside Claude Code. It opens a read-only list of every hook it knows about and which file each one came from. You should see your PostToolUse hook, labelled as a project hook. If it is missing, the JSON probably has a typo; ask Claude to check the file.

Now test it. Ask Claude to make a small edit to a file that is laid out messily, then run git diff. The diff should show Claude’s change and the tidy-up.

What working looks like. The edit lands already formatted, every time, without you mentioning formatting. If the command fails, Claude Code shows a short “hook error” notice and carries on; the edit is not lost.

5. Know how to turn it off

  • Remove one hook: delete its entry from the settings file. There is no switch for a single hook, so delete it (Git keeps the old version if you want it back).
  • Turn off all hooks: add "disableAllHooks": true to the settings file.
  • Check it is gone: open /hooks again and make one more edit.

Once one hook is earning its keep, the same idea scales: a check that runs the tests, a sound when Claude is waiting for you. Add them one at a time, and only for chores you would otherwise forget. Things that need judgement still belong in CLAUDE.md or a skill.

Common questions

What is the difference between a hook and CLAUDE.md?

CLAUDE.md is advice the agent reads and usually follows. A hook is a command your computer runs every time, whether the agent remembers or not. Use CLAUDE.md for judgement and hooks for chores that must always happen.

Can a hook undo a bad edit?

No. A hook that runs after an edit fires once the file has already changed. It can tidy the file or tell Claude what is wrong, but the undo button is still Git.

Does it catch every change?

Only the ones it matches. A hook on Edit and Write does not fire when Claude changes a file by running a terminal command instead. For most tidy-up jobs that is fine.

Is it safe to copy hooks from the internet?

Treat them like any script from a stranger. A hook runs with all the permissions your user account has, so read every line before you add it, and ask Claude Code to explain any you do not understand.

What to do next

Related reading

Last reviewed 22 October 2026 by Nick. Checked against claude-code docs checked 2026-10-21.

Software changes fast. If something here is out of date, tell me and I'll fix it.

Get new guides by email

Occasional and practical. No spam, no hype, unsubscribe whenever.

Occasional practical guides from UncomplicateAI. You will get a confirmation email before anything else is sent. Unsubscribe any time. Privacy

You are not subscribed until you confirm the email. Unsubscribe any time.