format-code — Claude Code plugin: auto-format on write
format-code — a PostToolUse hook that formats each file right after Claude writes or edits it: ruff for Python, prettier for JS, TS, JSON, MD, YAML and HTML.
MIT ★ 586 updated 2026-10-26
/plugin marketplace add karanb192/claude-code-hooks
/plugin install format-code@claude-code-hooksWhat format-code does
The hook listens on PostToolUse for Write and Edit. It reads the file path from the event, resolves relative paths against the session cwd and picks a formatter by extension. Python gets two ruff passes, uv run ruff check --fix then uv run ruff format; .js, .ts, .json, .md, .yaml, .yml and .html go to npx --yes prettier --write.
Paths travel as argv and never touch a shell, so a filename full of metacharacters cannot inject a command. Unknown extensions and files that no longer exist are skipped without noise. A formatter that fails does not stop the session: the hook answers {} and writes the outcome down.
Python formatting needs uv on your PATH, everything else needs Node with npx. Missing either one means the error is logged and the run continues, so nothing breaks and nothing gets formatted. There is no config and no env vars to set. Logs land in ~/.claude/hooks-logs/<date>.jsonl with paths, outcomes and session ids.
What it helps with
- keep Python imports and spacing ruff-clean after every edit
- run prettier on a freshly generated component without a manual step
- tidy JSON and YAML configs the agent just rewrote
- handle a filename with spaces or quotes without breaking the formatter call
- trace a skipped file through the hook log
Example prompts for the agent
- Add a date-parsing helper to
utils.pyand save it - Rewrite
package.jsonwith alintscript - Create
config.yamlfor the build settings
How to install format-code
Plugin marketplace
/plugin marketplace add karanb192/claude-code-hooks/plugin install format-code@claude-code-hooksStep by step, with commands for each platform: Claude Code plugins: marketplaces, installing, and building your own
Questions about format-code
How do I install format-code in Claude Code?
Add the marketplace first: /plugin marketplace add karanb192/claude-code-hooks. Then install the plugin: /plugin install format-code@claude-code-hooks. Restart Claude afterwards.
What does format-code do?
It rewrites the touched file in place after each write or edit, using ruff for Python and prettier for JS, TS, JSON, MD, YAML and HTML. Everything else is skipped.
What does the hook need to run?
uv on your PATH for Python, since it runs ruff. Node with npx for the other file types. A missing formatter gets logged and the session carries on.
Source text
format-code · MIT
# format-code
> Auto-formats Python (ruff) and JS/TS/HTML/JSON/MD/YAML (prettier) the moment Claude edits them.
A `PostToolUse` hook fires after every `Write` and `Edit` call, resolves the file path (relative paths resolve against the session cwd), picks a formatter by extension, and formats the file in place: `uv run ruff check --fix` then `uv run ruff format` for `.py`, `npx --yes prettier --write` for `.js`, `.ts`, `.json`, `.md`, `.yaml`, `.yml`, and `.html`. File paths are passed as argv, never through a shell, so filenames with shell metacharacters cannot inject commands. Unsupported extensions and missing files are skipped silently, a formatter failure never blocks the session, and every outcome (FORMATTED / SKIP / ERROR) is logged.
## Install
```
/plugin marketplace add karanb192/claude-code-hooks # once per machine
/plugin install format-code@claude-code-hooks
```
Restart Claude Code, done. (Or from a shell: `claude plugin install format-code@claude-code-hooks`.)
## What it does
| Event | Runs | What happens |
|-------|------|--------------|
| PostToolUse (`Write\|Edit`) | sync | Formats the touched file in place with ruff (Python) or prettier (JS/TS/JSON/MD/YAML/HTML), then returns `{}` so the session continues either way. |
Formatters run from your PATH: Python formatting needs `uv` (which runs `ruff`), the rest needs `npx` (Node); `npx --yes` fetches prettier on first use if it is not already installed. If a formatter is missing, the hook logs the error and moves on without blocking.
## Configuration
No configuration needed, and no environment variables to set. Logs go to `~/.claude/hooks-logs/<date>.jsonl` (the log directory is derived from `$HOME`).
## Data & privacy
Rewrites the edited file in place and records file paths, formatter outcomes, and session ids in the local log. The hook itself makes no network calls; the one indirect exception is `npx --yes prettier`, which downloads prettier from the npm registry the first time if it is not installed locally.
## Uninstall
```
/plugin uninstall format-code@claude-code-hooks
```
Part of [claude-code-hooks](https://github.com/karanb192/claude-code-hooks), a collection of production-ready Claude Code hooks, each installable on its own as a plugin.
© Karan Bansal · MIT · karanb192/claude-code-hooks