| 1 | (options-and-hooks)= |
| 2 | |
| 3 | # Options and hooks |
| 4 | |
| 5 | You shape how tmux sessions, windows, and panes behave by setting *options* — |
| 6 | values like `automatic-rename` or the status-bar format — and by registering |
| 7 | *hooks*, commands that tmux runs when an event fires, such as `session-renamed` |
| 8 | or `after-split-window`. libtmux gives you one consistent Python API to read, |
| 9 | set, and remove both, and it works the same way on every object in the |
| 10 | hierarchy: {class}`~libtmux.Server`, {class}`~libtmux.Session`, |
| 11 | {class}`~libtmux.Window`, and {class}`~libtmux.Pane`. |
| 12 | |
| 13 | Most scripts run happily on tmux's defaults and never open this page — reaching |
| 14 | for options and hooks is entirely optional. Read on only when you need to tweak |
| 15 | how a session behaves or react to something that happens inside it. |
| 16 | |
| 17 | ## Options |
| 18 | |
| 19 | Options are the knobs that control tmux's behavior and appearance, from whether |
| 20 | a window renames itself to how the status line looks. Whatever object you hold, |
| 21 | you read and change its options through the same four methods on |
| 22 | {class}`~libtmux.options.OptionsMixin`. |
| 23 | |
| 24 | ### Getting options |
| 25 | |
| 26 | Use {meth}`~libtmux.options.OptionsMixin.show_options` to get all options: |
| 27 | |
| 28 | ```python |
| 29 | >>> session.show_options() # doctest: +ELLIPSIS |
| 30 | {...} |
| 31 | ``` |
| 32 | |
| 33 | Use {meth}`~libtmux.options.OptionsMixin.show_option` to get a single option: |
| 34 | |
| 35 | ```python |
| 36 | >>> server.show_option('buffer-limit') |
| 37 | 50 |
| 38 | ``` |
| 39 | |
| 40 | ### Setting options |
| 41 | |
| 42 | Use {meth}`~libtmux.options.OptionsMixin.set_option` to set an option. The call |
| 43 | returns the object you set it on, so the change is live the moment it returns — |
| 44 | no {meth}`~libtmux.Window.refresh` needed: |
| 45 | |
| 46 | ```python |
| 47 | >>> window.set_option('automatic-rename', False) # doctest: +ELLIPSIS |
| 48 | Window(@... ...) |
| 49 | |
| 50 | >>> window.show_option('automatic-rename') |
| 51 | False |
| 52 | ``` |
| 53 | |
| 54 | ### Unsetting options |
| 55 | |
| 56 | Once you've overridden an option, you put it back the way tmux shipped it. Use |
| 57 | {meth}`~libtmux.options.OptionsMixin.unset_option` to revert an option to its |
| 58 | default: |
| 59 | |
| 60 | ```python |
| 61 | >>> window.unset_option('automatic-rename') # doctest: +ELLIPSIS |
| 62 | Window(@... ...) |
| 63 | ``` |
| 64 | |
| 65 | ### Option scopes |
| 66 | |
| 67 | By default a call reads or writes the option for the object you're holding — a |
| 68 | window's `set_option` touches that window. But tmux options live at distinct |
| 69 | scopes (server, session, window, pane), and sometimes you want to reach a |
| 70 | different level than the object in hand. Pass the `scope` parameter, drawn from |
| 71 | {class}`~libtmux.constants.OptionScope`, to say which one: |
| 72 | |
| 73 | ```python |
| 74 | >>> from libtmux.constants import OptionScope |
| 75 | |
| 76 | >>> # Get window-scoped options from a session |
| 77 | >>> session.show_options(scope=OptionScope.Window) # doctest: +ELLIPSIS |
| 78 | {...} |
| 79 | ``` |
| 80 | |
| 81 | ### Global options |
| 82 | |
| 83 | Each scope also has a global layer — the fallback tmux uses when an object |
| 84 | hasn't set its own value. Reach it with `global_=True` when you want the |
| 85 | server-wide default rather than what one session or window happens to override: |
| 86 | |
| 87 | ```python |
| 88 | >>> server.show_option('buffer-limit', global_=True) |
| 89 | 50 |
| 90 | ``` |
| 91 | |
| 92 | ## Hooks |
| 93 | |
| 94 | Hooks let you attach tmux commands to events, so something runs automatically |
| 95 | whenever, say, a session is renamed or a window is split. You manage them |
| 96 | through {class}`~libtmux.hooks.HooksMixin`, which mirrors the options API: set, |
| 97 | show, and unset, on any object. |
| 98 | |
| 99 | ### Setting and getting hooks |
| 100 | |
| 101 | Use {meth}`~libtmux.hooks.HooksMixin.set_hook` to set a hook and |
| 102 | {meth}`~libtmux.hooks.HooksMixin.show_hook` to read it back. The hook is |
| 103 | registered with tmux the instant `set_hook` returns — there's no refresh step |
| 104 | before it starts firing: |
| 105 | |
| 106 | ```python |
| 107 | >>> session.set_hook('session-renamed', 'display-message "Session renamed"') # doctest: +ELLIPSIS |
| 108 | Session(...) |
| 109 | |
| 110 | >>> session.show_hook('session-renamed') # doctest: +ELLIPSIS |
| 111 | {0: 'display-message "Session renamed"'} |
| 112 | |
| 113 | >>> session.show_hooks() # doctest: +ELLIPSIS |
| 114 | {...} |
| 115 | ``` |
| 116 | |
| 117 | A single hook reads back as a dict keyed by index rather than a bare string, |
| 118 | because tmux stores hooks as arrays (more on that under indexed hooks, |
| 119 | below). {meth}`show_hook() <libtmux.hooks.HooksMixin.show_hook>` returns a |
| 120 | {class}`~libtmux._internal.sparse_array.SparseArray`, a dict-like type whose |
| 121 | keys are those array indices. |
| 122 | |
| 123 | ### Removing hooks |
| 124 | |
| 125 | Use {meth}`~libtmux.hooks.HooksMixin.unset_hook` to remove a hook: |
| 126 | |
| 127 | ```python |
| 128 | >>> session.unset_hook('session-renamed') # doctest: +ELLIPSIS |
| 129 | Session(...) |
| 130 | ``` |
| 131 | |
| 132 | ### Indexed hooks |
| 133 | |
| 134 | A single event can fire more than one command. tmux models this by indexing |
| 135 | each hook (`session-renamed[0]`, `session-renamed[1]`, …), so you register |
| 136 | several commands against the same event and they all run: |
| 137 | |
| 138 | ```python |
| 139 | >>> session.set_hook('after-split-window[0]', 'display-message "Split 0"') # doctest: +ELLIPSIS |
| 140 | Session(...) |
| 141 | |
| 142 | >>> session.set_hook('after-split-window[1]', 'display-message "Split 1"') # doctest: +ELLIPSIS |
| 143 | Session(...) |
| 144 | |
| 145 | >>> hooks = session.show_hook('after-split-window') |
| 146 | >>> sorted(hooks.keys()) |
| 147 | [0, 1] |
| 148 | ``` |
| 149 | |
| 150 | This is why a hook comes back as a dict-like object: the index *is* part of the |
| 151 | data, and those indices can be sparse. If you set index 0 and index 5 but |
| 152 | nothing in between, tmux keeps the gap, and so does the |
| 153 | {class}`~libtmux._internal.sparse_array.SparseArray` you get back — its keys |
| 154 | stay exactly the indices tmux holds (0 and 5, with no 1–4), rather than |
| 155 | collapsing into a contiguous list. |
| 156 | |
| 157 | ### Bulk hook operations |
| 158 | |
| 159 | When you're setting several indices at once, you don't have to call |
| 160 | {meth}`~libtmux.hooks.HooksMixin.set_hook` per index. Use |
| 161 | {meth}`~libtmux.hooks.HooksMixin.set_hooks` to set multiple indexed hooks in one |
| 162 | call, passing the index-to-command mapping directly: |
| 163 | |
| 164 | ```python |
| 165 | >>> session.set_hooks('window-linked', { |
| 166 | ... 0: 'display-message "Window linked 0"', |
| 167 | ... 1: 'display-message "Window linked 1"', |
| 168 | ... }) # doctest: +ELLIPSIS |
| 169 | Session(...) |
| 170 | |
| 171 | >>> # Clean up |
| 172 | >>> session.unset_hook('after-split-window[0]') # doctest: +ELLIPSIS |
| 173 | Session(...) |
| 174 | >>> session.unset_hook('after-split-window[1]') # doctest: +ELLIPSIS |
| 175 | Session(...) |
| 176 | >>> session.unset_hook('window-linked[0]') # doctest: +ELLIPSIS |
| 177 | Session(...) |
| 178 | >>> session.unset_hook('window-linked[1]') # doctest: +ELLIPSIS |
| 179 | Session(...) |
| 180 | ``` |
| 181 | |
| 182 | ## tmux version compatibility |
| 183 | |
| 184 | Options and hooks need a reasonably recent tmux, and a few specific hooks |
| 185 | arrived later still. The floor for everything on this page is tmux 3.2: |
| 186 | |
| 187 | | Feature | Minimum tmux | |
| 188 | |---------|-------------| |
| 189 | | All options/hooks features | 3.2+ | |
| 190 | | Window/Pane hook scopes (`-w`, `-p`) | 3.2+ | |
| 191 | | `client-active`, `window-resized` hooks | 3.3+ | |
| 192 | | `pane-title-changed` hook | 3.5+ | |
| 193 | |
| 194 | :::{seealso} |
| 195 | - {ref}`api` for the full API reference |
| 196 | - {class}`~libtmux.options.OptionsMixin` for options methods |
| 197 | - {class}`~libtmux.hooks.HooksMixin` for hooks methods |
| 198 | - {class}`~libtmux._internal.sparse_array.SparseArray` for sparse array handling |
| 199 | ::: |