tmuxtmuxExamples

Choose documentation 1

latest

tmux manual version

Latest (3.7c) 3.7c 3.2a
English

Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

Edit this page on GitHub

Capture pane output

capture-pane -p prints a pane’s visible screen. Sending a command and reading its result are separate operations: the pane’s shell may still be processing input when the first capture runs.

Read what’s on screen

This complete shell program starts a private tmux server, sends a command, and captures until the expected line appears. It removes the server on exit and fails after 100 unsuccessful checks with 50-millisecond pauses.

Save it as capture.sh and run sh capture.sh, or paste the whole block into a POSIX shell. It requires tmux 3.2 or newer and sleep with fractional seconds.

capture.sh
(
set -eu
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-capture.XXXXXX")
socket="$directory/tmux.sock"
cleanup() {
status=$?
trap - EXIT
if [ -S "$socket" ] && ! tmux -S "$socket" kill-server; then
printf '%s\n' "Cannot stop tmux; kept $directory." >&2
exit 1
fi
rm -rf "$directory" || status=$?
exit "$status"
}
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
tmux -S "$socket" -f /dev/null new-session -d -s capture \
-e ENV=/dev/null 'sh'
tmux -S "$socket" send-keys -t capture:0.0 -l \
"printf '\\nlibtmux capture ready\\n'"
tmux -S "$socket" send-keys -t capture:0.0 Enter
attempt=0
while [ "$attempt" -lt 100 ]; do
screen=$(tmux -S "$socket" capture-pane -p -t capture:0.0)
if printf '%s\n' "$screen" | grep -Fqx 'libtmux capture ready'; then
printf '%s\n' "$screen"
exit 0
fi
attempt=$((attempt + 1))
sleep 0.05
done
printf '%s\n' 'Timed out waiting for captured output.' >&2
exit 1
)

Cleanup also runs if startup fails after creating the server. If tmux cannot be stopped, the script reports the error and keeps its socket directory so you can inspect or stop that server.

Wait for output or completion

The leading newline puts the output on a fresh screen row even if the shell’s first prompt arrives late. grep -Fx matches the complete line, so the echoed command cannot satisfy the check. The short pause limits polling; the captured output determines when the program finishes.

Capture reads screen state, so it can miss output that has scrolled away. Capturing output covers history and streaming; Sending keys explains input and completion.

Use a language library

Complete programs with imports, setup, and cleanup:

Python · TypeScript · Go · Rust · Java · Kotlin · Scala · C# · F# · C++ · Swift · Ruby · Lua

The tmux manual source describes capture-pane, send-keys, and kill-server.

Esc

Type to search.