Core LibraryalphaExamples

Choose documentation 3

latest

Current version

latest
English

Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

Edit this page on GitHub

Attach and send keys

Get a session handle, send a command to a pane, and capture output. These examples use libtmux from your program; to attach your terminal interactively, see Attaching to tmux.

The examples include setup, error handling, and cleanup. Source and verification identifies their files and checks.

import { Server, TmuxCommandError, type ServerSnapshot } from "libtmux";
/**
* A runnable tour of the API, driven by the tests so it cannot rot.
*
* Every step here appears in README.md.
*/
export async function quickstart(server: Server): Promise<ServerSnapshot> {
// Nothing is read until you ask. `snapshot()` is the only step that talks to
// tmux; everything reachable from it resolves locally.
const session = await server.newSession({ name: "quickstart" });
const editor = await session.newWindow({ name: "editor" });
await editor.split();
const snapshot = await server.snapshot();
// Declarative filtering, serializable and stable on the wire.
const found = snapshot.windows.where({ name: "editor" }).one();
// Relations are plain properties: no await, no tmux command.
const paneCount = found.panes.length;
if (paneCount !== 2) throw new Error(`expected two panes, saw ${String(paneCount)}`);
// A criterion is spelled like the handle accessor it filters.
if (snapshot.panes.count({ currentCommand: { contains: "" } }) === 0) {
throw new Error("expected panes to report a current command");
}
const first = found.panes.at(0);
if (first === undefined) throw new Error("expected a pane");
await first.sendKeys("echo hello-from-libtmux", { literal: true });
// Failures carry their parts rather than a formatted sentence.
try {
await server.setOption("not-a-real-option", "1");
} catch (error) {
if (!(error instanceof TmuxCommandError)) throw error;
if (error.args[0] !== "set-option") throw error;
}
return snapshot;
}

Finding an existing session instead

For a script that runs repeatedly, look up a session before creating it. Attaching to tmux shows that pattern, and Filtering and querying, in practice covers absent and ambiguous matches.

Where this comes from

Source: examples/quickstart/quickstart.ts

In this page: read whole from the file

Checked by: run against real tmux by bun test examples; its first half is also mirrored into README.md under a <!-- runs: ... --> marker, checked line-for-line by scripts/check-doc-runnable.ts

Source inclusion

A file="..." fence reads the named source during the site build. Hand-quoted excerpts are copies; their source files and regions are listed above.

Esc

Type to search.

Reference preview Open reference