set-hook
Set (or unset) a hook that runs a tmux command automatically when an event occurs, such as a session being created or a window being linked. Flags mirror set-option.
How set-hook works
set-hook runs a tmux command automatically when something happens. Hooks exist for events such as client-attached, client-detached, session-created, session-closed, pane-exited, and pane-died, plus an after- hook for most commands, such as after-new-window or after-split-window.
It takes the same flags as set-option. -g sets a global hook, -a appends another command to a hook instead of replacing it, and -u removes it. -R runs a hook right away, which is handy for testing. List hooks with show-hooks -g.
Hooks run tmux commands. To run a shell script, wrap it in run-shell, for example set-hook -g session-created 'run-shell ~/bin/on-session.sh'.
Synopsis
tmux set-hook [-agpRuw] [-t target-pane] hook [command]Options
| Flag | Description |
|---|---|
| -g | Set a global hook |
| -a | Append so the hook runs multiple commands |
| -u | Unset the hook |
| -R | Run the hook immediately |
| -t target-pane | Target pane/session for the hook |
Examples
Auto-apply the tiled layout to new windows
tmux set-hook -g after-new-window 'select-layout tiled'Run a script whenever a session is created
tmux set-hook -g session-created 'run-shell ~/on-session.sh'In .tmux.conf: show a message whenever you attach to a session.
set-hook -g client-attached 'display "Welcome back"'In .tmux.conf: rebalance panes side by side every time you split.
set-hook -ga after-split-window 'select-layout even-horizontal'Default Keybinding
set-hook has no default key. Run it from a shell as tmux set-hook, type it at the command prompt (Ctrl+b then :), or give it a key with bind-key.
Common Mistakes
- Without -a, setting a hook replaces whatever was there, including hooks added by plugins.
- A hook that runs the command it reacts to can loop. For example, an after-new-window hook that runs new-window keeps creating windows.
- pane-died only fires when remain-on-exit is on. Without it, a finished pane simply closes and only pane-exited fires.
Pro Tips
- •List active hooks with show-hooks; many options also fire a *-changed hook
- •Use -a to attach several commands to one event