Understanding Hooks
Hooks are commands that run automatically in response to tmux events. From options-table.c:1552-1620, tmux defines hooks as session-level options that are arrays of commands.Hook Definition
The hook macros from options-table.c:236-261 define three types:Setting Hooks
Useset-hook or set-option to configure hooks:
-a: Append to hook (run after existing commands)-g: Set globally-t: Target specific session/window/pane-u: Unset/remove hook-R: Run the hook immediately
Available Hooks
From options-table.c:1552-1620, here are all available hooks:Command Hooks
These hooks run after specific commands:Alert Hooks
These hooks run when alerts are triggered:Client Hooks
These hooks run for client events:Command Error Hook
Pane Hooks
These hooks run for pane events:Session Hooks
These hooks run for session events:Window Hooks
These hooks run for window events:Hook Execution Context
Hooks execute with access to format variables relevant to the event:Hook Examples
Automatic Logging
Status Line Updates
Theme Switching
Session Management
Pane Lifecycle
Window Monitoring
Multiple Hook Commands
Hooks can run multiple commands using; or by setting multiple array entries:
Appending to Hooks
Use-a to add commands without replacing existing hooks:
Conditional Hooks
Useif-shell for conditional hook execution:
Hook Debugging
View configured hooks:Removing Hooks
Unset hooks to remove them:Hook Scope
Hooks can be set at different scopes:Common Use Cases
Auto-save tmux session
Auto-save tmux session
Automatic pane naming
Automatic pane naming
Notification system
Notification system
Session initialization
Session initialization
Dynamic layouts
Dynamic layouts
Hook Best Practices
Keep hooks simple
Hooks should execute quickly. For complex operations, call external scripts with
run-shell.Use format variables
Leverage format variables to access context:
#{session_name}, #{window_index}, etc.Test thoroughly
Hooks run automatically and can cause issues if misconfigured. Test with
-R flag before relying on them.Document your hooks
Add comments explaining what each hook does and why it’s needed.