{
  "id": 23754232,
  "title": "How to use Claude Code hooks: four you can paste today",
  "url": "https://urgent.news/2026/12/06/how-to-use-claude-code-hooks-four-you-can-paste-today",
  "topic": "ai",
  "section": "AI",
  "published": "2026-12-06T02:39:25.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/attilaalbert/how-to-use-claude-code-hooks-four-you-can-paste-today-2o4c"
  },
  "original_language": null,
  "account": "Claude Code hooks are shell commands that run automatically at specific points during the code editing process, providing a way to customize the behavior of Claude Code. These hooks are defined in a settings file, typically located at ~/.claude/settings.json or .claude/settings.json, and are executed on various events, such as before or after a tool runs, when Claude needs attention, or when the session starts.\n\nThere are four hooks that can be easily implemented today:\n\n1. Notification hook: This hook displays a notification when Claude Code needs your attention. To enable it, add a \"Notification\" entry to the hooks block in the settings file. On macOS, use the osascript command to display a notification, while on Linux, use the notify-send command.\n\n2. Formatter hook: This hook formats every file Claude edits using a specified formatter, such as Prettier. In the .claude/settings.json file, add a \"PostToolUse\" entry with a matcher of \"Edit|Write\" and a command that uses the jq tool to extract the file path from the JSON input and then runs the formatter on that file.\n\n3. Protection hook: This hook prevents Claude Code from editing certain files, like .env files or package-lock.json. Create a shell script, make it executable, and then add a \"PreToolUse\" entry to the hooks block with the script's path. The script checks the file path against a list of protected patterns and exits with an error if a match is found.\n\n4. Dangerous command block hook: This hook blocks specific dangerous shell commands, like dropping tables in SQL or using git with --force. Create another shell script, test it to ensure it works correctly, and then add a \"PreToolUse\" entry with the script's path and a matcher for the specific tool (e.g., \"Bash\").\n\nTo use these hooks, simply add the appropriate sections to the hooks block in the settings file and save the file. Claude Code will automatically run the hooks on the specified events, allowing you to customize its behavior to suit your needs. Remember to check the JSON syntax if your hooks are not firing and ensure that the scripts are executable and the command names are spelled correctly.",
  "summary": "Instructions in CLAUDE.md are requests. Claude usually follows them, but \"always run Prettier after editing\" is still something the model has to remember. Hooks are different: they're shell commands that Claude Code itself runs at fixed points, every time, whatever the model decides. If something must happen (or must never happen), a hook is the right tool. This post explains how hooks work in a…",
  "summary_source": "publisher",
  "key_points": [
    "Notification hook displays Claude Code attention alerts",
    "Formatter hook automatically formats edited Claude files",
    "Protection hook blocks editing of critical files"
  ],
  "editors_take": "Developers can use these shell commands to automate file formatting, prevent accidental edits to sensitive files, and block dangerous commands during the coding process.",
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "outlets_capped": false,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}