Filtering and queries
Use list-sessions,
list-windows, and
list-panes to query a running tmux server.
Their -f flag selects rows with a boolean format expression. Their -F flag
chooses the fields printed in each row. A listing can return zero, one, or many
rows; an empty listing is a successful command.
Match names and choose fields
Save this complete script as queries.sh. It requires tmux 3.3a or newer and
a POSIX shell. It creates a private server, runs the queries, and stops that
server on exit.
#!/bin/shset -eudirectory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-query.XXXXXX")socket="$directory/tmux.sock"
cleanup() { status=$? trap - 0 HUP INT TERM if [ -S "$socket" ] && ! tmux -S "$socket" kill-server; then printf 'Cannot stop tmux; kept %s\n' "$directory" >&2 exit 1 fi rm -rf "$directory" || status=$? exit "$status"}trap cleanup 0trap 'exit 1' HUP INT TERM
tmux -S "$socket" -f /dev/null new-session -d -s work -n editor 'cat'tmux -S "$socket" new-session -d -s worker -n editor 'cat'tmux -S "$socket" new-window -d -t '=work:' -n logs 'cat'
exact=$(tmux -S "$socket" list-sessions \ -f '#{==:#{session_name},work}' -F '#{session_name}')[ "$exact" = work ]printf 'exact: %s\n' "$exact"
casefold=$(tmux -S "$socket" list-sessions \ -f '#{m/i:WORK,#{session_name}}' -F '#{session_name}')[ "$casefold" = work ]printf 'case insensitive: %s\n' "$casefold"
windows=$(tmux -S "$socket" list-windows -t '=work' \ -f '#{m/r:^edit,#{window_name}}' -F '#{session_name}:#{window_name}')[ "$windows" = work:editor ]printf 'regex: %s\n' "$windows"
panes=$(tmux -S "$socket" list-panes -a \ -f '#{&&:#{==:#{session_name},work},#{pane_active}}' \ -F '#{pane_id}')# Pane IDs contain no whitespace; split the rows to count matches.# shellcheck disable=SC2086set -- $panes[ "$#" -eq 2 ]printf 'active panes in work: %s\n' "$#"
missing=$(tmux -S "$socket" list-sessions \ -f '#{==:#{session_name},absent}' -F '#{session_id}')[ -z "$missing" ]printf 'no matching sessions\n'
tmux -S "$socket" has-session -t '=work'if tmux -S "$socket" has-session -t '=wor' 2>/dev/null; then printf 'An exact target unexpectedly matched a prefix\n' >&2 exit 1fiprintf 'exact target checked\n'Run the saved script:
$ sh queries.shThe output is:
exact: workcase insensitive: workregex: work:editoractive panes in work: 2no matching sessionsexact target checked#{==:#{session_name},work} compares the complete session name with case
sensitivity. The m operator uses shell patterns; its i modifier ignores
case. The r modifier selects an extended regular expression instead of a
shell pattern. && requires both nested conditions to be true. Single quotes
keep the shell from interpreting the format before tmux receives it.
Choose the listing scope
list-sessions lists the server’s sessions. list-windows -t '=work' lists
windows in exactly the session named work; list-windows -a visits all
sessions. list-panes normally lists one window. Add -s to list every window
in a session, or -a to visit all sessions on the server.
pane_active identifies the active pane in each window. It does not identify
one pane for the entire server. In the script, work has two windows, so the
filter returns two pane IDs.
A window can be linked into more than one session. Server-wide window and pane listings visit those links, so the same window or pane ID can appear more than once. Keep the session fields when the link matters, or deduplicate IDs before applying an operation once per object.
Require one target
An exact session target begins with =. The
has-session command exits with zero if
that session exists and one if it does not. Without =, tmux can match name
prefixes or shell patterns. The target syntax
describes how it resolves sessions, windows, and panes.
A filter has no exactly-one guarantee. Count the returned IDs before passing
one to the next command. The querying guide
shows that check and uses
display-message with the selected ID.
Objects can disappear between commands, so keep errors from the later command
visible too.
Diagnose an empty result
Formats treat an empty string or 0 as false. Unknown format variables expand
to an empty string, which can make a misspelled field look like a valid query
with no matches. Inspect the same fields with -F before adding -f, and check
the format variables and operators.
Each listing reads live server state. Saving its output lets several local decisions use the same rows, but does not reserve the sessions, windows, or panes. Run another listing when the decision needs fresh state.
These commands and expressions are documented in the pinned tmux manual.