Claude Tips mascot
Claude Tips & Tricks
Claude Code advanced

Automate Repetitive Tasks with Claude Code Hooks

Configure hooks in .claude/settings.json to automatically run shell commands in response to Claude Code events like file saves or tool calls.

Claude Code hooks are shell commands that run automatically in response to specific events. They let you build automated workflows without manual intervention.

Configure hooks via the interactive command:

/hooks

This opens an interface where you can define hooks for various events.

Or configure them directly in .claude/settings.json:

{
  "hooks": {
    "afterWrite": [
      {
        "pattern": "*.py",
        "command": "ruff check --fix $FILE"
      }
    ],
    "afterWrite": [
      {
        "pattern": "*.ts",
        "command": "npx prettier --write $FILE"
      }
    ]
  }
}

Common hook use cases:

  • Auto-format on save: Run Prettier, Black, or ruff after Claude writes a file
  • Auto-lint: Run ESLint or mypy after code changes
  • Auto-test: Run related tests after modifying source files
  • Notifications: Send a Slack message or desktop notification when a long task completes

Hook events include:

  • afterWrite - after Claude creates or modifies a file
  • afterToolUse - after Claude uses any tool
  • preToolUse - before Claude uses a tool (can block execution)

Combine with skills and slash commands: Hooks handle the “automatic reactions” while slash commands handle the “explicit actions” and skills handle the “conventions.”

Paste into Claude Code
Set up Claude Code hooks in .claude/settings.json to automatically run the linter after any file edit and run tests after changes to test files.