Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Hooks: Setting Up Automatic Actions on Certain Events

Hooks: Setting Up Automatic Actions on Certain Events

~7 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026

Block 4 closes with a tool that doesn't give Claude Code more knowledge or access (like MCP in the last chapter), but automation: hooks automatically trigger a defined action on certain events during the workflow - without you having to remember it manually every time.

What a hook is

A hook connects an event (for example: "a file was changed") with an action (for example: "run the tests"). Instead of you or Claude Code having to remember to run the tests after every change, a hook takes care of it automatically - reliably, on every matching occasion, with no chance of forgetting it.

An example for the recipe planner

In the recipe planner, one obvious hook suggests itself: after every change to a file in the src/ folder, the tests should run automatically, so it's immediately obvious if a change broke something existing - instead of noticing it only at the end of the session.

Setting up hooks

Hooks are configured per project in the .claude/settings.json configuration file. There you define which event to react to and which command should then run automatically:

rezept-planer/.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "npm test"
          }
        ]
      }
    ]
  }
}

This configuration makes sure that npm test runs automatically after every file change made through the Edit or Write tools. If a test then fails, it's noticed right away - immediately after the change that caused it, instead of at some point later.

More typical use cases

  • Automatically run a code formatter after a file has been changed.
  • Write an extra log line before running a potentially risky Bash command.
  • Automatically show a reminder to commit open changes at the end of a session (more on git in chapter 24).

Achtung: A hook runs automatically, without you specifically approving it each time - that's the whole point of automation, but also its biggest risk. A hook command that accidentally does the wrong thing (for example deleting a file instead of just testing it) will keep doing that on every matching occasion, with no confirmation asked. So test a new hook command manually in the terminal first, before setting it up as a hook.

Hooks vs. custom slash commands

The difference from the custom slash commands in chapter 17: you have to actively invoke a slash command, while a hook runs by itself as soon as the matching event occurs. Both tools automate recurring tasks - slash commands for things you want to trigger deliberately from time to time, hooks for things that should really happen every single time.

That wraps up block 4: slash commands, skills, plan mode, subagents, MCP, and hooks. Block 5 turns to another important topic: how Claude Code retains context over time - within a session and beyond it.