Current section
Files
Jump to
Current section
Files
README.md
<!-- libtmux-logo -->
<div align="center">
<picture>
<source srcset="assets/logo.svg" type="image/svg+xml">
<img src="assets/logo.png" width="128" height="128" alt="libtmux for Elixir">
</picture>
</div>
<!-- /libtmux-logo -->
<div align="center">
<h1>libtmux for Elixir</h1>
<p>
<a href="https://hex.pm/packages/libtmux">Hex.pm</a> ยท
<a href="https://libtmux.org/en/elixir/latest/reference/">API reference</a> ยท
<a href="https://libtmux.hexdocs.pm/">HexDocs</a> ยท
<a href="https://libtmux.org/en/elixir/">Documentation</a>
</p>
<p>
<a href="https://github.com/libtmux/libtmux-elixir/actions/workflows/scaffold.yml"><img src="https://github.com/libtmux/libtmux-elixir/actions/workflows/scaffold.yml/badge.svg" alt="Core checks"></a>
<a href="https://hex.pm/packages/libtmux"><img src="https://img.shields.io/hexpm/v/libtmux?label=libtmux" alt="libtmux"></a>
<a href="https://libtmux.org/en/elixir/"><img src="https://img.shields.io/badge/docs-libtmux.org-0b7285" alt="libtmux.org"></a>
<a href="https://github.com/libtmux/libtmux-elixir/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="license"></a>
</p>
</div>
Drive tmux from Elixir: [capture][snapshot] sessions, windows and panes as an
immutable snapshot, [filter][where] it with plain data, then rename, split and
type through domain actions.
## Packages
### [libtmux](lib)
Capture, filter and act on tmux sessions, windows and panes.
[API reference](https://libtmux.org/en/elixir/latest/reference/) ยท [Hex package](https://hex.pm/packages/libtmux) ยท [Changelog](CHANGELOG.md)
### [libtmux_workspace](packages/workspace)
Plan and apply session and window descriptions against a running tmux server. The [package guide](packages/workspace/README.md) is available in the source checkout.
### [libtmux_mcp](packages/mcp)
Let an MCP client inspect tmux and apply explicitly authorized workspace plans over standard I/O. The [package guide](packages/mcp/README.md) is available in the source checkout.
## Test a program in a pane
From a repository checkout with the [requirements](#installation) installed
and dependencies fetched, run the notebook test on a private tmux server.
```console
$ mix run examples/interactive.exs
```
The [program](examples/interactive.exs) 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.
<!-- example: examples/interactive.exs#submit -->
```elixir
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)
```
<!-- /example -->
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.
```console
$ mix run examples/interactive.exs --omit-enter
```
```text
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.
```elixir
{:libtmux, "~> 0.1"}
```
For an API listed under [Unreleased](CHANGELOG.md#unreleased), use a checkout
that contains it. With `libtmux-elixir` beside your application, use this Mix
dependency instead.
```elixir
{:libtmux, path: "../libtmux-elixir"}
```
Fetch the selected dependency.
```console
$ 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](docs/managed-control.md#borrowed-servers).
The core uses no NIF or third-party runtime packages. See
[Packages and support](docs/implementation.md#packages-and-support) for the tested versions.
[Ship a CLI](docs/ship-a-cli.md) 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](examples/readme.exs). It
starts a private tmux server and stops it afterward. Follow the
[checkout setup](.github/CONTRIBUTING.md#setup), then run the example with tmux on `PATH`.
```console
$ 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](docs/standalone-demo.md) installs the
package from Hex.
## Start a private server
[`Server.with_owned/2`][with-owned] 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`][start] and [`Server.stop/2`][stop].
The [owned-server guide](docs/owned-server.md) runs the complete window
creation and retirement example.
## Choose the server
Use [`Server.new/1`][server-new] 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](docs/first-task.md#choose-a-socket) for the executable example.
## Check tmux features on the daemon
### [`Server.capabilities/2`][server-capabilities]: decide from the daemon's release
[`Capabilities.require/2`][capabilities-require] returns a supported native
primitive or a refusal before dispatch. [`Control.capabilities/2`][control-capabilities]
reads through an established control connection.
Run the [capabilities example](examples/capabilities.exs) to check event-wait support.
It prints `"available"` on tmux 3.8 and `"refused"` on tmux 3.3.
More: [Check daemon features](docs/capabilities.md).
## Find sessions, windows and panes
### [`Server.snapshot!/2`][snapshot]: what tmux has now
The snippets from here on run inside the function that
[`Server.with_owned/2`](#start-a-private-server) calls, so `server` is its
private server. One call captures every session, window and pane as an
immutable graph.
[`Snapshot.sessions/1`][snapshot-sessions],
[`Snapshot.windows/1`][snapshot-windows] and
[`Snapshot.panes/1`][snapshot-panes] pick a type, and
[`Selection.where/2`][where] filters it. None of them calls tmux again.
<!-- example: examples/readme.exs#capture -->
```elixir
alias LibTmux.{Selection, Snapshot}
snapshot = Server.snapshot!(server)
active_ids =
snapshot
|> Snapshot.panes()
|> Selection.where(active: true)
|> Enum.map(& &1.id)
```
<!-- /example -->
`active_ids` is `["%0"]`: the demo server starts with one pane. Capture again
when you need fresh state.
More: [Captured state](docs/observations.md) and
[compare two snapshots](docs/observations.md#compare-two-snapshots).
### [`Selection.where/2`][where]: filter by data
Criteria are keywords or maps, and can test related panes.
[`Filter.from_json/3`][filter-json] reads the same criteria from JSON, so a
filter can come from a file or a request. [`Selection.filter/2`][filter] takes
an Elixir function instead.
<!-- example: examples/readme.exs#filter -->
```elixir
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)
```
<!-- /example -->
`windows` is `["@0"]` and `pane_ids` is `["%0"]`. One pane must meet both
conditions inside `some`.
More: [Filtering](docs/filtering.md).
## Rename a window
### [`Window.rename!/4`][rename]: change a window's name
Continue with the same [`snapshot`][snapshot-type] and
[`server`][server-type]. [`Selection.one!/2`][one] needs exactly one match,
[`Session.active_window/2`][active-window] reads the captured link, and
[`Ref.from_observation/1`][ref] turns an observation into an action target.
<!-- example: examples/readme.exs#rename -->
```elixir
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)
```
<!-- /example -->
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](docs/one-shot-actions.md).
## Split a pane, send input and read the screen
### [`Control.with_connection/2`][scope]: one connection for many commands
It attaches a control client to an existing session, runs your callback, and
closes the client. [`Pane.split!/3`][split] returns the new pane's ID and a
bound reference. [`Pane.send_text!/4`][text] sends literal text and
[`Pane.send_keys!/4`][keys] sends named keys. After the scope closes,
[`Pane.capture!/3`][capture] reads the new pane's screen through the `server`.
<!-- example: examples/readme.exs#control -->
```elixir
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)
```
<!-- /example -->
The program prints:
```text
{"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
`screen` may not show `hello` yet.
- `Pane.capture!/3` runs one command through the `server`; a control
connection refuses it.
- `lane: :host_qualified` says you control this server's hooks and aliases.
A server from `Server.start/1` does. Read the managed-control guide before
using another host.
More: [Managed control](docs/managed-control.md).
## Errors and recovery
### [`Action.CompletionError`][completion-error]: 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](docs/observations.md#diagnose-capture-work) for
the commands and retained response bytes a snapshot used before it returned.
More: [Recovery](docs/recovery.md), [Mental model](docs/mental-model.md).
## Guides
- [Worked API examples](docs/api-examples.md) show the public calls for each task.
- [Filtering](docs/filtering.md): predicates, relations and finding one match.
- [One-shot actions](docs/one-shot-actions.md): rename, split, input and
screen capture.
- [Managed control](docs/managed-control.md): one connection, many commands.
- [Daemon features](docs/capabilities.md): release decisions and observation boundaries.
- [Owned programs in tmux panes](docs/owned-program-concept.md). Run, wait
for and cancel an application on tmux 3.8.
- [Mental model](docs/mental-model.md): snapshots, references and results.
- [API reference](https://libtmux.org/en/elixir/latest/reference/): functions
and types.
## Support and status
- **Alpha.** Releases are numbered `0.1.N`. Any release may change or remove
exported functions; the [changelog](CHANGELOG.md) 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](docs/managed-control.md#borrowed-servers).
## What it leaves out
- **A query planner.** Selections run locally over a snapshot; `Query`,
`Batch` and `Plan` are planned.
- **Change notifications.** `Observation` and `Tracker` are planned.
## AI policy
The [documentation index for agents](https://libtmux.org/en/elixir/latest/llms.txt)
and [full Markdown reference](https://libtmux.org/en/elixir/latest/llms-full.txt)
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](https://libtmux.org/en/meta/ai-policy/), also kept as
[AI_POLICY.md](https://github.com/libtmux/.github/blob/HEAD/AI_POLICY.md).
## Contributing
See [Contributing](.github/CONTRIBUTING.md) for development setup and checks,
and [Writing](.github/WRITING.md) for documentation conventions.
Part of the [libtmux.org](https://libtmux.org/) family alongside Python
[`libtmux`](https://github.com/tmux-python/libtmux), sibling
[language ports](https://github.com/libtmux), and
[tmux](https://github.com/tmux/tmux). [Artwork](assets/README.md) ยท
[MIT License](LICENSE).
[with-owned]: https://libtmux.org/en/elixir/latest/reference/libtmux-server-with_owned-2/
[start]: https://libtmux.org/en/elixir/latest/reference/libtmux-server-start-1/
[stop]: https://libtmux.org/en/elixir/latest/reference/libtmux-server-stop-2/
[snapshot]: https://libtmux.org/en/elixir/latest/reference/libtmux-server-snapshot-bang-2/
[snapshot-type]: https://libtmux.org/en/elixir/latest/reference/libtmux-snapshot/
[snapshot-sessions]: https://libtmux.org/en/elixir/latest/reference/libtmux-snapshot-sessions-1/
[snapshot-windows]: https://libtmux.org/en/elixir/latest/reference/libtmux-snapshot-windows-1/
[snapshot-panes]: https://libtmux.org/en/elixir/latest/reference/libtmux-snapshot-panes-1/
[server-type]: https://libtmux.org/en/elixir/latest/reference/libtmux-server/
[server-new]: https://libtmux.org/en/elixir/latest/reference/libtmux-server-new-1/
[server-capabilities]: docs/capabilities.md#observe-the-server-and-connection
[control-capabilities]: docs/capabilities.md#preserve-the-observation-boundary
[capabilities-require]: docs/capabilities.md#check-a-named-primitive
[where]: https://libtmux.org/en/elixir/latest/reference/libtmux-selection-where-2/
[filter]: https://libtmux.org/en/elixir/latest/reference/libtmux-selection-filter-2/
[filter-json]: https://libtmux.org/en/elixir/latest/reference/libtmux-filter-from_json-3/
[one]: https://libtmux.org/en/elixir/latest/reference/libtmux-selection-one-bang-2/
[active-window]: https://libtmux.org/en/elixir/latest/reference/libtmux-session-active_window-2/
[ref]: https://libtmux.org/en/elixir/latest/reference/libtmux-ref-from_observation-1/
[rename]: https://libtmux.org/en/elixir/latest/reference/libtmux-window-rename-bang-4/
[scope]: https://libtmux.org/en/elixir/latest/reference/libtmux-control-with_connection-2/
[split]: https://libtmux.org/en/elixir/latest/reference/libtmux-pane-split-bang-3/
[text]: https://libtmux.org/en/elixir/latest/reference/libtmux-pane-send_text-bang-4/
[keys]: https://libtmux.org/en/elixir/latest/reference/libtmux-pane-send_keys-bang-4/
[capture]: https://libtmux.org/en/elixir/latest/reference/libtmux-pane-capture-bang-3/
[completion-error]: https://libtmux.org/en/elixir/latest/reference/libtmux-action-completionerror/