Skip to main content
plugin

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

Install into Claude Code · Plugin marketplace All methods and platforms ↓
/plugin marketplace add karanb192/claude-code-hooks
/plugin install format-code@claude-code-hooks
Open on GitHub

What 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.py and save it
  • Rewrite package.json with a lint script
  • Create config.yaml for the build settings

How to install format-code

Plugin marketplace

/plugin marketplace add karanb192/claude-code-hooks
/plugin install format-code@claude-code-hooks

Step 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

Similar plugins

All plugins for Agent setup: memory, context, orchestration →

From the same set28

The whole set →
receipts receipts — a skill that displays your personal quality card across sessions: failure rate, edit churn, and tokens per task broken down by model version. Runs silently in the background and surfaces shifts in model performance. CX Codex +1 ★ 586repo stars guard-pack guard-pack is a Claude Code plugin that runs seven guard hooks in one Node process. The first blocking verdict stops the chain. ★ 586repo stars notify-permission notify-permission sends Slack cards when Claude Code waits for permissions or user input, freeing you from watching long-running terminal tasks. ★ 586repo stars session-logger session-logger is a Claude Code plugin that keeps a Markdown record of each session. Its hooks track the repository, touched file paths, and Bash commands. ★ 586repo stars auto-stage auto-stage stages modified files in Git automatically whenever Claude Code writes or edits them, keeping pre-commit reviews clean without manual git add calls. ★ 586repo stars config-watch config-watch makes every mid-session change to settings or skills visible in the session, or blocks it outright. Aimed at installers and npm scripts that rewrite config while the agent works. ★ 586repo stars subagent-spawn-cap subagent-spawn-cap counts subagent spawns per session: asks at 20, asks again every 10, refuses at 60. Puts back the cap Claude dropped in 2.1.224. ★ 586repo stars config-guard config-guard — a PreToolUse hook that stops the agent from rewriting the files holding its own guardrails: settings, hook scripts, plugin manifests. Reads pass. ★ 586repo stars

Project sponsors

Companies that keep this analytics open