tmuxtmuxExamples

Choose documentation 1

latest

tmux manual version

Latest (3.7c) 3.7c 3.2a
English

Edit this page on GitHub

Build a workspace from a file

A tmux command file can create a session, arrange its windows, and split its panes. This example builds two windows with three panes, prints their names and pane counts, then removes its private server. The first four steps write the command file, and the last four load and inspect it. It requires tmux 3.3a or newer and a POSIX shell.

Write the command file

1. Create a detached session

new-session -d creates a session without attaching a terminal. -s dev names it and -n editor names its first window. -x 100 -y 30 sizes that window, because a detached session has no terminal to size it. The final 'sh' is the program in the first pane.

workspace.confExcerpt · line 1
new-session -d -s dev -n editor -x 100 -y 30 'sh'

2. Split the window

split-window -h adds a pane beside the first one. -t '=dev:editor' names the window with an exact session name, so the split cannot reach a session whose name only starts with dev.

workspace.confExcerpt · line 2
split-window -h -t '=dev:editor' 'sh'

The editor window now has two panes. Each runs sh.

3. Add a second window

new-window -n logs adds a window named logs with one pane. A new window becomes the current one.

workspace.confExcerpt · line 3
new-window -t '=dev' -n logs 'sh'

4. Choose what is selected

select-window and select-pane move the focus back to the first pane of the editor window. .0 is the pane’s index in that window.

workspace.confExcerpt · lines 4–5
select-window -t '=dev:editor'
select-pane -t '=dev:editor.0'

The complete command file

Explicit targets keep each command tied to the intended session and window. Save the four steps together as workspace.conf:

workspace.confComplete program
new-session -d -s dev -n editor -x 100 -y 30 'sh'
split-window -h -t '=dev:editor' 'sh'
new-window -t '=dev' -n logs 'sh'
select-window -t '=dev:editor'
select-pane -t '=dev:editor.0'

Load the file

5. Start a server that stays up

start-server starts the private server before any session exists. The \; passes a literal semicolon to tmux, so set-option -s exit-empty off runs as the next command of the same invocation. Without that option the server exits when it has no sessions, which would leave nothing to stop if the file failed before creating one.

workspace.shExcerpt · lines 19–20
tmux -S "$socket" -f /dev/null start-server \; \
set-option -s exit-empty off

6. Load the file

source-file runs each line of the file as a tmux command on this server. Errors stay visible and make the script fail. Paths in the file are relative to the directory you run the script from, so keep workspace.conf beside it.

workspace.shExcerpt · line 21
tmux -S "$socket" source-file ./workspace.conf

7. List what was built

list-windows prints one row per window of the exact session dev. -F formats each row with #{window_name} and #{window_panes}.

workspace.shExcerpt · lines 22–23
tmux -S "$socket" list-windows -t '=dev' \
-F '#{window_name}: #{window_panes} panes'

8. Clean up

kill-server stops the private server. trap cleanup EXIT runs cleanup on every exit, including a failed source-file, so the server never outlives the script. If tmux cannot be stopped, the script keeps the socket directory and prints its path.

workspace.shExcerpt · lines 6–17
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

The complete script

In the file, cleanup comes first so that it covers every command after it. The parentheses run the script in a subshell, so its exit and traps leave your shell alone. Save it as workspace.sh beside the command file:

workspace.shComplete program
(
set -eu
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-workspace.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 start-server \; \
set-option -s exit-empty off
tmux -S "$socket" source-file ./workspace.conf
tmux -S "$socket" list-windows -t '=dev' \
-F '#{window_name}: #{window_panes} panes'
)

Run it from that directory:

Terminal window
$ sh workspace.sh

The result is:

editor: 2 panes
logs: 1 panes

Use a workspace manager

For YAML or JSON configuration, validation, and language APIs, choose a port:

Python CLI · TypeScript · Go · Rust · Java · C# · C++ · Swift

The workspace concept guide explains configuration and ownership. The capture example shows how to wait for pane output after sending a command.

The tmux manual source describes source-file, window targets, and exit-empty.

Esc

Type to search.

Reference previewOpen reference