# LibTmux.Server

- **Module:** LibTmux
- **Package:** libtmux
- **Language:** Elixir
- **Kind:** struct
- **Source:** https://github.com/libtmux/libtmux-elixir/blob/5bacf40788b43a5af27bb3656e727a921ba2db40/lib/libtmux/server.ex#L1
- **Page:** https://libtmux.org/en/elixir/pr-173/reference/libtmux-server/

Describes a local tmux server for capturing state and running commands.

`new/1` takes an absolute `:executable` path. Use
`System.find_executable/1` to resolve a name such as `"tmux"` through PATH
before construction. The optional `:cwd` and `:env` default to the caller's
working directory and environment. Construction validates the path forms
without checking that the executable or socket exists. A Server is an
immutable value; constructing one with `new/1` neither connects to tmux nor
starts it. Commands use `-N`, so a missing daemon is never started
implicitly. `start/1` and `with_owned/2` start a private server and return
a Server that `stop/2` can retire.

## Choosing the server

`new/1` picks the socket in this order, and the first rule that applies
wins:

1. `:socket`, an absolute socket path, passes `-S`.
2. `:socket_name`, a socket name, passes `-L`. Pass at most one of
   `:socket` and `:socket_name`.
3. The `LIBTMUX_SOCKET_PATH` variable passes `-S` with its path.
4. The `LIBTMUX_SOCKET` variable passes `-L` with its name.
5. Otherwise no socket is passed, and tmux chooses. It follows the server
   that `TMUX` names when `:env` has one, and otherwise uses its default
   socket: `default` in the `tmux-UID` directory of `TMUX_TMPDIR`, or of
   `/tmp`.

An explicit option ignores both variables, even an invalid one. When both
variables are set, the path wins, as `-S` does over `-L` in tmux. A variable
that is empty counts as unset, each one alone. The variables come from the
`:env` option, which defaults to the caller's environment, so a Server built
with its own `:env` ignores the caller's variables. A set variable with an
invalid value raises `ArgumentError` that names the variable and never
repeats its value.

The choice is made once, in `new/1`, and `t:t/0` records it. A Server that
chose a socket removes `TMUX` and `TMUX_PANE` from the environment it gives
tmux, so a surrounding tmux session cannot redirect it. A Server that chose
none keeps both, for one-shot execution and for control connections alike,
so a program run inside tmux reaches the server it runs in. Leave `TMUX` out
of `:env` to send a Server with no socket to tmux's default socket instead.

## What is on here

Server describes the endpoint. In Elixir, observation, selection and action
functions live in their own modules. Start with these groups:

**Connecting.** Describe the server for one-shot commands, which do not attach
a client. Attach a control client to an existing session for the length of a
callback; the client closes afterwards. Run a control client under application
supervision. See the [managed control guide](managed-control.html) for
connection setup and the host's hook and command-alias requirements.

- Describe: `new/1`
- Attach for a callback: `LibTmux.Control.with_connection/2`
- Supervise: `LibTmux.Control.start_link/1`, `LibTmux.Control.child_spec/1`

**Owning a private server.** Start a tmux server in a private directory,
with no inherited `TMUX` or `TMUX_PANE`, and stop it when you are done.
`with_owned/2` does both around a function, so a program needs no shell step
to start or clean up tmux. The process that starts the server owns it, and the
server stops when that process exits. `stop/2` refuses a Server that
`start/1` did not create; `kill/2` sends [`kill-server`](/en/tmux/latest/manual/kill-server/) to any Server.

- Start and stop around a function: `with_owned/2`
- Start: `start/1`
- Stop: `stop/2`
- Kill any server: `kill/2`
- Raise on failure: `kill!/2`

**Finding one thing.** Capture a snapshot, then require one match or allow no
match. Both reject multiple matches. Turn the chosen observation into a
reference tied to the daemon that supplied it, ready for an action.

- Capture: `snapshot/2`
- Require one: `LibTmux.Selection.one/2`
- Allow none: `LibTmux.Selection.one_or_nil/2`
- Reference: `LibTmux.Ref.from_observation/1`

**Listing everything.** Capture sessions, physical windows, window links, panes
and attached clients as a `LibTmux.Snapshot`. One accessor per kind returns a
selection from that captured graph without contacting tmux again. Capture
failures return an error, not an empty server. Capture covers an interval
rather than an atomic transaction; take another snapshot when fresh state is
needed.

- Capture: `snapshot/2`
- Select: `LibTmux.Snapshot.sessions/1`, `LibTmux.Snapshot.windows/1`,
  `LibTmux.Snapshot.window_links/1`, `LibTmux.Snapshot.panes/1`,
  `LibTmux.Snapshot.clients/1`

**Querying things and getting around.** Select by criteria such as
`active: true`, or by an Elixir function; selections work with `Enum` and
`Stream`. Navigation functions use the supplied snapshot. Plural helpers return
selections for further filtering; a `LibTmux.WindowLink` preserves a window's
membership in one session.

- By criteria: `LibTmux.Selection.where/2`
- By function: `LibTmux.Selection.filter/2`
- Down: `LibTmux.Session.windows/2`, `LibTmux.Session.panes/2`,
  `LibTmux.Window.panes/2`
- Focused: `LibTmux.Session.active_window/2`
- Other relationships: `LibTmux.Snapshot.related/4`

**Changing things.** Create detached sessions and windows on an existing server
using one-shot execution. Each returns the created ID with `ref: nil`; capture
a fresh observation to obtain a reference. Split a pane to create a pane and
get its ID in the completed result. A qualified Control connection also
supplies a bound reference; one-shot split returns `ref: nil`. Rename a session
or a window. Send literal bytes or named keys such as `"Enter"` to a pane.
Split, rename and input take a Server or Control connection followed by a
reference. Domain bang variants return completed values or raise; nonbang
variants return `{:ok, result}` for completed commands and `{:error, error}`
otherwise. See the [action guide](one-shot-actions.html) and
[recovery guide](recovery.html).

- Create: `new_session/3`, `LibTmux.Session.new_window/4`
- Split: `LibTmux.Pane.split/3`
- Rename: `LibTmux.Session.rename/4`, `LibTmux.Window.rename/4`
- Send literal bytes: `LibTmux.Pane.send_text/4`
- Send named keys: `LibTmux.Pane.send_keys/4`

**Reading screens, options and formats.** Capture pane screen bytes through
one-shot execution, which requires a Server. On a control connection, execute a
request that reads a field or writes a server user option. Submit the request
and keep the returned `LibTmux.Operation` when the result needs to remain
available for independent inspection.

- Screen: `LibTmux.Pane.capture/3`
- Execute: `LibTmux.Control.execute/3`
- Read a field: `LibTmux.Control.Request.field/2`
- Write a user option: `LibTmux.Control.Request.set_user_option/2`
- Submit: `LibTmux.Control.submit/3`

**Running any other command.** Build a command to run any other tmux command.
Running executes one command with literal arguments and returns bounded output
and exit evidence. Configured server hooks can still run. Targeted one-shot
actions check the observed server identity before writing, leaving a race if
the daemon is replaced between that check and the command. Use a qualified
Control connection for mutations bound to one daemon instance.

- Build: `LibTmux.Command.new/2`
- Build an identity probe or literal reply: `probe_command/1`
- Run: `run/3`

## Members

### Collections and queries

- [`snapshot/2`](/en/elixir/pr-173/reference/libtmux-server-snapshot-2/) (function): Captures a new graph explicitly, with bounded work and read retries.

### Common operations

- [`run/3`](/en/elixir/pr-173/reference/libtmux-server-run-3/) (function): Runs one trusted command without attaching; returns bounded output and exit evidence.
- [`new/1`](/en/elixir/pr-173/reference/libtmux-server-new-1/) (function): Constructs a descriptor for an existing server without connecting or creating it.
- [`start/1`](/en/elixir/pr-173/reference/libtmux-server-start-1/) (function): Starts a private tmux server and returns a Server for it.
- [`new_session!/3`](/en/elixir/pr-173/reference/libtmux-server-new_session-bang-3/) (function): Returns the created session value when `new_session/3` completes; raises the original error otherwise.
- [`snapshot_with_diagnostics/2`](/en/elixir/pr-173/reference/libtmux-server-snapshot_with_diagnostics-2/) (function): Captures a graph and returns work diagnostics on success or failure.
- [`snapshot!/2`](/en/elixir/pr-173/reference/libtmux-server-snapshot-bang-2/) (function): Captures a graph or raises the same structured capture error.
- [`with_owned/2`](/en/elixir/pr-173/reference/libtmux-server-with_owned-2/) (function): Starts a private server, calls `fun` with it and stops it afterwards.

### Other members

- [`capabilities/2`](/en/elixir/pr-173/reference/libtmux-server-capabilities-2/) (function): Observes named native tmux prerequisites without attaching or creating resources.
- [`cwd`](/en/elixir/pr-173/reference/libtmux-server-cwd/) (property): Required: building the struct without this key is an error.
- [`daemon`](/en/elixir/pr-173/reference/libtmux-server-daemon/) (property)
- [`env`](/en/elixir/pr-173/reference/libtmux-server-env/) (property): Required: building the struct without this key is an error.
- [`executable`](/en/elixir/pr-173/reference/libtmux-server-executable/) (property): Required: building the struct without this key is an error.
- [`kill!/2`](/en/elixir/pr-173/reference/libtmux-server-kill-bang-2/) (function): Returns `:ok` when `kill/2` completes; raises API errors or `LibTmux.Action.CompletionError` otherwise.
- [`kill/2`](/en/elixir/pr-173/reference/libtmux-server-kill-2/) (function): Kills the server with `kill-server`, whoever started it.
- [`native_helper`](/en/elixir/pr-173/reference/libtmux-server-native_helper/) (property)
- [`new_session/3`](/en/elixir/pr-173/reference/libtmux-server-new_session-3/) (function): Creates a named detached session on an existing server through one-shot execution.
- [`option/0`](/en/elixir/pr-173/reference/libtmux-server-option-0/) (typealias): Descriptor inputs.
- [`probe_command/1`](/en/elixir/pr-173/reference/libtmux-server-probe_command-1/) (function): Builds a command that prints the daemon's identity or a literal reply.
- [`socket`](/en/elixir/pr-173/reference/libtmux-server-socket/) (property)
- [`socket_name`](/en/elixir/pr-173/reference/libtmux-server-socket_name/) (property)
- [`start_option/0`](/en/elixir/pr-173/reference/libtmux-server-start_option-0/) (typealias): Inputs for `start/1`.
- [`stop/2`](/en/elixir/pr-173/reference/libtmux-server-stop-2/) (function): Stops a server that `start/1` created and removes its directory.
- [`t/0`](/en/elixir/pr-173/reference/libtmux-server-t-0/) (typealias): A local server descriptor; constructing it neither connects nor reserves server identity.

## Guides

Pages that discuss `Server`.

### Concepts

- [Read values, execute explicitly](/en/elixir/pr-173/concepts/mental-model/)

### Guides

- [Attaching to tmux](/en/elixir/pr-173/guides/attaching-to-tmux/)

## In other ports

A tmux server

| Port | API | Differs from Elixir |
| --- | --- | --- |
| Elixir | `LibTmux.Server`. Describes a local tmux server for capturing state and running commands. | This page |
| Python | [`libtmux.Server`](/en/py/latest/reference/libtmux-server/) (class). tmux(1) Server [server_manual]. | None recorded in the model |
| Ruby | [`LibTmux::Server`](/en/ruby/latest/reference/libtmux-server/) (class). Connects to an existing endpoint. Closing retires clients, never the daemon. | None recorded in the model |
| Lua | [`libtmux.Server`](/en/lua/latest/reference/libtmux-server/) (class) | None recorded in the model |
| PowerShell | [`LibTmux\New-TmuxServer`](/en/powershell/latest/reference/libtmux-new-tmuxserver/) (cmdlet). Construct a tmux endpoint handle without contacting it. | 2 overloads. This cmdlet constructs a shared .NET `LibTmux.Server` handle without contacting tmux. |
| Julia | [`LibTmux.Server`](/en/julia/latest/reference/libtmux-server/) (struct). Describe a tmux endpoint without contacting it. Supply exactly one socket selector. A description does not own a daemon or any sessions. Use from_env to opt into the ambient tmux endpoint. | `Server` describes an endpoint without contacting tmux. Read its hierarchy with `snapshot`. |
| Erlang | [`tmux_server`](/en/erlang/latest/reference/tmux_server/) (module). Open tmux endpoints and execute explicit command values. | `open/1` observes an existing daemon and starts an endpoint owner. Domain rows remain immutable values. |
| Haskell | [`LibTmux.Server`](/en/haskell/latest/reference/libtmux-server/) (module). Start, observe and change a tmux daemon through a scoped handle. | `LibTmux.Server` owns the `Server` handle. `withOwnedServer` starts and retires a private daemon; `withServer` borrows an existing endpoint and releases its own resources afterwards. `killServer` retires the admitted daemon generation explicitly. Operations such as `capture` and `renameSession` are functions of this module that take the handle. |
| Common Lisp | [`libtmux:client`](/en/lisp/latest/reference/libtmux-client/) (class). A connection scope with one external-operation lease. Closing cancels owned work and leaves the tmux daemon alive. Captured data may outlive the client. Public operations never expose its mutable storage. | `client` is a connection scope with one external-operation lease. Closing it cancels owned work and leaves the tmux daemon alive; `daemon` owns a daemon the library started. |
| TypeScript | [`server.Server`](/en/ts/latest/reference/server-server/) (class). One tmux server, addressed by the socket it listens on. | None recorded in the model |
| Rust | [`server.Server`](/en/rs/latest/reference/server-server/) (struct). A cloneable handle to one captured tmux server endpoint. | None recorded in the model |
| Go | [`tmux.Server`](/en/go/latest/reference/tmux-server/) (struct). Server is an immutable handle to one tmux configuration. Its zero value is invalid. Copying a configured Server preserves its configuration and shares its mutex-guarded version cache and checked daemon identity, so a Server and the values derived from it are safe for concurrent method calls (see the package documentation). | None recorded in the model |
| Java | [`io.github.libtmux.Server`](/en/java/latest/reference/io-github-libtmux-server-server/) (class). Manages sessions, windows, and panes on a tmux server. | readonly |
| Clojure | [`libtmux.core/open!`](/en/clojure/latest/reference/libtmux-core-open/) (function). Opens local resources for an explicit :socket-path or :socket-name endpoint. | Opens a closeable Java server client for an explicit endpoint. Acquisitions return persistent maps with explicit Java references for effects. |
| Kotlin | [`io.github.libtmux.kotlin.Server`](/en/kotlin/latest/reference/io-github-libtmux-kotlin-server/) (class). Manages sessions, windows, and panes on a tmux server. | None recorded in the model |
| Scala | [`io.github.libtmux.scaladsl.Server`](/en/scala/latest/reference/io-github-libtmux-scaladsl-server/) (typealias). The Java client, opaque: every member beyond java.lang.AutoCloseable#close is an extension, generated from the operation catalog or handwritten where the catalog marks it WAIT, STREAM or LIFECYCLE. Closing an owned client releases its transport, not tmux; close() comes from the AutoCloseable bound with no forwarding of its own, so Using.resource(server) { ... } works. | 2 overloads |
| C# | [`LibTmux.Server`](/en/csharp/latest/reference/libtmux-server/) (class). Represents an immutable server handle and snapshot. | None recorded in the model |
| F# | [`LibTmux.FSharp.Server`](/en/fsharp/latest/reference/libtmux-fsharp-server/) (module). Starts server reads and queries with the caller's cancellation token. | This F# module provides functions over the shared .NET `LibTmux.Server` type. |
| C++ | [`libtmux::Server`](/en/cxx/latest/reference/libtmux-server/) (class). One tmux server, addressed by the socket it listens on. | None recorded in the model |
| Swift | [`Server`](/en/swift/latest/reference/server/) (struct). A tmux server, addressed by its endpoint. | None recorded in the model |
| OCaml | [`Libtmux_unix.Client`](/en/ocaml/latest/reference/libtmux_unix-client/) (module). An explicit generation-bound connection. Applications retain ownership of the endpoint choice and resource lifetime. | A client is a live, generation-bound server connection. Endpoint describes a socket without connecting; closing a client releases its resources. |
| Perl | [`LibTmux::Server`](/en/perl/latest/reference/libtmux-server/) (class). Explicit tmux acquisition and commands | Construction records connection settings without tmux I/O. Optional LibTmux::Async::Server returns standard Future results for operations. |
| Raku | [`LibTmux::Server`](/en/raku/latest/reference/libtmux-server/) (class) | None recorded in the model |
