Server
One tmux server, addressed by the socket it listens on.
Source and package
-
Description#
Constructing this starts no process. tmux spawns the daemon on the first command that needs one, and a daemon already listening on that socket is used rather than replaced, so two
Servervalues built from the same options address the same tmux.Sessions, windows and panes are reached from here and carry the server with them. Handles taken from one daemon do not survive its restart: the replacement numbers panes from
%0again — seeDaemonIdentity.Save this complete program as
server.ts. With Bun 1.4.2 or newer, tmux 3.2a or newer, andlibtmuxinstalled, runbun run server.ts. It creates and stops a private tmux server. A failed operation or cleanup exits with an error; a cleanup error names the retained socket directory.Examples#
import { strict as assert } from "node:assert";import { mkdtemp, readdir, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { Server } from "libtmux";const directory = await mkdtemp(join(tmpdir(), "ltx-example-"));const server = new Server({socketPath: join(directory, "tmux.sock"),configFile: "/dev/null",timeoutMs: 5_000,});const failures: unknown[] = [];try {assert.equal(await server.hasSession("work"), false);const session = await server.newSession({name: "work",windowName: "editor",shellCommand: "cat",});assert.equal(await server.hasSession("work"), true);const snapshot = await server.snapshot();assert.equal(snapshot.sessions.one({ name: "work" }).id, session.id);assert.equal(snapshot.windows.count(), 1);assert.equal(snapshot.panes.count(), 1);console.log("work: 1 session, 1 window, 1 pane");} catch (error) {failures.push(error);} finally {try {if ((await readdir(directory)).includes("tmux.sock")) await server.kill();await rm(directory, { recursive: true });} catch (error) {failures.push(new Error(`Cleanup failed; inspect ${directory}`, { cause: error }));}}if (failures.length > 0) throw new AggregateError(failures, "Example failed");
Members56#
Collections and queries7#
- sessions method Every session on the server, read now.
- windows method Every window on the server, including each placement of a linked window.
- panes method Every pane on the server.
- clients method Every client attached to the server.
- listBuffers method Every buffer name this server holds.
- listCommands method Every command name the running tmux understands.
- snapshot method Acquire an immutable view of the server at this instant.
Common operations46#
- newSession method Create a detached session and resolve it as a handle.
- kill method Terminate the tmux server and every session on it.
- sourceFile method Run a tmux config file against this server.
- showOptions method Every server-scope option tmux currently reports.
- showHooks method Every global hook tmux currently reports.
- setBuffer method Put a string into a named paste buffer.
- isAlive method Whether the tmux server is reachable.
- showEnvironment method Every variable in the server's global environment.
- showBuffer method Read a named paste buffer's contents.
- deleteBuffer method Discard a named paste buffer.
- runShell method Run a shell command through tmux and return whatever it printed.
- cmd method Run a tmux command this package does not model.
- setOption method Set a server-scope option.
- setEnvironment method Set a variable in the server's global environment.
- unsetEnvironment method Drop a variable from the server's global environment entirely.
- hasSession method Whether a session with this name exists.
- unsetOption method Remove a server-scope option.
- loadBuffer method Fill a paste buffer from data fed through tmux's stdin.
- saveBuffer method Write a paste buffer to a file instead of reading it back.
- setHook method Bind a tmux command to a global hook.
- unsetHook method Remove a global hook.
- removeEnvironment method Mark a variable in the server's global environment for removal from the environment of processes tmux starts, leaving the entry in place.
- ifShell method Run one command or another depending on a condition.
- raiseIfDead method Assert the server is reachable, raising with tmux's reason if not.
- version method The tmux version this server is running.
- configFile method The configuration file this server was pointed at, if any.
- socketPath method The socket path this server addresses, if it was given one.
- showGlobalOptions method Read the defaults every session or window inherits.
- setGlobalOption method Set a default every session or window inherits.
- getEnvironment method One variable from the server's global environment, or
undefinedwhen tmux carries no entry. - colors method The terminal color override; undefined leaves detection to tmux.
- socketName method The socket name this server addresses, if it was named rather than pathed.
- batch method Run planned mutations in order, resolving each to what it made.
- connect method Bind this server to one control-mode connection and return it.
- pipeline method Run several tmux commands in order.
- watch method Stream tmux's control-mode notifications until the stream is disposed.
- checkAlive method Assert the server is reachable, raising with tmux's reason if not.
- daemonIdentity method Which daemon is answering on this socket right now.
- equals method Whether
otheris a handle addressing the same tmux server. - showBufferBytes method Read a named paste buffer without decoding or splitting its bytes.
- showResolvedOptions method The option values that govern this server, own and inherited together.
- tmuxBin method The tmux executable this server runs.
- unsetGlobalOption method Remove a default, so tmux falls back to its own built-in value.
- validateLayouts method Check every planned window layout before setup scripts or mutations.
- versionAtLeast method Whether this server is at least
minimum, written the way tmux writes it. - withConnection method Run
bodyagainst a connected server, closing it afterwards.
Other members3#
- constructor method
- serverBrand: undefined attribute
- toString method How this server renders in a log line or an error.
Guides#
Pages that discuss Server.
In other ports#
A tmux server
| Language | Equivalent |
|---|---|
| Python | libtmux |
| Ruby | Lib |
| Lua | libtmux |
| PowerShell | Lib This cmdlet constructs a shared .NET LibTmux.Server handle without contacting tmux. |
| Julia | Lib Server describes an endpoint without contacting tmux. Read its hierarchy with snapshot. |
| Elixir | Lib Server describes an existing server by executable and socket. It neither connects nor reserves the server’s identity. |
| Rust | server |
| Go | tmux |
| Java | io |
| Kotlin | io |
| Scala | io |
| C# | Lib |
| F# | Lib This F# module provides functions over the shared .NET LibTmux.Server type. |
| C++ | libtmux:: |
| Swift | Server |