tmuxtmuxConcepts

Choose documentation 1

latest

tmux manual version

Latest (3.7c) 3.7c 3.2a
English

Edit this page on GitHub

Control mode vs one-shot

A tmux client sends commands to a server selected by its socket. A shell invocation normally sends its commands and exits after they complete. Add -C to keep an attached client that accepts commands on standard input and reports replies and events on standard output.

NeedNative tmux approach
Run a command from a shellStart a client for that command.
Run several commands togetherSeparate tmux commands with a quoted or escaped ;.
Send commands and observe events over one connectionAttach a client with -C and read the control protocol.

Group commands and open control mode

Save this complete script as transports.sh. It requires tmux 3.3a or newer and a POSIX shell. It creates a private server with a command group, sends commands through a control client, and cleans up the server.

transports.sh
#!/bin/sh
set -eu
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-transports.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 0
trap 'exit 1' HUP INT TERM
windows=$(tmux -S "$socket" -f /dev/null \
new-session -d -s work -n main 'cat' \; \
new-window -d -t '=work:' -n logs 'cat' \; \
list-windows -t '=work' -F '#{window_name}')
[ "$windows" = "$(printf 'main\nlogs')" ]
printf 'batch created two windows\n'
tmux -S "$socket" -C attach-session -t '=work' \
> "$directory/control.log" <<'COMMANDS'
display-message -p 'control command ready'
list-clients -F '#{client_control_mode}'
detach-client
COMMANDS
grep -Fx 'control command ready' "$directory/control.log" > /dev/null
grep -Fx '1' "$directory/control.log" > /dev/null
printf 'control command completed\n'
tmux -S "$socket" has-session -t '=work'
printf 'session remains after control client detaches\n'

Run the saved script:

Terminal window
$ sh transports.sh

The output is batch created two windows, control command completed, and session remains after control client detaches on separate lines.

One invocation with several commands

The escaped \; reaches tmux as a command separator. An unquoted shell ; would instead end the shell command. tmux runs the grouped commands in order on its command queue. A failure can stop later commands in the group; earlier mutations remain applied. The group does not roll back changes.

Creating a session with new-session starts the server if necessary. Ending the client process leaves the server and pane processes running. A later client can query them with list-windows or attach interactively. The command syntax describes separators, quoting, command queues, and parsing.

Read the control protocol

attach-session with the global -C flag opens a control client. Each line on its standard input is a tmux command. Replies are enclosed by %begin and %end, or by %begin and %error when a command fails. Asynchronous notifications can arrive between replies; they begin with % and include events such as %window-add and %output.

The script sends a fixed sequence and checks two response lines after the client exits. A long-running controller must parse response boundaries, command numbers, escaping, and notifications from the control-mode protocol. Reading one line for each command does not identify a complete reply.

display-message -p prints its result. detach-client closes the attached control client after the preceding commands complete.

Account for the attached client

A control connection is a real tmux client. It appears in list-clients, increments session_attached, and affects hooks and options that depend on attachment. The client_control_mode format distinguishes it from an interactive client. Detaching it leaves the session running under the default configuration; destroy-unattached can change that behavior.

Sending input with send-keys reports that tmux handled the keys. It does not prove that the pane’s program finished processing them. Use completion signals when a later step needs a completed result.

The pinned tmux manual documents command queues and control mode.

Esc

Type to search.

Reference previewOpen reference