Auto-format files the moment Claude Code edits them — with whatever formatter your project already uses.
A single PostToolUse hook that runs after every Write/Edit. It looks at the edited file's extension and dispatches to the matching formatter. If that formatter isn't installed, it silently does nothing — so it's safe to drop onto any machine and into any repo.
Without this, you either format by hand or approve a formatter command on every edit. This eliminates that loop: the file is formatted on the way out, no prompt, no diff noise from inconsistent style.
| File types | Formatter (first one found) |
|---|---|
.py |
black, else ruff format |
.js .jsx .ts .tsx .json .css .scss .html .md .yaml .yml |
project-local prettier, else global prettier |
.go |
gofmt |
It never auto-installs anything. No formatter present = no-op.
git clone https://github.com/jay739/claude-autoformat
cd claude-autoformat
./install.shThe installer copies hooks/autoformat.sh into ~/.claude/hooks/ and merges a hook entry into ~/.claude/settings.json (your existing settings and hooks are preserved). It's idempotent — re-running replaces the entry instead of duplicating it.
Then open /hooks in Claude Code once (or restart) so the new config loads.
The hook only runs a formatter that's on your PATH:
pipx install black # Python (avoids PEP 668 "externally-managed" errors)
npm install -g prettier # JS/TS/JSON/CSS/MD/YAML
# gofmt ships with GoA project-local prettier (node_modules/.bin/prettier) is preferred over the global one, so per-repo prettier configs are respected.
- Reads the hook JSON on stdin and pulls the file path with
jq. - Dispatches by extension. The prettier branch
cds into the edited file's own directory first, sonpx --no-installcan walk up to that project'snode_modules(hooks run with the working directory set to your Claude session, not the project). - Everything is guarded so a missing tool, an unknown file type, or a parse error just exits cleanly.
./uninstall.shRemoves the hook entry from settings.json and deletes the script.
jq(for reading the hook payload)python3(used by the installer for a safe JSON merge)
MIT