Hooks

Hooks let you run custom shell commands at specific points in the Code CLI lifecycle. They are configured in codecli.json under the hooks section.

Hook Types

TypeTriggerUse Case
pre_tool_useBefore a tool executesValidate commands, block dangerous operations
post_tool_useAfter a tool executesAuto-format, run linters, notify on changes
session_startWhen a session beginsLoad environment, set up context
stop_hookWhen a session endsCleanup, generate reports, commit changes

Configuration

codecli.json
json
"hooks": {
"pre_tool_use": [{
"matcher": "bash(git push*)",
"command": "echo {"$"}1 | grep -v main || exit 1",
"timeout": 5000
}],
"post_tool_use": [{
"matcher": "write(*)",
"command": "npx prettier --write {"$"}1",
"timeout": 5000
}, {
"matcher": "write(src/**/*.ts)",
"command": "npx eslint {"$"}1",
"timeout": 10000
}],
"session_start": [{
"command": "echo {"\""}Session started at {"$( "}date{" ) "}{ "\""} >> ~/.codecli/sessions.log",
"timeout": 1000
}]
}

Pattern Matching

PatternMatches
*All tool calls
bash(*)All bash commands
bash(git commit:*)Git commit commands
write(src/**/*.ts)Writes to TypeScript files in src/
edit(*)All edit operations

Captured groups from patterns are available as $1, $2, etc.

PreviousSkillsNextPermissions