Try free

Last updated 13 October 2026

Auto-format code after Claude Code edits

AI edits often break your style guide

Claude Code works with impressive speed to refactor your logic and write new functions. However, it does not always respect the specific formatting rules of your project. You might find that the agent uses single quotes when your linter demands double quotes. It might use four spaces for indentation in a project that requires two. These small deviations create a friction point where you cannot simply commit the changes without a manual cleanup phase.

This problem gets worse as the session continues. If the agent reads a file it previously edited with the wrong style, it might continue to write new code in that incorrect style. You end up with a codebase that looks like a patchwork of different conventions. Fixing these issues manually after every prompt defeats the purpose of using a high speed coding agent. You need a way to force the code into the correct shape the moment the agent touches it.

Automate formatting with hook events

Claude Code includes a system for hooks that trigger bash commands based on specific events. These hooks allow you to integrate your existing developer tools directly into the agent workflow. Instead of you running a formatter in a separate terminal window, the agent can trigger it for you. This ensures that every file written to disk is immediately processed by your project linting and formatting rules.

The two most useful events for this purpose are PostToolUse and Stop. PostToolUse fires immediately after the agent uses a tool like Write or Edit. This provides the most aggressive formatting because it happens during the agent's work cycle. The Stop event fires when the agent has finished its entire task and is waiting for your next input. Choosing between them depends on how heavy your formatting process is and how much you want the agent to see its own formatted output.

Configure settings in the project folder

You manage these hooks through the .claude/settings.json file located in your repository root. If this file does not exist, you can create it manually. This file allows you to define a list of hooks and the corresponding bash commands they should run. Because this file is part of your project, you can commit it to the repository so that every team member using Claude Code benefits from the same automated formatting.

If you want to use formatting rules that are specific to your local environment, you can use .claude/settings.local.json instead. This file is for your own untracked overrides. It is the perfect place for experimental hooks or paths to local binaries that your colleagues might not have. Claude Code merges these settings, giving priority to your local choices while still respecting the core project instructions.

The PostToolUse hook for immediate results

When you use the PostToolUse hook, you target the moment a file is modified. In your settings file, you define an entry for this event and provide a command like prettier --write or black. Every time the agent uses the Edit tool to change a line or the Write tool to create a file, your command executes. This means the file on your disk is always in a valid state according to your style guide.

This approach is beneficial when the agent needs to perform multiple steps. If it writes a function and then needs to read it back to write a test, it will read the formatted version. This prevents the agent from getting confused by its own lack of stylistic consistency. You should be aware that this can add a few seconds of delay to every tool use if your formatter is slow or if your project is massive.

The Stop hook for efficiency

The Stop event is often the preferred choice for developers who want a faster interactive experience. This hook triggers only when the agent has completed its current response and stopped working. If you ask the agent to refactor three different files, it might use the Edit tool several times. With the Stop hook, your formatter runs only once at the end of the entire process.

This saves significant time and system resources. It also prevents potential conflicts where a background formatting process might be trying to save a file at the exact same moment the agent is trying to perform a second edit. By waiting for the agent to reach an idle state, you ensure that the file system is stable. You can check the status of the agent to see if it is still working or if it has reached the Stop event.

Manage many projects with adhdTerminal

Running automated hooks across many different repositories can be difficult to track in a standard terminal. You might have one project using Prettier and another using a complex custom linting script. This is an area where adhdTerminal provides a lot of clarity. It runs each Claude Code session as a separate tile on a wall, showing you exactly what is happening in every project simultaneously.

The tiles in adhdTerminal show whether a project is working, waiting for you, or idle. When a hook like Stop triggers a formatter, you can see the results in the tile preview. This allows you to monitor multiple refactoring tasks across different codebases without manually switching directories. If a formatter fails and produces an error, you will see it immediately on your wall of tiles rather than discovering it later when a build fails.

Handling permissions for bash commands

Claude Code has different permission modes that affect how hooks run. In the default mode, the agent might ask for your permission before it executes a bash command triggered by a hook. This is a security feature to ensure you know what is happening on your system. If you find these prompts disruptive to your flow, you can change the permission mode.

By pressing Shift+Tab inside a session, you can cycle through modes like plan and bypassPermissions. If you switch to bypassPermissions, the agent will run your formatting hooks automatically without asking for approval every time. This creates a much smoother experience where the code is simply fixed in the background while you focus on the logic of your prompts.

Cleaning the session context

Automated formatting can sometimes lead to a lot of small changes that fill up your conversation history. Every time the agent sees a change, it adds to the context of the session. If you feel the agent is getting slow or losing focus, use the /compact command. This command summarizes the session so far and frees up space in the context window, removing the noise of minor stylistic edits.

You can also use the /status command to see how much context you have used and what tools have been active. If you find that the formatting hooks are adding too much overhead to your costs, use /cost to monitor your spend. Balancing automated quality control with token usage is a key skill for any developer using Claude Code in a professional environment.

Questions people ask

Can I run multiple commands in a single hook?

Yes, you can chain commands in the bash string inside your settings file. Use the standard bash syntax like a double ampersand to run a formatter followed by a linter. This ensures that both style and syntax are checked every time the event triggers.

What happens if my formatting command fails?

If the bash command in your hook returns a non-zero exit code, Claude Code will usually report this in the terminal. You can use the /doctor command to check your general installation if you suspect the environment is not configured correctly. Most failures are due to missing binaries or incorrect paths in your local shell.

Does this work with CLAUDE.md instructions?

The CLAUDE.md file is for project instructions rather than hook configuration. While you can use CLAUDE.md to tell the agent how it should format code, the actual automation of those commands must live in the settings.json file. Use CLAUDE.md to provide the context and settings.json to provide the action.

Read next

More on claude code basics·All posts