Skip to main content
tmux hooks allow you to automatically execute commands when specific events occur. Hooks enable powerful automation and customization of tmux behavior.

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:
All hooks are array options with scope-specific visibility.

Setting Hooks

Use set-hook or set-option to configure hooks:
Flags:
  • -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

Use if-shell for conditional hook execution:

Hook Debugging

View configured hooks:
Test hooks by running them manually:

Removing Hooks

Unset hooks to remove them:

Hook Scope

Hooks can be set at different scopes:
More specific hooks override global ones.

Common Use Cases

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.
Hooks that fail or produce errors can disrupt tmux operation. Always test hooks before deploying them in production configurations.