# libtmux.Server

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Lua
- **Kind:** class
- **Source:** https://github.com/libtmux/libtmux-lua/blob/d68f829fe097e38dcbbf7147e276666fe615f0cf/lua/libtmux/_internal/server.lua#L736
- **Page:** https://libtmux.org/en/lua/v0.1.1/reference/libtmux-server/

A handle to one running tmux server, reached through a connected runtime.

 `libtmux.Runtime:connect` resolves to a server for a daemon that is already
 running. The library never starts a daemon, and closing the handle leaves
 the daemon running. With no socket named it passes tmux none, and tmux picks
 the server (see `libtmux.ConnectOptions`). A method that talks to tmux
 returns a `libtmux.Request`: call `libtmux.Request:await` from a task the
 runtime manages, or pass a callback to `libtmux.Request:on_complete`. A
 failed request reports a `libtmux.Error`. If the daemon restarts or its
 socket is replaced, the handle stops working and the caller connects again.

 # What is on here

 A tmux server does a great many things, so this class has a great many
 methods. They fall into a few groups:

 **Connecting.** `libtmux.Runtime:connect` takes `libtmux.ConnectOptions`: an
 absolute path to the tmux binary and, optionally, to the daemon's socket.
 `Server:close` closes the connections the handle owns. `Server:observe`
 takes a session handle and resolves to a `libtmux.Observation`, a
 control-mode view of that session whose watches report pane output,
 notifications and format values.

 `libtmux.Runtime:connect`, `Server:close` and `Server:observe`

 **Finding one thing.** Take one record from a `libtmux.Snapshot`, or from
 the rows of a query, with `libtmux.Selection:one`, which reports no match or
 several as errors, or `libtmux.Selection:one_or_nil`, which also accepts no
 match. Pass it, with its snapshot, to `Server:handle`: that binds the record
 to a live session, window, window link, pane, client or buffer handle of the
 same class, and returns it directly rather than as a request.

 `libtmux.Selection:one`, `libtmux.Selection:one_or_nil` and `Server:handle`

 **Listing everything.** `Server:snapshot` lists sessions, windows, window
 links, panes, clients and buffers, and resolves to a `libtmux.Snapshot`
 whose collections are selections. The capture spans several tmux commands,
 so it is not an atomic view: observed races land in `libtmux.Snapshot.races`
 and clear `libtmux.Snapshot.complete`, and `libtmux.SnapshotOptions.strict`
 adds a verification pass.

 `Server:snapshot`, `libtmux.Snapshot.races`, `libtmux.Snapshot.complete` and
 `libtmux.SnapshotOptions.strict`

 **Querying things and getting around.** `Server:query` reads live state and
 resolves to the rows that match a criteria table, with the snapshot and plan
 behind them; `Server:query_panes` does the same for panes. `Server:explain`
 and `Server:explain_panes` return the plan without contacting tmux. On a
 snapshot already in hand, `libtmux.Selection:where` and
 `libtmux.Selection:filter` narrow a collection in memory, and
 `libtmux.Selection:first`, `libtmux.Selection:count` and
 `libtmux.Selection:exists` inspect it; the `libtmux.query` module does the
 same for ordinary Lua tables. Records lead to each other without another
 command: `libtmux.SnapshotSession.window_links`,
 `libtmux.SnapshotWindow.panes`, `libtmux.SnapshotPane.window`,
 `libtmux.SnapshotWindowLink.session` and
 `libtmux.SnapshotWindowLink.window`.

 `Server:query`, `Server:query_panes`, `Server:explain`,
 `Server:explain_panes`, `libtmux.Selection:where`,
 `libtmux.Selection:filter`, `libtmux.Selection:first`,
 `libtmux.Selection:count`, `libtmux.Selection:exists`, `libtmux.query`,
 `libtmux.SnapshotSession.window_links`, `libtmux.SnapshotWindow.panes`,
 `libtmux.SnapshotPane.window`, `libtmux.SnapshotWindowLink.session` and
 `libtmux.SnapshotWindowLink.window`

 **Changing things.** `Server:new_session` creates a detached session and
 resolves to a `libtmux.Creation` holding handles for the session and the
 window, pane and window link it starts with. `Server:switch_client` points
 an attached client, selected by the name and TTY a snapshot reports, at a
 session handle, and `Server:detach_client` detaches it.

 `Server:new_session`, `Server:switch_client` and `Server:detach_client`

 **Options and hooks.** `Server:get_option`, `Server:list_options`,
 `Server:set_option` and `Server:unset_option` address the server's own
 options; pass `scope = "global_session"` or `"global_window"` for tmux's
 global defaults. `Server:get_hook`, `Server:list_hooks`, `Server:set_hook`
 and `Server:unset_hook` need one of those two scopes spelled out. The global
 environment has `Server:get_environment`, `Server:list_environment` and
 `Server:set_environment`; `Server:unset_environment` deletes a name, while
 `Server:remove_environment` records a removal marker instead.

 `Server:get_option`, `Server:list_options`, `Server:set_option`,
 `Server:unset_option`, `Server:get_hook`, `Server:list_hooks`,
 `Server:set_hook`, `Server:unset_hook`, `Server:get_environment`,
 `Server:list_environment`, `Server:set_environment`,
 `Server:unset_environment` and `Server:remove_environment`

 **Everything else tmux keeps.** Paste buffers, always by explicit name:
 `Server:set_buffer` stores bytes, `Server:show_buffer` reads them back and
 `Server:delete_buffer` removes the buffer. None infers the most recent
 buffer. Key bindings, configuration files, shell jobs and wait-for channels
 have no method here; reach them through `Server:command`.

 `Server:set_buffer`, `Server:show_buffer`, `Server:delete_buffer` and
 `Server:command`

 **Things that need a terminal.** None. The luv and Neovim adapters do not
 own an interactive terminal, so `libtmux.Session:attach` returns
 `unsupported_tty`.

 `libtmux.Session:attach`

 **Running a command.** Anything tmux can do that is not here is reachable
 through `Server:command`, which runs one tmux command from literal arguments
 and resolves to its stdout, stderr, exit code and signal; a nonzero exit is
 reported in that result, not as a failure. `Server:group` sends several
 commands as one ordered group with a single aggregate result, and
 `Server:batch` runs independent commands and reports an outcome for each.

 `Server:command`, `Server:group` and `Server:batch`

## Members

### Collections and queries

- [`snapshot`](/en/lua/v0.1.1/reference/libtmux-server-snapshot/) (method)
- [`query_panes`](/en/lua/v0.1.1/reference/libtmux-server-query_panes/) (method)
- [`query`](/en/lua/v0.1.1/reference/libtmux-server-query/) (method)

### Common operations

- [`new_session`](/en/lua/v0.1.1/reference/libtmux-server-new_session/) (method)
- [`list_hooks`](/en/lua/v0.1.1/reference/libtmux-server-list_hooks/) (method)
- [`set_buffer`](/en/lua/v0.1.1/reference/libtmux-server-set_buffer/) (method)
- [`list_environment`](/en/lua/v0.1.1/reference/libtmux-server-list_environment/) (method)
- [`show_buffer`](/en/lua/v0.1.1/reference/libtmux-server-show_buffer/) (method)
- [`delete_buffer`](/en/lua/v0.1.1/reference/libtmux-server-delete_buffer/) (method)
- [`command`](/en/lua/v0.1.1/reference/libtmux-server-command/) (method)
- [`set_option`](/en/lua/v0.1.1/reference/libtmux-server-set_option/) (method)
- [`set_environment`](/en/lua/v0.1.1/reference/libtmux-server-set_environment/) (method)
- [`unset_environment`](/en/lua/v0.1.1/reference/libtmux-server-unset_environment/) (method)
- [`unset_option`](/en/lua/v0.1.1/reference/libtmux-server-unset_option/) (method)
- [`get_option`](/en/lua/v0.1.1/reference/libtmux-server-get_option/) (method)
- [`set_hook`](/en/lua/v0.1.1/reference/libtmux-server-set_hook/) (method)
- [`unset_hook`](/en/lua/v0.1.1/reference/libtmux-server-unset_hook/) (method)
- [`remove_environment`](/en/lua/v0.1.1/reference/libtmux-server-remove_environment/) (method)
- [`observe`](/en/lua/v0.1.1/reference/libtmux-server-observe/) (method)
- [`handle`](/en/lua/v0.1.1/reference/libtmux-server-handle/) (method): Binds a snapshot record to a live handle of its class.
- [`close`](/en/lua/v0.1.1/reference/libtmux-server-close/) (method)
- [`detach_client`](/en/lua/v0.1.1/reference/libtmux-server-detach_client/) (method)
- [`explain`](/en/lua/v0.1.1/reference/libtmux-server-explain/) (method)
- [`explain_panes`](/en/lua/v0.1.1/reference/libtmux-server-explain_panes/) (method)
- [`group`](/en/lua/v0.1.1/reference/libtmux-server-group/) (method)
- [`switch_client`](/en/lua/v0.1.1/reference/libtmux-server-switch_client/) (method)

### Other members

- [`batch`](/en/lua/v0.1.1/reference/libtmux-server-batch/) (method)
- [`get_environment`](/en/lua/v0.1.1/reference/libtmux-server-get_environment/) (method)
- [`get_hook`](/en/lua/v0.1.1/reference/libtmux-server-get_hook/) (method)
- [`list_options`](/en/lua/v0.1.1/reference/libtmux-server-list_options/) (method)

## In other ports

A tmux server

| Language | Equivalent |
| --- | --- |
| Python | [`libtmux.Server`](/en/py/latest/reference/libtmux-server/) (class) |
| Ruby | [`LibTmux::Server`](/en/ruby/latest/reference/libtmux-server/) (class) |
| PowerShell | [`LibTmux\New-TmuxServer`](/en/powershell/latest/reference/libtmux-new-tmuxserver/) (cmdlet). This cmdlet constructs a shared .NET `LibTmux.Server` handle without contacting tmux. |
| Julia | [`LibTmux.Server`](/en/julia/latest/reference/libtmux-server/) (struct). `Server` describes an endpoint without contacting tmux. Read its hierarchy with `snapshot`. |
| Elixir | [`LibTmux.Server`](/en/elixir/latest/reference/libtmux-server/) (struct). `Server` describes an existing server by executable and socket. It neither connects nor reserves the server’s identity. |
| TypeScript | [`server.Server`](/en/ts/latest/reference/server-server/) (class) |
| Rust | [`server.Server`](/en/rs/latest/reference/server-server/) (struct) |
| Go | [`tmux.Server`](/en/go/latest/reference/tmux-server/) (struct) |
| Java | [`io.github.libtmux.Server`](/en/java/latest/reference/io-github-libtmux-server-server/) (class) |
| Kotlin | [`io.github.libtmux.kotlin.Server`](/en/kotlin/latest/reference/io-github-libtmux-kotlin-server/) (class) |
| Scala | [`io.github.libtmux.scaladsl.Server`](/en/scala/latest/reference/io-github-libtmux-scaladsl-server/) (typealias) |
| C# | [`LibTmux.Server`](/en/csharp/latest/reference/libtmux-server/) (class) |
| F# | [`LibTmux.FSharp.Server`](/en/fsharp/latest/reference/libtmux-fsharp-server/) (module). This F# module provides functions over the shared .NET `LibTmux.Server` type. |
| C++ | [`libtmux::Server`](/en/cxx/latest/reference/libtmux-server/) (class) |
| Swift | [`Server`](/en/swift/latest/reference/server/) (struct) |
