brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tmux set-hook runs a command after an event such as a split

Tmux
~/tips/tip-tmux-set-hook-after-split-window

set-hook -g after-split-window "select-layout even-vertical"

Runs a tmux command automatically whenever an event such as a pane split happens.

Key bindings only react when I press something. A hook reacts to what tmux itself does, which opens a few small automations I would otherwise do by hand.

set-hook attaches a tmux command to an event. The manual has this example, which applies the even-vertical layout after every split:

set-hook -g after-split-window "select-layout even-vertical"

Put it in ~/.tmux.conf and each new split is followed by that layout. Besides the after hooks, which run after a command completes, the manual lists events such as client-attached, pane-exited and pane-died. The last one fires when a program exits but remain-on-exit keeps the pane open.

Another example from the manual writes a line to a log whenever a tmux command fails:

set-hook -g command-error "run-shell \"echo 'a tmux command failed' >>/tmp/log\""

Show what is set with tmux show-hooks -g. To remove a hook again, use -u:

set-hook -gu after-split-window

The hook command runs every time the event fires, so something heavy in a hook makes tmux feel slow. The layout example also rearranges all panes after each split, which undoes a layout you arranged by hand. And pane-focus-in only fires when the focus-events option is on.

Further reading: tmux manual

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Tmux tips tmux new -d -s report -x 200 -y 50 'top' tmux wait-for build-done set -g allow-passthrough on tmux kill-server Ctrl-b [ All Tmux tips (125) All topics

Link