Drive tmux from Elixir: capture sessions, windows and panes as an immutable snapshot, filter it with plain data, then rename, split and type through domain actions.
Packages
libtmux
Capture, filter and act on tmux sessions, windows and panes. API reference · Hex package · Changelog
libtmux_workspace
Plan and apply session and window descriptions against a running tmux server. The package guide is available in the source checkout.
libtmux_mcp
Let an MCP client inspect tmux and apply explicitly authorized workspace plans over standard I/O. The package guide is available in the source checkout.
Test a program in a pane
From a repository checkout with the requirements installed and dependencies fetched, run the notebook test on a private tmux server.
$ mix run examples/interactive.exs
The program waits for the notebook's READY line,
types a note and submits it with Enter. Pane.wait_for_text/4 checks for
the notebook's SAVED 1 receipt before the test prints it.
Pane.send_text!(server, pane, "buy milk")
unless omit_enter?, do: Pane.send_keys!(server, pane, ["Enter"])
wait = [match: :line, max_bytes: 4096, timeout: timeout]
Pane.wait_for_text(server, pane, "SAVED 1", wait)
Run with --omit-enter to skip the named key. The text reaches the pane.
The notebook still waits for a complete line, so the test prints its last
screen and exits with status 124.
$ mix run examples/interactive.exs --omit-enter
Timed out after 650 ms waiting for SAVED 1. Last screen:
READY
Type a note and press Enter.
The example stops its private server and removes the notebook's temporary data in both cases.
Installation
For a published release, add the Hex package to your Mix dependencies.
{:libtmux, "~> 0.1"}
For an API listed under Unreleased, use a checkout
that contains it. With libtmux-elixir beside your application, use this Mix
dependency instead.
{:libtmux, path: "../libtmux-elixir"}
Fetch the selected dependency.
$ mix deps.get
Requirements
- Elixir 1.20.x (minimum 1.20.4), built for OTP 29
- Erlang/OTP 29.x (minimum 29.1.2)
- tmux 3.3 or newer
- Linux with kernel headers and a C11 compiler to build the I/O helper
On tmux versions before 3.7, attaching a managed control client to an existing server requires a verified backport.
The core uses no NIF or third-party runtime packages. See Packages and support for the tested versions.
Ship a CLI covers a Mix consumer, a relocated release and the escript delivery choices for owned and existing tmux servers.
Try the examples
The snippets below form one executable program. It
starts a private tmux server and stops it afterward. Follow the
checkout setup, then run the example with tmux on PATH.
$ mix run examples/readme.exs
- Your own tmux sessions are untouched: the server has its own directory and
never sees
TMUX. - Examples that run tmux start their own private server. The pure examples run directly in the VM.
No checkout? The standalone demo installs the package from Hex.
Start a private server
Server.with_owned/2 starts a private tmux server, passes it
to your function and stops it when the function returns or raises. To own a
server across calls, use Server.start/1 and Server.stop/2.
The owned-server guide runs the complete window
creation and retirement example.
Choose the server
Use Server.new/1 to describe an existing server. Pass
:socket for an absolute socket path or :socket_name for a name.
Without either, the descriptor checks LIBTMUX_SOCKET_PATH and then
LIBTMUX_SOCKET. With both variables unset, tmux chooses the server.
See Choose a socket for the executable example.
Check tmux features on the daemon
Server.capabilities/2: decide from the daemon's release
Capabilities.require/2 returns a supported native
primitive or a refusal before dispatch. Control.capabilities/2
reads through an established control connection.
Run the capabilities example to check event-wait support.
It prints "available" on tmux 3.8 and "refused" on tmux 3.3.
More: Check daemon features.
Find sessions, windows and panes
Server.snapshot!/2: what tmux has now
The snippets from here on run inside the function that
Server.with_owned/2 calls, so server is its
private server. One call captures every session, window and pane as an
immutable graph.
Snapshot.sessions/1,
Snapshot.windows/1 and
Snapshot.panes/1 pick a type, and
Selection.where/2 filters it. None of them calls tmux again.
alias LibTmux.{Selection, Snapshot}
snapshot = Server.snapshot!(server)
active_ids =
snapshot
|> Snapshot.panes()
|> Selection.where(active: true)
|> Enum.map(& &1.id)
active_ids is ["%0"]: the demo server starts with one pane. Capture again
when you need fresh state.
More: Captured state and compare two snapshots.
Selection.where/2: filter by data
Criteria are keywords or maps, and can test related panes.
Filter.from_json/3 reads the same criteria from JSON, so a
filter can come from a file or a request. Selection.filter/2 takes
an Elixir function instead.
alias LibTmux.{Filter, Pane, Selection, Snapshot}
windows =
snapshot
|> Snapshot.windows()
|> Selection.where(panes: [some: [active: true, current_command: "cat"]])
|> Enum.map(& &1.id)
json = """
{"version":1,"entity":"pane",
"where":{"active":true,"currentCommand":{"in":["cat","nvim"]}}}
"""
{:ok, filter} = Filter.from_json(Pane, json)
pane_ids =
snapshot
|> Snapshot.panes()
|> Selection.where(filter)
|> Enum.map(& &1.id)
windows is ["@0"] and pane_ids is ["%0"]. One pane must meet both
conditions inside some.
More: Filtering.
Rename a window
Window.rename!/4: change a window's name
Continue with the same snapshot and
server. Selection.one!/2 needs exactly one match,
Session.active_window/2 reads the captured link, and
Ref.from_observation/1 turns an observation into an action target.
alias LibTmux.{Ref, Selection, Session, Snapshot, Window}
session = snapshot |> Snapshot.sessions() |> Selection.one!(id: session_id)
window = Session.active_window(snapshot, session)
{:ok, session_ref} = Ref.from_observation(session)
{:ok, window_ref} = Ref.from_observation(window)
Window.rename!(server, window_ref, "editor", session: session_ref)
It returns :ok once tmux acknowledges the command. session: names the
session whose link to the window to use. The old snapshot keeps the old name.
More: One-shot actions.
Split a pane, send input and read the screen
Control.with_connection/2: one connection for many commands
It attaches a control client to an existing session, runs your callback, and
closes the client. Pane.split!/3 returns the new pane's ID and a
bound reference. Pane.send_text!/4 sends literal text and
Pane.send_keys!/4 sends named keys. After the scope closes,
Pane.capture!/3 reads the new pane's screen through the server.
alias LibTmux.{Control, Pane, Ref, Selection, Snapshot}
pane =
snapshot
|> Snapshot.panes()
|> Selection.one!(window_id: window.id, active: true)
{:ok, pane_ref} = Ref.from_observation(pane)
{:ok, {created_id, created}} =
Control.with_connection(
[server: server, session: {:existing, session.id}, lane: :host_qualified],
fn control ->
split_opts = [axis: :horizontal, command: {:argv, ["/bin/cat"]}]
%{id: id, ref: created} = Pane.split!(control, pane_ref, split_opts)
Pane.send_text!(control, created, "hello")
Pane.send_keys!(control, created, ["Enter"])
{id, created}
end
)
screen = Pane.capture!(server, created, max_bytes: 4096)
The program prints:
{"window":"editor","active_panes":["%0"],"created_pane":"%1","connection_retired":true}
{:argv, ["/bin/cat"]}runs a program directly;{:shell, "command"}goes through a shell.- An acknowledgement does not mean the program has read its input, so
screenmay not showhelloyet. Pane.capture!/3runs one command through theserver; a control connection refuses it.lane: :host_qualifiedsays you control this server's hooks and aliases. A server fromServer.start/1does. Read the managed-control guide before using another host.
More: Managed control.
Errors and recovery
Action.CompletionError: what went wrong
Actions return {:ok, result} only when the command completes; the bang forms
raise instead. A command that times out may still have taken effect, so
inspect it before you retry.
See diagnose capture work for the commands and retained response bytes a snapshot used before it returned.
More: Recovery, Mental model.
Guides
- Worked API examples show the public calls for each task.
- Filtering: predicates, relations and finding one match.
- One-shot actions: rename, split, input and screen capture.
- Managed control: one connection, many commands.
- Daemon features: release decisions and observation boundaries.
- Owned programs in tmux panes. Run, wait for and cancel an application on tmux 3.8.
- Mental model: snapshots, references and results.
- API reference: functions and types.
Support and status
- Alpha. Releases are numbered
0.1.N. Any release may change or remove exported functions; the changelog marks each break. Pin an exact version to avoid one. - Linux. The helper that owns tmux processes needs Linux.
- tmux. One-shot operations support 3.3 or newer. Managed control on a borrowed daemon requires 3.7 or a verified backport.
What it leaves out
- A query planner. Selections run locally over a snapshot;
Query,BatchandPlanare planned. - Change notifications.
ObservationandTrackerare planned.
AI policy
The documentation index for agents and full Markdown reference cover the published Elixir API.
Use these packages with AI tools as you like. When contributing, check what you submit and wait for a maintainer's invitation before preparing a pull request. See the AI policy, also kept as AI_POLICY.md.
Contributing
See Contributing for development setup and checks, and Writing for documentation conventions.
Part of the libtmux.org family alongside Python
libtmux, sibling
language ports, and
tmux. Artwork ·
MIT License.