用一段 PostToolUse hook,讓 Claude Code 每次 Edit/Write 完檔案就自動跑 formatter(Prettier/Black/gofmt)+linter,把「格式不一致」這件雜事從你的 code review 裡永久移除。
# 目標:Claude Code 每次編輯檔案後,自動格式化+lint
# 放到 .claude/settings.json(專案層級)或 ~/.claude/settings.json(全域)。
# 機制:PostToolUse hook 在 Edit / Write / MultiEdit 完成「之後」觸發,
# hook 從 stdin 收到 JSON,裡面 tool_input.file_path 是剛被改的檔案。
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write|MultiEdit",
"hooks": [
{
"type": "command",
"command": "bash {{PROJECT_DIR}}/.claude/hooks/format-after-edit.sh"
}
]
}
]
}
}
# ---- .claude/hooks/format-after-edit.sh ----
# chmod +x 後使用。依副檔名挑工具,找不到工具就安靜跳過(不要讓 hook 把流程弄爆)。
#!/usr/bin/env bash
set -euo pipefail
# 從 stdin 的 JSON 取出剛被編輯的檔案路徑
file=$(jq -r '.tool_input.file_path // empty')
[ -z "$file" ] && exit 0
[ ! -f "$file" ] && exit 0
case "$file" in
*.ts|*.tsx|*.js|*.jsx|*.mjs|*.json|*.css|*.md)
# JS/TS 系:先 format 再 lint --fix(工具不存在就略過)
command -v {{PRETTIER_CMD}} >/dev/null 2>&1 && {{PRETTIER_CMD}} --write "$file" >/dev/null 2>&1 || true
command -v {{ESLINT_CMD}} >/dev/null 2>&1 && {{ESLINT_CMD}} --fix "$file" >/dev/null 2>&1 || true
;;
*.py)
command -v {{PY_FORMATTER}} >/dev/null 2>&1 && {{PY_FORMATTER}} "$file" >/dev/null 2>&1 || true # 例:black / ruff format
command -v ruff >/dev/null 2>&1 && ruff check --fix "$file" >/dev/null 2>&1 || true
;;
*.go)
command -v gofmt >/dev/null 2>&1 && gofmt -w "$file" >/dev/null 2>&1 || true
;;
*.rs)
command -v rustfmt >/dev/null 2>&1 && rustfmt "$file" >/dev/null 2>&1 || true
;;
esac
exit 0
# 注意:PostToolUse 在工具已執行「之後」才跑,無法阻止編輯;
# 它的用途是「事後自動修好」,不是「事前攔截」。要攔截請用 PreToolUse。不用離開網站,直接看這組 prompt 跑出來長怎樣(AI 即時生成,扣 1 點)。
不只複製貼上 — 下載後放到 ~/.claude/skills/hook-auto-format-lint-after-edit/SKILL.md,之後每個 session 自動可用(觸發時自動載入)。
mkdir -p ~/.claude/skills/hook-auto-format-lint-after-edit && mv ~/Downloads/SKILL.md ~/.claude/skills/hook-auto-format-lint-after-edit/SKILL.mdNew-Item -ItemType Directory -Force "$env:USERPROFILE\.claude\skills\hook-auto-format-lint-after-edit" | Out-Null; Move-Item "$env:USERPROFILE\Downloads\SKILL.md" "$env:USERPROFILE\.claude\skills\hook-auto-format-lint-after-edit\SKILL.md"## 這是什麼/解決什麼痛點 讓 AI 寫 code 最惱人的小事之一,就是「排版風格不一致」:它有時用 2 空白、有時 4 空白,import 順序、引號、結尾分號每次都不太一樣。結果你的 code review 一半時間在看 whitespace diff,git blame 也被無意義的格式變動洗版。這個 hook 食譜把「格式化+lint --fix」綁進 Claude Code 的生命週期:只要 AI 用 Edit / Write / MultiEdit 改了檔案,PostToolUse hook 就立刻對那個檔案跑 formatter 與 linter,等你看到 diff 時它「已經對齊」了。 ## 為什麼這來源值得用 Claude Code 的 hooks 是「由 harness 執行的確定性 shell 指令」,不是請 AI「記得格式化」——AI 會忘、hook 不會。本食譜的 PostToolUse 結構(`matcher` 鎖定 `Edit|Write`、`hooks[].type=command`、由 stdin 收 JSON 取 `tool_input.file_path`)直接取自 rohitg00/awesome-claude-code-toolkit 裡實際運作的 `auto-stage` PostToolUse hook;該 hook 原本是「編輯後自動 git stage」,我把同一套觸發機制改寫成「編輯後自動 format+lint」,是對同一個官方機制的忠實延伸,不是憑空杜撰。 ## 怎麼用(步驟) 1. 把上面的 JSON 併進 `.claude/settings.json`(只想對自己生效就放 `~/.claude/settings.json`)。 2. 建立 `.claude/hooks/format-after-edit.sh`,貼上腳本,`chmod +x` 給執行權限。 3. 確認機器上有 `jq`(hook 用它解析 stdin JSON)與你要的工具(prettier / eslint / black / ruff / gofmt …)。 4. 把 `{{PRETTIER_CMD}}`、`{{ESLINT_CMD}}`、`{{PY_FORMATTER}}` 換成你的實際指令(例:`npx prettier`、`npx eslint`、`black`)。 5. 在 Claude Code 裡用 `/hooks` 確認載入,然後叫 AI 隨便改一個檔案,觀察是否自動被格式化。 ## 要點 - **找不到工具就靜默跳過**(`|| true`):別讓 hook 因為某台機器沒裝某工具而中斷整個編輯流程。 - **只針對單一檔案跑**:用 `file_path` 對剛改的檔案 format,而不是全 repo,速度快、不污染其他檔案。 - **PostToolUse 無法阻止編輯**:它在工具執行「之後」才跑,定位是「事後自動修好」。要在危險動作發生前攔截,請改用 PreToolUse(見本站另一篇危險指令 guardrail 食譜)。 ## 何時用 團隊有共用 formatter/linter 設定、又常讓 Claude Code 大量改檔時最有感;個人專案想維持乾淨 git 歷史、讓每個 diff 都只有「真正的邏輯變動」也很適合。若你的 formatter 很慢(大型 mono-repo 全量跑),記得把它鎖在「單檔」而非全專案,避免每次編輯都卡住。 📎 來源:rohitg00/awesome-claude-code-toolkit(作者 Rohit Ghumare,Apache-2.0 授權)— 本篇 PostToolUse hook 結構取自其 auto-stage hook,繁中改寫並從「自動 git stage」延伸為「自動格式化+lint」,原始內容見上方連結。
[PROJECT_DIR]專案根目錄路徑,hook 腳本所在位置(也可用相對路徑 .claude/hooks/...)
[PRETTIER_CMD]Prettier 執行指令,例如 npx prettier 或 pnpm prettier
[ESLINT_CMD]ESLint 執行指令,例如 npx eslint 或 pnpm eslint
[PY_FORMATTER]Python formatter 指令,例如 black 或 ruff format
填下面的欄位,上方 prompt 會即時替換 [方括號] 內容。填好後按「複製組好的 prompt」直接丟進工具。
# 目標:Claude Code 每次編輯檔案後,自動格式化+lint
# 放到 .claude/settings.json(專案層級)或 ~/.claude/settings.json(全域)。
# 機制:PostToolUse hook 在 Edit / Write / MultiEdit 完成「之後」觸發,
# hook 從 stdin 收到 JSON,裡面 tool_input.file_path 是剛被改的檔案。
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write|MultiEdit",
"hooks": [
{
"type": "command",
"command": "bash {{PROJECT_DIR}}/.claude/hooks/format-after-edit.sh"
}
]
}
]
}
}
# ---- .claude/hooks/format-after-edit.sh ----
# chmod +x 後使用。依副檔名挑工具,找不到工具就安靜跳過(不要讓 hook 把流程弄爆)。
#!/usr/bin/env bash
set -euo pipefail
# 從 stdin 的 JSON 取出剛被編輯的檔案路徑
file=$(jq -r '.tool_input.file_path // empty')
[ -z "$file" ] && exit 0
[ ! -f "$file" ] && exit 0
case "$file" in
*.ts|*.tsx|*.js|*.jsx|*.mjs|*.json|*.css|*.md)
# JS/TS 系:先 format 再 lint --fix(工具不存在就略過)
command -v {{PRETTIER_CMD}} >/dev/null 2>&1 && {{PRETTIER_CMD}} --write "$file" >/dev/null 2>&1 || true
command -v {{ESLINT_CMD}} >/dev/null 2>&1 && {{ESLINT_CMD}} --fix "$file" >/dev/null 2>&1 || true
;;
*.py)
command -v {{PY_FORMATTER}} >/dev/null 2>&1 && {{PY_FORMATTER}} "$file" >/dev/null 2>&1 || true # 例:black / ruff format
command -v ruff >/dev/null 2>&1 && ruff check --fix "$file" >/dev/null 2>&1 || true
;;
*.go)
command -v gofmt >/dev/null 2>&1 && gofmt -w "$file" >/dev/null 2>&1 || true
;;
*.rs)
command -v rustfmt >/dev/null 2>&1 && rustfmt "$file" >/dev/null 2>&1 || true
;;
esac
exit 0
# 注意:PostToolUse 在工具已執行「之後」才跑,無法阻止編輯;
# 它的用途是「事後自動修好」,不是「事前攔截」。要攔截請用 PreToolUse。這組 prompt 專為 Claude Code 設計。把 prompt 內 4 個方括號 [變數] 換成你自己的內容,貼進 Claude Code 執行即可。難度中等,照變數說明填好後即可上手。
完整 prompt 免費開放閱讀,不用註冊;登入後可一鍵複製、收藏與留言。
prompt 文字本身你可自由使用與修改。但 AI 生成物(圖/音樂/影片/文字)的商用授權,取決於你在 Claude Code 使用的方案與其官方服務條款,請以該工具的授權規範為準。
Studio engineer 視角拆解 Suno 致命弱點(油炸 vocals、高頻 artifact)+ 4 步驟 DAW workflow + Suno Studio 修音 prompt
提案產生器 / 會議處理器 / 內容再利用 / 週五回顧 / 收工 reset — 試了 40 個只有這 5 個沒被丟掉、各省 30+ 分鐘 / 次。
適合:部落格、Medium、Notion 公開頁、Substack — 任何支援 iframe / HTML 嵌入的地方。對方點「看完整」會回到本站、是 prompt 庫的免費 backlink。
<iframe src="https://prompt.luvai.net/embed/claude-code-hook-auto-format-lint-after-edit" width="100%" height="380" frameborder="0" style="border:1px solid #e0dcd0;border-radius:4px;" loading="lazy" title="PromptCraft Embed"></iframe>
把方括號 [ ] 內的變數換成你的內容,丟進 Claude Code。
六個月在 Claude / GPT-4 / Gemini 上用人工 rater A/B 測 200+ prompt 後寫的。包含 persona+constraint stacking / anti-example / role reversal QA / cognitive scaffold / emotional priming / uncertainty CoT / steelman first。
一年的 role-play system prompt + 14-step framework 後總結:真正改變品質的是 5 個單行 prompt。沒有 role、沒有 markdown、沒有「you are an expert」。