libtmux
English
Reference MCP Search

Build a workspace from a file

Edit this page on GitHub

tmuxp popularized describing a tmux session as YAML — a session name, a list of windows, a list of panes and shell commands per window — and building it in one call instead of scripting each piece by hand. Several ports ship their own reader and builder for that same shape, on top of their object API rather than instead of it. Where a port has no checked snippet for this, the paragraph below says so instead of inventing one; see the table at the end for exactly which file backs each block that does exist.

Not every port folds this into the library itself. Python’s own README draws that line directly: tmuxp is “an app on top of libtmux” for exactly this job — “declarative tmux workspaces from YAML / TOML” — kept as a separate project rather than absorbed into the library. Depend on tmuxp there; it is built on the same Server/Session/Window/Pane objects the rest of this site documents.

import type { Server } from "libtmux/server";
import type { Session } from "libtmux/session";
import { applyWorkspace } from "@libtmux/workspace";
import type { WorkspaceInput } from "@libtmux/workspace/config";
/**
* Build the shape most people reach for tmux to get: one session, a window
* per concern, each pane already running the thing it is there for.
*
* `server.batch` plans several windows and resolves them from one final
* snapshot. `buildWorkspace`, below, delegates declared topology to the
* workspace package instead of maintaining another reconciler here.
*/
export async function buildSimpleWorkspace(server: Server): Promise<Session> {
const built = await server.newSession({
name: "work",
shellCommand: "sleep 30",
windowName: "editor",
});
const [logs, shell] = await server.batch([
built.plan.newWindow({ name: "logs", shellCommand: "tail -f /dev/null" }),
built.plan.newWindow({ name: "shell" }),
]);
await logs.selectLayout("even-horizontal");
void shell.name; // "shell"
return built;
}
export const DEVELOPMENT_WORKSPACE = {
session_name: "workspace-example",
windows: [
{ panes: ["sleep 30", "sleep 30"], window_name: "editor" },
{ panes: ["sleep 30"], window_name: "server" },
{ window_name: "logs" },
],
} satisfies WorkspaceInput;
/** Apply the package's declarative workspace to a server. */
export function buildWorkspace(server: Server): Promise<Session> {
return applyWorkspace(server, DEVELOPMENT_WORKSPACE);
}
/**
* Tear a workspace down without caring whether it is there.
*
* Killing a session that has already gone is not a failure worth propagating,
* which is the one case worth handling separately from every other tmux error.
*/
export async function removeWorkspace(server: Server, name: string): Promise<boolean> {
const found = (await server.snapshot()).sessions.first({ name });
if (found === undefined) return false;
await found.kill();
return true;
}

Swift’s WorkspaceBuilder.build refuses rather than adopting a session that already has the name — two callers building the same workspace shouldn’t silently share one. Workspace.decode(yaml:), behind the YAMLWorkspaces trait, reads the tmuxp-format YAML directly; Workspace.decode(json:) needs no trait, because tmuxp’s keys decode straight into these types.

Where this comes fromLink to section

PortSourceIn this pageChecked by
Pythonno fence; the README says tmuxp is a separate project by designn/a
TypeScriptexamples/workspace/workspace.ts (@libtmux/workspace)read whole from the filerun against real tmux by bun test examples/workspace
Goworkspace/example_test.go (workspace.Parse / workspace.Build)read whole from the fileExample() and its siblings run under go test and are checked against their own // Output: comments
Rustcrates/tmux-workspace/README.md, “Build it”hand-quotedthe crate’s own crates/tmux-workspace/src/lib.rs includes the README as a doc comment (#![doc = include_str!("../README.md")]), so cargo test --doc runs this exact block
Javalibtmux-workspace/README.md, “What you get back”hand-quotedevery Java fence in the module’s README is compiled and run against real tmux by docs-tests
.NETsrc/LibTmux.Workspace/README.mdhand-quotedone of the READMEs and docs ReadmeExampleTests compiles and runs against real tmux
C++examples/workspace/ (a consumer, not a library API)prose onlyexamples/workspace/tests/ runs it against real tmux as consumer.workspace; it exercises the example’s own types, not a published libtmux API
SwiftExamples/Sources/ExampleCode/Workspaces.swiftread whole from the filematched against the README’s “Workspaces, from a file or from Swift” section by Scripts/check_examples.py; compiled and run by swift test --package-path Examples

Rust, Java, and .NET quote a fenced block straight out of a README rather than a standalone example file — none of the three has one for this task — so those three rows are hand-quoted rather than read live: a build-time file= read of a whole README would pull in its surrounding prose along with the code.

Esc

Type to search.