# Getting started

Source: https://libtmux.org/en/elixir/pr-173/guides/overview/

> Add the Hex package, check the Elixir, OTP and tmux requirements, and run an example.

<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://hex.pm/packages/libtmux"><img src="/badges/1004598792fd.svg" alt="libtmux"></a>
  <a href="https://libtmux.org/en/elixir/"><img src="/badges/cbc86bdd4623.svg" alt="libtmux.org"></a>
  <a href="https://github.com/libtmux/libtmux-elixir/blob/main/LICENSE"><img src="/badges/8378cbe3d7aa.svg" alt="license"></a>
</p>

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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/CHANGELOG.md)

### [libtmux_workspace](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/packages/workspace)

Plan and apply session and window descriptions against a running tmux server. The [package guide](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/packages/workspace/README.md) is available in the source checkout.

### [libtmux_mcp](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/packages/mcp)

Let an MCP client inspect tmux and apply explicitly authorized workspace plans over standard I/O. The [package guide](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/packages/mcp/README.md) is available in the source checkout.

## Test a program in a pane

From a repository checkout with the [requirements](https://libtmux.org/en/elixir/pr-173/guides/overview/#installation) installed
and dependencies fetched, run the notebook test on a private tmux server.

```console
$ mix run examples/interactive.exs
```

The test prints `SAVED 1`. The [program](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/examples/interactive.exs) waits for
the notebook's `READY` line, types a note with [`Pane.send_text!/4`][text],
and submits it with [`Pane.send_keys!/4`][keys]. Its `await_line` helper
reads the screen until it sees the receipt or the wait expires.

<!-- example: examples/interactive.exs#submit -->

```elixir
Pane.send_text!(server, pane, "buy milk")
unless omit_enter?, do: Pane.send_keys!(server, pane, ["Enter"])

InteractiveDemo.await_line(server, pane, "SAVED 1", timeout)
```

<!-- /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

Add the package to your Mix dependencies:

```elixir
{:libtmux, "~> 0.1"}
```

Fetch dependencies:

```console
$ mix deps.get
```

It needs:

- Elixir 1.20.4 or later in the 1.20 line, compiled for OTP 29
- OTP 29.1.2 or later in the 29 line
- tmux 3.3 or newer
- Linux and a C compiler, which builds a small helper
- no third-party runtime packages and no NIF

The exact floors and tested releases are in
[Packages and support](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/implementation.md#packages-and-support).

[Ship a CLI](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/examples/readme.exs). It
starts a private tmux server and stops it afterward. Follow the
[checkout setup](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/.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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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](https://libtmux.org/en/elixir/pr-173/examples/capture-and-read/#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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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`](https://libtmux.org/en/elixir/pr-173/guides/overview/#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](https://libtmux.org/en/elixir/pr-173/concepts/observations/) and
[compare two snapshots](https://libtmux.org/en/elixir/pr-173/concepts/observations/#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](https://libtmux.org/en/elixir/pr-173/guides/filtering/).

## 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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-program-screen-2/>) may not show `hello` yet.
- [`Pane.capture!/3`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-pane-capture-bang-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`](<https://libtmux.org/en/elixir/pr-173/reference/libtmux-server-start-1/>) does. Read the managed-control guide before
  using another host.

More: [Managed control](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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](https://libtmux.org/en/elixir/pr-173/concepts/observations/#diagnose-capture-work) for
the commands and retained response bytes a snapshot used before it returned.

More: [Recovery](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/recovery.md), [Mental model](https://libtmux.org/en/elixir/pr-173/concepts/mental-model/).

## Guides

- [Worked API examples](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/api-examples.md) show the public calls for each task.
- [Filtering](https://libtmux.org/en/elixir/pr-173/guides/filtering/): predicates, relations and finding one match.
- [One-shot actions](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/one-shot-actions.md): rename, split, input and
  screen capture.
- [Managed control](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/managed-control.md): one connection, many commands.
- [Daemon features](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/capabilities.md): release decisions and observation boundaries.
- [Owned programs in tmux panes](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/owned-program-concept.md). Run, wait
  for and cancel an application on tmux 3.8.
- [Mental model](https://libtmux.org/en/elixir/pr-173/concepts/mental-model/): 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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/CHANGELOG.md) marks each break. Pin an
  exact version to avoid one.
- **Linux.** The helper that owns tmux processes needs Linux.
- **tmux.** 3.3 or newer.

## 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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/.github/CONTRIBUTING.md) for development setup and checks,
and [Writing](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/.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](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/assets/README.md) ·
[MIT License](https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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]: https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/capabilities.md#observe-the-server-and-connection
[control-capabilities]: https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/docs/capabilities.md#preserve-the-observation-boundary
[capabilities-require]: https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/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/
