|
| 1 | +(options-and-hooks)= |
| 2 | + |
| 3 | +# Options and Hooks |
| 4 | + |
| 5 | +libtmux provides a unified API for managing tmux options and hooks across all |
| 6 | +object types (Server, Session, Window, Pane). |
| 7 | + |
| 8 | +## Options |
| 9 | + |
| 10 | +tmux options control the behavior and appearance of sessions, windows, and |
| 11 | +panes. libtmux provides a consistent interface through |
| 12 | +{class}`~libtmux.options.OptionsMixin`. |
| 13 | + |
| 14 | +### Getting options |
| 15 | + |
| 16 | +Use {meth}`~libtmux.options.OptionsMixin.show_options` to get all options: |
| 17 | + |
| 18 | +```python |
| 19 | +>>> session.show_options() # doctest: +ELLIPSIS |
| 20 | +{...} |
| 21 | +``` |
| 22 | + |
| 23 | +Use {meth}`~libtmux.options.OptionsMixin.show_option` to get a single option: |
| 24 | + |
| 25 | +```python |
| 26 | +>>> server.show_option('buffer-limit') |
| 27 | +50 |
| 28 | +``` |
| 29 | + |
| 30 | +### Setting options |
| 31 | + |
| 32 | +Use {meth}`~libtmux.options.OptionsMixin.set_option` to set an option: |
| 33 | + |
| 34 | +```python |
| 35 | +>>> window.set_option('automatic-rename', False) # doctest: +ELLIPSIS |
| 36 | +Window(@... ...) |
| 37 | + |
| 38 | +>>> window.show_option('automatic-rename') |
| 39 | +False |
| 40 | +``` |
| 41 | + |
| 42 | +### Unsetting options |
| 43 | + |
| 44 | +Use {meth}`~libtmux.options.OptionsMixin.unset_option` to revert an option to |
| 45 | +its default: |
| 46 | + |
| 47 | +```python |
| 48 | +>>> window.unset_option('automatic-rename') # doctest: +ELLIPSIS |
| 49 | +Window(@... ...) |
| 50 | +``` |
| 51 | + |
| 52 | +### Option scopes |
| 53 | + |
| 54 | +tmux options exist at different scopes. Use the `scope` parameter to specify: |
| 55 | + |
| 56 | +```python |
| 57 | +>>> from libtmux.constants import OptionScope |
| 58 | + |
| 59 | +>>> # Get window-scoped options from a session |
| 60 | +>>> session.show_options(scope=OptionScope.Window) # doctest: +ELLIPSIS |
| 61 | +{...} |
| 62 | +``` |
| 63 | + |
| 64 | +### Global options |
| 65 | + |
| 66 | +Use `global_=True` to work with global options: |
| 67 | + |
| 68 | +```python |
| 69 | +>>> server.show_option('buffer-limit', global_=True) |
| 70 | +50 |
| 71 | +``` |
| 72 | + |
| 73 | +## Hooks |
| 74 | + |
| 75 | +tmux hooks allow you to run commands when specific events occur. libtmux |
| 76 | +provides hook management through {class}`~libtmux.hooks.HooksMixin`. |
| 77 | + |
| 78 | +### Setting and getting hooks |
| 79 | + |
| 80 | +Use {meth}`~libtmux.hooks.HooksMixin.set_hook` to set a hook and |
| 81 | +{meth}`~libtmux.hooks.HooksMixin.show_hook` to get its value: |
| 82 | + |
| 83 | +```python |
| 84 | +>>> session.set_hook('session-renamed', 'display-message "Session renamed"') # doctest: +ELLIPSIS |
| 85 | +Session(...) |
| 86 | + |
| 87 | +>>> session.show_hook('session-renamed') # doctest: +ELLIPSIS |
| 88 | +{0: 'display-message "Session renamed"'} |
| 89 | + |
| 90 | +>>> session.show_hooks() # doctest: +ELLIPSIS |
| 91 | +{...} |
| 92 | +``` |
| 93 | + |
| 94 | +Note that hooks are stored as indexed arrays in tmux, so `show_hook()` returns a |
| 95 | +{class}`~libtmux._internal.sparse_array.SparseArray` (dict-like) with index keys. |
| 96 | + |
| 97 | +### Removing hooks |
| 98 | + |
| 99 | +Use {meth}`~libtmux.hooks.HooksMixin.unset_hook` to remove a hook: |
| 100 | + |
| 101 | +```python |
| 102 | +>>> session.unset_hook('session-renamed') # doctest: +ELLIPSIS |
| 103 | +Session(...) |
| 104 | +``` |
| 105 | + |
| 106 | +### Indexed hooks |
| 107 | + |
| 108 | +tmux hooks support multiple values via indices (e.g., `session-renamed[0]`, |
| 109 | +`session-renamed[1]`). This allows multiple commands to run for the same event: |
| 110 | + |
| 111 | +```python |
| 112 | +>>> session.set_hook('after-split-window[0]', 'display-message "Split 0"') # doctest: +ELLIPSIS |
| 113 | +Session(...) |
| 114 | + |
| 115 | +>>> session.set_hook('after-split-window[1]', 'display-message "Split 1"') # doctest: +ELLIPSIS |
| 116 | +Session(...) |
| 117 | + |
| 118 | +>>> hooks = session.show_hook('after-split-window') |
| 119 | +>>> sorted(hooks.keys()) |
| 120 | +[0, 1] |
| 121 | +``` |
| 122 | + |
| 123 | +The return value is a {class}`~libtmux._internal.sparse_array.SparseArray`, |
| 124 | +which preserves sparse indices (e.g., indices 0 and 5 with no 1-4). |
| 125 | + |
| 126 | +### Bulk hook operations |
| 127 | + |
| 128 | +Use {meth}`~libtmux.hooks.HooksMixin.set_hooks` to set multiple indexed hooks: |
| 129 | + |
| 130 | +```python |
| 131 | +>>> session.set_hooks('window-linked', { |
| 132 | +... 0: 'display-message "Window linked 0"', |
| 133 | +... 1: 'display-message "Window linked 1"', |
| 134 | +... }) # doctest: +ELLIPSIS |
| 135 | +Session(...) |
| 136 | + |
| 137 | +>>> # Clean up |
| 138 | +>>> session.unset_hook('after-split-window[0]') # doctest: +ELLIPSIS |
| 139 | +Session(...) |
| 140 | +>>> session.unset_hook('after-split-window[1]') # doctest: +ELLIPSIS |
| 141 | +Session(...) |
| 142 | +>>> session.unset_hook('window-linked[0]') # doctest: +ELLIPSIS |
| 143 | +Session(...) |
| 144 | +>>> session.unset_hook('window-linked[1]') # doctest: +ELLIPSIS |
| 145 | +Session(...) |
| 146 | +``` |
| 147 | + |
| 148 | +## tmux version compatibility |
| 149 | + |
| 150 | +| Feature | Minimum tmux | |
| 151 | +|---------|-------------| |
| 152 | +| All options/hooks features | 3.2+ | |
| 153 | +| Window/Pane hook scopes (`-w`, `-p`) | 3.2+ | |
| 154 | +| `client-active`, `window-resized` hooks | 3.3+ | |
| 155 | +| `pane-title-changed` hook | 3.5+ | |
| 156 | + |
| 157 | +:::{seealso} |
| 158 | +- {ref}`api` for the full API reference |
| 159 | +- {class}`~libtmux.options.OptionsMixin` for options methods |
| 160 | +- {class}`~libtmux.hooks.HooksMixin` for hooks methods |
| 161 | +- {class}`~libtmux._internal.sparse_array.SparseArray` for sparse array handling |
| 162 | +::: |
0 commit comments