| 1 | (format-tokens)= |
| 2 | |
| 3 | # Format-token fields |
| 4 | |
| 5 | When you work with a libtmux object — {class}`~libtmux.Server`, |
| 6 | {class}`~libtmux.Session`, {class}`~libtmux.Window`, |
| 7 | {class}`~libtmux.Pane`, or {class}`~libtmux.Client` — you get a flat set |
| 8 | of typed string attributes that report the object's current state |
| 9 | straight from tmux, mirroring tmux's built-in |
| 10 | [FORMATS](https://man.openbsd.org/tmux.1#FORMATS) tokens (`pane_id`, |
| 11 | `window_zoomed_flag`, `session_name`, etc.). This is why a single |
| 12 | {class}`~libtmux.Pane` can hand you |
| 13 | {attr}`pane.pane_id <libtmux.Pane.pane_id>`, |
| 14 | {attr}`pane.window_id <libtmux.Pane.window_id>`, and |
| 15 | {attr}`pane.session_id <libtmux.Pane.session_id>` without you writing a raw tmux |
| 16 | command. |
| 17 | |
| 18 | Most of the time you just read these attributes and move on. Not every |
| 19 | field holds a value on every object, though: the object's type and your |
| 20 | tmux version decide which fields are populated and which stay `None`. |
| 21 | |
| 22 | Which fields hold a value comes down to two gates: |
| 23 | |
| 24 | 1. **Scope** — which kind of tmux object can provide the token. A `pane_*` |
| 25 | token needs pane context, a `session_*` token needs session context, and |
| 26 | so on. |
| 27 | 2. **Version** — which tmux release first registered the token in |
| 28 | `format.c`'s static table. |
| 29 | |
| 30 | If either gate excludes a token, libtmux leaves the field at `None` |
| 31 | rather than risking a server-side fault on an older tmux. You trade an |
| 32 | occasional `None` check for attribute access that stays safe on every |
| 33 | supported version. |
| 34 | |
| 35 | ## Why a field is `None` |
| 36 | |
| 37 | A typed field is `None` for one of three reasons: |
| 38 | |
| 39 | - **Not yet introduced.** Older tmux doesn't know the token at all. |
| 40 | {attr}`~libtmux.Pane.pane_dead_signal` is `None` on tmux 3.2a because |
| 41 | the token landed in 3.3. |
| 42 | - **Wrong scope for this object.** A {class}`~libtmux.Client` row can report |
| 43 | client tokens plus the client's current session/window/pane. `buffer_*` |
| 44 | tokens never apply to client rows. |
| 45 | - **Live-only token.** Some tokens (`mouse_*`, `cursor_*`, |
| 46 | `selection_*`) only resolve inside a live event context (key |
| 47 | binding, copy-mode, popup) — never in a `list-*` snapshot. libtmux |
| 48 | excludes them from every `-F` template. |
| 49 | |
| 50 | The version map for post-3.2a tokens is small and stable. The |
| 51 | following are the tokens libtmux currently gates: |
| 52 | |
| 53 | | Added in | Tokens | |
| 54 | |----------|--------| |
| 55 | | 3.3 | {attr}`~libtmux.Pane.pane_dead_signal`, {attr}`~libtmux.Pane.pane_dead_time` | |
| 56 | | 3.7 | {attr}`~libtmux.Pane.bracket_paste_flag`, {attr}`~libtmux.Pane.pane_flags`, {attr}`~libtmux.Pane.pane_floating_flag`, {attr}`~libtmux.Pane.pane_pb_progress`, {attr}`~libtmux.Pane.pane_pb_state`, {attr}`~libtmux.Pane.pane_pipe_pid`, {attr}`~libtmux.Pane.pane_x`, {attr}`~libtmux.Pane.pane_y`, {attr}`~libtmux.Pane.pane_z`, {attr}`~libtmux.Pane.pane_zoomed_flag`, {attr}`~libtmux.Pane.synchronized_output_flag` | |
| 57 | |
| 58 | Everything not listed above is safe on every supported tmux (≥ 3.2a). |
| 59 | Fields for newer tmux tokens will be added as each supported version is |
| 60 | validated. |
| 61 | |
| 62 | ## Active child fields |
| 63 | |
| 64 | Reach for {attr}`session.pane_id <libtmux.Session.pane_id>` and you get a real |
| 65 | pane id back, not an error. When tmux lists a parent object, it also reports |
| 66 | fields from that parent's active child — so the pane fields on a session row |
| 67 | describe the active pane in the session's current window. |
| 68 | |
| 69 | ```python |
| 70 | >>> session = server.new_session() |
| 71 | >>> session.pane_id == session.active_window.active_pane.pane_id |
| 72 | True |
| 73 | >>> session.window_id == session.active_window.window_id |
| 74 | True |
| 75 | ``` |
| 76 | |
| 77 | The relationship is **one-way**. A {class}`~libtmux.Pane` carries |
| 78 | `window_*` and `session_*` fields for its parents, but a |
| 79 | {class}`~libtmux.Session` does not carry `client_*` fields because tmux cannot |
| 80 | infer one attached client from a session row. The `client_*` tokens only |
| 81 | appear on {class}`~libtmux.Client` rows returned by |
| 82 | {attr}`~libtmux.Server.clients`. |
| 83 | |
| 84 | So read {attr}`session.pane_id <libtmux.Session.pane_id>` as "the active pane |
| 85 | of the session's current window," not "the session's pane id." Treat it as the |
| 86 | latter and the value will surprise you the moment the |
| 87 | {attr}`~libtmux.Session.active_window` changes. |
| 88 | |
| 89 | ## Inspecting which fields apply |
| 90 | |
| 91 | For the rarer cases — contributors, or code that introspects libtmux's |
| 92 | own queries — you can ask, for a given `list-*` subcommand and tmux |
| 93 | version, which tokens libtmux will request. Use |
| 94 | {func}`libtmux.neo.get_output_format`: |
| 95 | |
| 96 | ```python |
| 97 | >>> from libtmux.neo import get_output_format |
| 98 | >>> fields, _ = get_output_format("list-sessions", "3.6a") |
| 99 | >>> 'session_id' in fields |
| 100 | True |
| 101 | >>> 'pane_id' in fields # active pane for the listed session |
| 102 | True |
| 103 | >>> 'client_name' in fields # client fields require list-clients |
| 104 | False |
| 105 | ``` |
| 106 | |
| 107 | For `list-clients`, the gate widens to include `client_*` plus every |
| 108 | attached session/window/pane token: |
| 109 | |
| 110 | ```python |
| 111 | >>> from libtmux.neo import get_output_format |
| 112 | >>> fields, _ = get_output_format("list-clients", "3.6a") |
| 113 | >>> all(t in fields for t in ("client_name", "session_id", "pane_id")) |
| 114 | True |
| 115 | ``` |
| 116 | |
| 117 | The result is cached per `(list_cmd, tmux_version)` pair. |
| 118 | |
| 119 | ## tmux version detection |
| 120 | |
| 121 | You never call this directly, but it's worth knowing how the version |
| 122 | gate gets its answer. libtmux detects the live tmux version via |
| 123 | {func}`libtmux.common.get_version` and passes it through to |
| 124 | `get_output_format` whenever it builds a `-F` template. That lookup is |
| 125 | memoized for the process lifetime, as is the raw-string |
| 126 | {func}`libtmux.common.get_version_str`; the two cache independently, so |
| 127 | if you're swapping the `tmux` binary mid-test, clear both with |
| 128 | `libtmux.common.get_version.cache_clear()` and |
| 129 | `get_version_str.cache_clear()`. |
| 130 | |
| 131 | The {ref}`project` page tracks the project's minimum tmux version |
| 132 | (currently 3.2a); see {doc}`/project/compatibility` for the full |
| 133 | matrix. |
| 134 | |
| 135 | ## See also |
| 136 | |
| 137 | - {doc}`/api/libtmux.neo` — API reference for format-field helpers |
| 138 | - {func}`libtmux.neo.get_output_format` — the scope and version filter |
| 139 | - {ref}`clients` — attached-client fields and live attachment lookups |
| 140 | - {doc}`/project/compatibility` — supported tmux versions |