Server.newSession
Create a detached session and resolve it as a handle.
Source and package
-
Description#
Save this complete program as
new-session.ts. With Bun 1.4.2 or newer, tmux 3.2a or newer, andlibtmuxinstalled, runbun run new-session.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 {const session = await server.newSession({name: "work",windowName: "editor",shellCommand: "cat",startDirectory: directory,environment: { LIBTMUX_EXAMPLE: "ready" },});assert.equal(session.name, "work");assert.equal(session.windows.one().name, "editor");assert.equal(session.panes.one().currentPath, directory);console.log("created:", session.name, session.windows.one().name);await server.newSession({ name: "review", groupWith: session.id });await session.newWindow({ name: "logs", shellCommand: "cat" });const snapshot = await server.snapshot();const review = snapshot.sessions.one({ name: "review" });assert.equal(review.windows.count(), 2);assert.equal(snapshot.sessions.one({ name: "work" }).windows.count(), 2);console.log("grouped review windows:",review.windows.map((item) => item.name).sort().join(", "),);} 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");- Parameters
tmux command#
The tmux commands behind newSession.
tmux C API#
The C declarations in tmux that implement these commands.
Each path starts at a function a command runs, which is degree 1. The degree counts steps from there along the path, and each step is a call or a reference: degree 2 is reached from that function, degree 3 from the next, and so on.
Create a session and its first pane
- 1
cmd_new_session_exec - calls → 2
spawn_window - calls → 3
spawn_pane - calls → 4
window_add_pane - calls → 5
window_pane_create
Guides#
Pages that discuss newSession.
In other ports#
Create a session
| Language | Equivalent |
|---|---|
| Python | libtmux |
| Ruby | Lib |
| Lua | libtmux |
| Rust | server |
| Go | tmux |
| Java | io |
| Kotlin | io |
| Scala | io |
| C# | Lib |
| C++ | libtmux:: |
| Swift | Server |
No equivalent is recorded in PowerShell, Julia, Elixir or F#.