Core Library0.xExamples

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice
Edit this page on GitHub

Attach and send keys

Needs libtmux for TypeScript 0.x Install

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 names the file they come from.

quickstart.tsComplete program
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> {
// Acquire the snapshot after the mutations so it includes the editor window.
// Its fields and queries then resolve 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 });
// A held window is what tmux said when it was read: renaming it changes tmux, not the handle.
const window = snapshot.windows.one({ name: "editor" });
await window.rename("build");
const stale = window.name;
const fresh = (await window.refreshed()).name;
console.log(stale, fresh);
// editor build
if (stale !== "editor" || fresh !== "build")
throw new Error("expected refreshed() to read the rename");
// 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

The program is examples/quickstart/quickstart.ts in the libtmux-ts repository, shown whole.

Esc
Search in

Type to search.

Reference previewOpen reference