Automations

Claude Code hook that formats and lints each file after Claude edits it

A PostToolUse hook for .claude/settings.json that runs the right formatter on every file Claude writes or edits (Prettier, Ruff, gofmt, rustfmt, Pint) and sends remaining lint errors back to Claude to fix.

Automation in Engineering by AISkillHub Library

What it does

Agents often leave formatting drift and lint errors that you then fix by hand or catch in CI. This hook runs after every `Edit` or `Write` tool call, formats the changed file with your project's own tools, and, when a linter still reports problems, returns them to Claude so it fixes them in the same session.

**How it decides what to run** By file extension, and only if the tool is available in the project: - `.js .jsx .ts .tsx .mjs .cjs .json .css .scss .md .yaml .yml .html .vue .svelte`: Prettier, only when it is installed locally (`node_modules/.bin/prettier`). For JS and TS files it also runs ESLint with `--fix` if installed locally, and reports remaining errors. - `.py`: `ruff format` and `ruff check --fix`, through `uv run` when a `uv.lock` exists or a `ruff` binary otherwise. Remaining Ruff errors are reported. - `.go`: `gofmt -w`. - `.rs`: `rustfmt` on the file, using your `rustfmt.toml` if present. - `.php`: Laravel Pint (`vendor/bin/pint`) if installed.

Files outside the project directory are skipped. Tools that are not installed are skipped silently, so the hook never blocks work because of missing tooling.

**How errors reach Claude** For `PostToolUse`, exit code 2 sends the hook's stderr to Claude as feedback (the edit has already happened, so nothing is undone). The script uses that only for lint errors that remain after auto-fix, and keeps the message short.

**Limits** It formats single files, so project-wide checks such as type checking still belong in CI or a pre-commit step. It uses your configuration files; it does not add formatter config of its own. Requires `jq`.

How to use it

1. Install `jq` if needed. 2. Save both files below at the paths shown. If you already have `.claude/settings.json` (for example from another hooks listing), merge the `PostToolUse` block into its `hooks` object instead of replacing the file. 3. Make the script executable: `chmod +x .claude/hooks/format-on-edit.sh`. 4. Make sure your formatters are installed as project dependencies (Prettier/ESLint in `devDependencies`, Ruff via uv or pip, Pint via Composer). 5. Run `/hooks` in Claude Code to confirm the hook is registered, then ask Claude to edit a file and check the result is formatted.

Files

Save each file at the path shown, relative to your project root.

  • .claude/hooks/format-on-edit.sh1.9 KB
  • .claude/settings.json289 B