tmuxtmuxExamples

Choose documentation 1

latest

tmux manual version

Latest (3.7c) 3.7c 3.2a
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 libtmux
>>> server = libtmux.Server()
>>> session = server.new_session(session_name='demo')
Session(...)
>>> window = session.active_window
>>> pane = window.split(shell='sh')
>>> pane.capture_pane()
['$']
>>> pane.send_keys('echo "Hello world"', enter=True)
>>> pane.capture_pane()
['$ echo "Hello world"', 'Hello world', '$']

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

Python

Source: src/libtmux/server.py, session.py, pane.py docstrings

In this page: hand-quoted, composed from three separate docstrings

Checked by: pytest runs every >>> doctest (testpaths includes src/libtmux) against a real, isolated tmux session on every test run

TypeScript

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

Rust

Source: crates/libtmux/examples/scratch.rs

In this page: read whole from the file

Checked by: run to completion against a throwaway tmux by scripts/run-examples.sh (just examples), which CI runs and which also asserts the example leaves no session behind

Go

Source: examples/quickstart/main.go

In this page: read whole from the file

Checked by: the whole file runs against a real tmux server as TestQuickstart; the docs:quickstart region inside it is additionally mirrored into README.md by go generate ./tmux, and CI fails if the two drift

Java

Source: examples/src/main/java/io/github/libtmux/examples/BuildAWorkspace.java

In this page: read whole from the file

Checked by: run against real tmux by the examples module's own ExamplesRunTest

C#

Source: examples/LibTmux.Examples/Snippets/OneShot.cs

In this page: read whole from the file

Checked by: its ConnectAndBuild region is mirrored into README.md and checked by sync_snippets.py --check; the mirrored csharp run block is additionally compiled and run by ReadmeExampleTests

C++

Source: examples/05-readme.cpp, the connect and build regions

In this page: Copied excerpts from the connect and build regions.

Checked by: quoted verbatim into README.md, checked for drift by tools/docs/check_readme.py, and the whole file is built and run by CTest

Swift

Source: Examples/Sources/ExampleCode/Changing.swift

In this page: read whole from the file

Checked by: matched against the README's "Change what is there" section by Scripts/check_examples.py; compiled and run through the package's public products by swift test --package-path Examples

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.