Boris Cherny9/ Use a PostToolUse Hook to Auto-Format Code
Source snapshot: 2026-11-09Independent editorial note
When this pattern helps
Treat this as an extension point with an explicit trigger, inputs, permissions, and failure behavior. Start with a narrow workflow, document prerequisites, and verify the action in a disposable project before making it part of a team routine.
把它当作扩展点来设计:明确触发条件、输入、权限和失败处理。先从窄流程开始,写清前置条件,并在临时项目验证后再纳入团队日常。
Use a PostToolUse hook to format Claude's code. Claude usually generates well-formatted code out of the box, and the hook handles the last 10% to avoid formatting errors in CI later.
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bun run format || true"
}
]
}
]
Original source on X