title: "Pre-Commit Lint Hook for Claude Code" tested_with: claude-code: "1.0.x" last_updated: 2026-03-21 status: proven difficulty: intermediate prerequisites: [04-hooks-and-commands]

Pre-Commit Lint Hook

What This Config Does

Automatically runs your project's linter after Claude Code edits a file. If the lint fails, the agent sees the errors and can fix them in the same session.

The Config

Add to your .claude/settings.json (project-level):

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hook": "npx eslint --fix $(echo $TOOL_OUTPUT | jq -r '.filePath // empty') 2>&1 || true"
      }
    ]
  }
}
# Adapt this by:
# - Replace `npx eslint --fix` with your project's linter command
# - For Python: `ruff check --fix $FILE`
# - For Go: `gofmt -w $FILE`
# - For Rust: `cargo fmt -- $FILE`
# - The `|| true` prevents the hook from blocking the agent on lint errors

Where to Put It

.claude/settings.json in your project root for project-level, or ~/.claude/settings.json for global.

How to Verify It Works

Ask Claude Code to create a file with intentional style issues (e.g., missing semicolons in JS). After the file is written, the hook should auto-fix and you'll see the lint output in the session.

Notes

  • This runs after EVERY file edit, which adds a small delay per edit. If your linter is slow, consider running it only on specific file types.
  • The hook output is visible to the agent, so it can react to lint failures.
  • See Module 04: Hooks & Commands for more hook patterns.