Free Tool
Claude Code Hooks Generator
Hooks run your own shell commands at set points in a Claude Code session: before a tool runs, after a file is edited, when Claude needs you, or when it finishes. Tick the hooks you want and copy a valid hooks block for your settings file.
Add your own hook (optional)
hooks block
.claude/settings.json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path // empty'); [ -n \"$f\" ] && npx --no-install prettier --write --ignore-unknown \"$f\" >/dev/null 2>&1; exit 0"
}
]
}
],
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.command' | grep -qE 'rm[[:space:]]+-[a-zA-Z]*r[a-zA-Z]*f|rm[[:space:]]+-[a-zA-Z]*f[a-zA-Z]*r' && { echo 'Blocked by hook: rm -rf is not allowed' >&2; exit 2; } || exit 0"
}
]
},
{
"matcher": "Read|Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path // empty' | grep -qE '(^|/)\\.env($|\\.)|\\.pem$|id_rsa' && { echo 'Blocked by hook: secret files are off limits' >&2; exit 2; } || exit 0"
}
]
}
]
}
}
These hooks need: jq, Node.js, jq.
How to add the hooks
- Open (or create)
.claude/settings.json. - If the file is empty, paste the whole output. If it already has settings, add the
"hooks"key next to them. - Restart Claude Code or run
/hooksto check they loaded. - Test each hook once, for example ask Claude to run
rm -rf tmpin a scratch folder and confirm it is blocked.
Frequently asked questions
In a settings file under the "hooks" key: .claude/settings.json to share with your team, .claude/settings.local.json for just you in this project, or ~/.claude/settings.json for all projects. If the file already has other settings, merge the hooks block into it rather than replacing the file.
A command hook gets JSON about the event on standard input, for example tool_name and tool_input for PreToolUse. Exit code 0 means carry on. Exit code 2 blocks the action and sends your standard error text back to Claude as the reason. Other exit codes show a non-blocking error.
For tool events it is matched against the tool name. Use an exact name like Bash, a list like Edit|Write, or * to match every tool. Characters other than letters, digits, _, -, spaces, commas and | make it a regular expression, for example mcp__github__.*.
Some do. Most examples read the event JSON with jq, the auto-format hook runs Prettier through npx, and the desktop notification uses osascript on macOS or notify-send on Linux. Install what a hook needs, or edit the command to use your own tools.
Common ones are SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Notification, Stop, SubagentStop, PreCompact and SessionEnd. The full list in the docs is longer and includes events for permissions, tasks, worktrees and compaction.
Hooks run shell commands on your computer with your permissions, automatically. Only add hooks you understand, keep scripts in your repo where you can review them, and test each one in a throwaway project first.