GC-201c · Module 3
Hooks for Workflow Enforcement
3 min read
Hooks are lifecycle interceptors that run custom logic at specific points in Gemini CLI's execution. The most valuable hooks fire after tool execution — specifically after file writes. A post-write hook that runs your linter and formatter ensures every file Gemini touches meets your standards automatically. No separate cleanup step. No "Gemini wrote code that does not match our style" complaints. The hook enforces it silently, consistently, on every write.
Hook configuration lives in settings.json. Each hook specifies an event (when to fire), a command (what to run), and optional conditions (which tools or file patterns trigger it). Hooks execute synchronously — the Gemini CLI session waits for the hook to complete before proceeding. This means a slow hook slows down your entire session. Keep hooks fast. Linting a single file: fast. Running the full test suite: too slow for a hook.
{
"hooks": {
"post-write": {
"command": "npx prettier --write {{file}}",
"description": "Auto-format files after Gemini writes them"
},
"post-edit": {
"command": "npx eslint --fix {{file}}",
"description": "Auto-lint files after Gemini edits them"
}
}
}
Do This
- Configure post-write hooks for formatting and linting — highest value, lowest effort
- Keep hooks fast — single-file operations, not full-project builds
- Use hooks for consistent enforcement that should never be skipped
Avoid This
- Run the full test suite as a hook — it is too slow and blocks the session
- Add so many hooks that every file write takes 10 seconds
- Use hooks for complex logic that belongs in CI/CD pipelines