Monitor Smart Formatting
Boost productivity using this smart, code, formatting, based. Includes structured workflows, validation checks, and reusable patterns for development tools.
Monitor Smart Formatting
Automatically formats code using the appropriate language-specific formatter after every file modification, supporting Prettier, Black, gofmt, rustfmt, and more.
When to Use This Hook
Attach this hook when you need to:
- Automatically format code in the correct language-specific style after every file edit without remembering which formatter to use
- Maintain consistent formatting across a polyglot codebase where different languages require different formatting tools
- Eliminate formatting discussions in code reviews by ensuring all code is formatted before it reaches the review stage
Consider alternatives when:
- Your project uses a single language and you already have format-on-save configured in your editor
- You need custom formatting rules that differ from the standard formatter defaults and require extensive configuration
Quick Start
Configuration
name: monitor-smart-formatting type: hook trigger: PostToolUse category: development-tools
Example Trigger
# Claude edits src/utils/parser.ts # Hook fires: # Smart Formatting: Formatting src/utils/parser.ts with Prettier...
Example Output
Smart Formatting: src/utils/parser.ts
Detected Language: TypeScript
Formatter: Prettier
Config: .prettierrc found in project root
Changes Applied: 3 formatting adjustments
- Line 14: trailing comma added
- Line 22: indent adjusted from 4 to 2 spaces
- Line 35: line length wrapped at 80 characters
Status: Formatted successfully
Core Concepts
Formatter Selection Overview
| Aspect | Details |
|---|---|
| JS/TS/CSS/HTML/JSON | Prettier via npx prettier --write |
| Python | Black via black for consistent Python formatting |
| Go | gofmt via gofmt -w (standard Go formatting) |
| Rust | rustfmt via rustfmt for Rust code |
| PHP | php-cs-fixer via php-cs-fixer fix for PHP code |
| Trigger | PostToolUse on Edit and MultiEdit operations |
Smart Detection Workflow
PostToolUse (Edit/MultiEdit)
|
v
[Read $CLAUDE_TOOL_FILE_PATH extension]
|
+--- .js/.ts/.jsx/.tsx/.json/.css/.html --> [Prettier]
|
+--- .py --> [Black]
|
+--- .go --> [gofmt]
|
+--- .rs --> [rustfmt]
|
+--- .php --> [php-cs-fixer]
|
+--- Other --> [Skip formatting]
|
v
[Run formatter with --write/in-place flag]
|
v
[Report: changes applied or already formatted]
|
v
[Exit 0 (non-blocking, errors suppressed)]
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
matcher | string | Edit|MultiEdit | Tool types that trigger formatting |
prettier_args | string | --write | Additional arguments for Prettier execution |
black_args | string | (none) | Additional arguments for Black Python formatter |
gofmt_args | string | -w | Additional arguments for gofmt |
suppress_errors | boolean | true | Whether to suppress formatter errors and continue silently |
Best Practices
-
Install all required formatters before enabling - The hook suppresses errors silently by default, which means a missing formatter will simply skip formatting without notification. Install Prettier, Black, gofmt, and other formatters your project needs before activating the hook.
-
Configure Prettier with a project-level config - Create a
.prettierrcfile in your project root to define formatting rules. Without a config file, Prettier uses defaults that may not match your team's preferences. Include the config in version control for consistency. -
Use Black with default settings for Python - Black is intentionally opinionated and provides minimal configuration options. Embrace its defaults to eliminate formatting debates. If you need Black-incompatible formatting, consider switching to autopep8 which offers more flexibility.
-
Let gofmt handle Go formatting exclusively - The Go community standardizes on gofmt output. Do not add custom formatting rules for Go files. The hook correctly uses
gofmt -wwhich formats files in-place following the Go standard. -
Test formatter compatibility with your linter - Formatters and linters can conflict when they have overlapping rules with different preferences. Run both tools on the same file and verify they converge on the same output. For JavaScript, use
eslint-config-prettierto resolve conflicts.
Common Issues
-
Prettier reformats files that were already correctly formatted - This usually indicates a mismatch between Prettier's config and the formatting Claude Code produces. Ensure your
.prettierrcsettings match the formatting style Claude Code uses, or accept that Prettier will normalize to its configured style. -
Black is not installed and Python files are not formatted - The hook suppresses errors with
|| true, so missing Black installation fails silently. Install Black withpip install blackand verify it is available in your PATH before relying on auto-formatting for Python files. -
gofmt modifies files that cause compilation errors - gofmt only handles formatting, not syntax correctness. If a Go file has syntax errors, gofmt will either fail silently or make formatting changes that highlight the underlying syntax issue. Fix compilation errors before expecting clean formatting.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Pre-Commit Security Scanner
Pre-commit hook that scans staged files for hardcoded secrets, API keys, passwords, and sensitive data patterns before allowing commits.
Agents Md Watcher
Streamline your workflow with this automatically, loads, agents, configuration. Includes structured workflows, validation checks, and reusable patterns for automation.
Automated Build Inspector
Boost productivity using this automatically, trigger, build, processes. Includes structured workflows, validation checks, and reusable patterns for automation.