Skip to main content
hook

smart-formatting — Claude Code hook: what it does, how to install

Runs formatters like Prettier, Black, gofmt, rustfmt, or php-cs-fixer automatically whenever Claude Code edits a file.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --hook development-tools/smart-formatting
Open on GitHub

What smart-formatting does

The hook attaches to PostToolUse events for Edit and MultiEdit actions. Once Claude Code finishes modifying a file, a shell script inspects $CLAUDE_TOOL_FILE_PATH to detect the extension.

For web formats like JavaScript, TypeScript, JSON, CSS, and HTML, it invokes npx prettier --write. It runs black on Python scripts, gofmt -w on Go code, rustfmt on Rust files, and php-cs-fixer fix on PHP sources.

Each formatting command appends 2>/dev/null || true. If a required CLI tool is missing from your environment, the file simply stays unformatted without throwing errors to Claude.

What it helps with

  • keep JavaScript and TypeScript files formatted with Prettier after edits
  • run Black on Python modules automatically
  • format Go files using gofmt right after modifications
  • apply rustfmt styling to Rust code without leaving the session

Example prompts for the agent

  • Refactor the authentication logic in src/auth.ts
  • Add input validation to routes/users.py
  • Update the data models in internal/db.go

How to install smart-formatting

1claude-code-templates

npx claude-code-templates@latest --hook development-tools/smart-formatting

2Manually: put the files in place

.claude/settings.json

The hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json

Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command

Questions about smart-formatting

How to install smart-formatting in Claude Code?

Run npx claude-code-templates@latest --hook development-tools/smart-formatting in your terminal or paste the hook settings into .claude/settings.json.

What tools does smart-formatting trigger?

It checks file extensions to run Prettier on web assets, Black on Python, gofmt on Go, rustfmt on Rust, and php-cs-fixer on PHP.

What happens if a formatter is not installed?

The command ignores missing tools because calls append 2>/dev/null || true. The session continues without errors, but that file remains unformatted.

Source text

smart-formatting.json · MIT
{
  "description": "Smart code formatting based on file type. Automatically formats code using Prettier, Black, gofmt, rustfmt, and other language-specific formatters.",
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|MultiEdit",
        "hooks": [
          {
            "type": "command",
            "command": "if [[ \"$CLAUDE_TOOL_FILE_PATH\" == *.js || \"$CLAUDE_TOOL_FILE_PATH\" == *.ts || \"$CLAUDE_TOOL_FILE_PATH\" == *.jsx || \"$CLAUDE_TOOL_FILE_PATH\" == *.tsx || \"$CLAUDE_TOOL_FILE_PATH\" == *.json || \"$CLAUDE_TOOL_FILE_PATH\" == *.css || \"$CLAUDE_TOOL_FILE_PATH\" == *.html ]]; then npx prettier --write \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; elif [[ \"$CLAUDE_TOOL_FILE_PATH\" == *.py ]]; then black \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; elif [[ \"$CLAUDE_TOOL_FILE_PATH\" == *.go ]]; then gofmt -w \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; elif [[ \"$CLAUDE_TOOL_FILE_PATH\" == *.rs ]]; then rustfmt \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; elif [[ \"$CLAUDE_TOOL_FILE_PATH\" == *.php ]]; then php-cs-fixer fix \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null || true; fi"
          }
        ]
      }
    ]
  }
}

© davila7 · MIT · davila7/claude-code-templates

Similar hooks

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

From the same set804

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor