Skip to main content

Typed library that provides an ORM wrapper for tmux, a terminal multiplexer.

Project description

⚙️ libtmux

Drive tmux from Python: typed, object-oriented control over servers, sessions, windows, and panes.

libtmux logo

PyPI version Docs status Tests status Coverage License

🐍 What is libtmux?

libtmux is a typed Python API over tmux, the terminal multiplexer. Stop shelling out and parsing tmux ls. Instead, interact with real Python objects: Server, Session, Window, and Pane. The same API powers tmuxp, so it stays battle-tested in real-world workflows.

✨ Features

  • Typed, object-oriented control of tmux state
  • Query and traverse live sessions, windows, and panes
  • Raw escape hatch via .cmd(...) on any object
  • Works with multiple tmux sockets and servers
  • Context managers for automatic cleanup
  • pytest plugin for isolated tmux fixtures
  • Proven in production via tmuxp and other tooling

Requirements & support

  • tmux: >= 3.2a
  • Python: >= 3.10 (CPython and PyPy)

Maintenance-only backports (no new fixes):

📦 Installation

Stable release:

$ pip install libtmux

With pipx:

$ pipx install libtmux

With uv / uvx:

$ uv add libtmux
$ uvx --from "libtmux" python

From the main branch (bleeding edge):

$ pip install 'git+https://github.com/tmux-python/libtmux.git'

Tip: libtmux is pre-1.0. Pin a range in projects to avoid surprises:

requirements.txt:

libtmux==0.50.*

pyproject.toml:

libtmux = "0.50.*"

🚀 Quickstart

Open a tmux session

First, start a tmux session to connect to:

$ tmux new-session -s foo -n bar

Pilot your tmux session via Python

Use ptpython, ipython, etc. for a nice REPL with autocompletions:

$ pip install --user ptpython
$ ptpython

Connect to a live tmux session:

>>> import libtmux
>>> svr = libtmux.Server()
>>> svr
Server(socket_path=/tmp/tmux-.../default)

Tip: You can also use tmuxp's tmuxp shell to drop straight into your current tmux server / session / window / pane.

Run any tmux command

Every object has a .cmd() escape hatch that honors socket name and path:

>>> server = Server(socket_name='libtmux_doctest')
>>> server.cmd('display-message', 'hello world')
<libtmux...>

Create a new session:

>>> server.cmd('new-session', '-d', '-P', '-F#{session_id}').stdout[0]
'$...'

List and filter sessions

Learn more about Filtering

>>> server.sessions
[Session($... ...), ...]

Filter by attribute:

>>> server.sessions.filter(history_limit='2000')
[Session($... ...), ...]

Direct lookup:

>>> server.sessions.get(session_id=session.session_id)
Session($... ...)

Control sessions and windows

Learn more about Workspace Setup

>>> session.rename_session('my-session')
Session($... my-session)

Create new window in the background (don't switch to it):

>>> bg_window = session.new_window(attach=False, window_name="bg-work")
>>> bg_window
Window(@... ...:bg-work, Session($... ...))

>>> session.windows.filter(window_name__startswith="bg")
[Window(@... ...:bg-work, Session($... ...))]

>>> session.windows.get(window_name__startswith="bg")
Window(@... ...:bg-work, Session($... ...))

>>> bg_window.kill()

Split windows and send keys

Learn more about Pane Interaction

>>> pane = window.split(attach=False)
>>> pane
Pane(%... Window(@... ...:..., Session($... ...)))

Type inside the pane (send keystrokes):

>>> pane.send_keys('echo hello')
>>> pane.send_keys('echo hey', enter=False)
>>> pane.enter()
Pane(%... ...)

Capture pane output

>>> pane.clear()
Pane(%... ...)
>>> pane.send_keys("echo 'hello world'", enter=True)
>>> pane.cmd('capture-pane', '-p').stdout  # doctest: +SKIP
["$ echo 'hello world'", 'hello world', '$']

Traverse the hierarchy

Learn more about Traversal

Navigate from pane up to window to session:

>>> pane.window
Window(@... ...:..., Session($... ...))
>>> pane.window.session
Session($... ...)

Core concepts

libtmux object tmux concept Notes
Server tmux server / socket Entry point; owns sessions
Session tmux session ($0, $1,...) Owns windows
Window tmux window (@1, @2,...) Owns panes
Pane tmux pane (%1, %2,...) Where commands run

Also available: Options and Hooks abstractions for tmux configuration.

Collections are live and queryable:

server = libtmux.Server()
session = server.sessions.get(session_name="demo")
api_windows = session.windows.filter(window_name__startswith="api")
pane = session.active_window.active_pane
pane.send_keys("echo 'hello from libtmux'", enter=True)

tmux vs libtmux vs tmuxp

Tool Layer Typical use case
tmux CLI / terminal multiplexer Everyday terminal usage, manual control
libtmux Python API over tmux Programmatic control, automation, testing
tmuxp App on top of libtmux Declarative tmux workspaces from YAML / TOML

Testing & fixtures

Learn more about the pytest plugin

Writing a tool that interacts with tmux? Use our fixtures to keep your tests clean and isolated.

def test_my_tmux_tool(session):
    # session is a real tmux session in an isolated server
    window = session.new_window(window_name="test")
    pane = window.active_pane
    pane.send_keys("echo 'hello from test'", enter=True)

    assert window.window_name == "test"
    # Fixtures handle cleanup automatically
  • Fresh tmux server/session/window/pane fixtures per test
  • Temporary HOME and tmux config fixtures keep indices stable
  • TestServer helper spins up multiple isolated tmux servers

When you might not need libtmux

  • Layouts are static and live entirely in tmux config files
  • You do not need to introspect or control running tmux from other tools
  • Python is unavailable where tmux is running

Project links

Topics: Traversal · Filtering · Pane Interaction · Workspace Setup · Automation Patterns · Context Managers · Options & Hooks

Reference: Docs · API · pytest plugin · Architecture · Changelog · Migration

Project: Issues · Coverage · Releases · License · Support

The Tao of tmux — deep-dive book on tmux fundamentals

Contributing & support

Contributions are welcome. Please open an issue or PR if you find a bug or want to improve the API or docs. If libtmux helps you ship, consider sponsoring development via support.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libtmux-0.55.1.tar.gz (439.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

libtmux-0.55.1-py3-none-any.whl (80.7 kB view details)

Uploaded Python 3

File details

Details for the file libtmux-0.55.1.tar.gz.

File metadata

  • Download URL: libtmux-0.55.1.tar.gz
  • Upload date:
  • Size: 439.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for libtmux-0.55.1.tar.gz
Algorithm Hash digest
SHA256 dcee950537b8bda4337267bc2cb62b434c4c8da3a75c1546151674238ef14e20
MD5 36f41e19a26f8ff17c9d2475c4857ee5
BLAKE2b-256 8601fa4f839045d32576d4c193d85ba37e47908fea8227f18800847f589e2476

See more details on using hashes here.

Provenance

The following attestation bundles were made for libtmux-0.55.1.tar.gz:

Publisher: tests.yml on tmux-python/libtmux

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libtmux-0.55.1-py3-none-any.whl.

File metadata

  • Download URL: libtmux-0.55.1-py3-none-any.whl
  • Upload date:
  • Size: 80.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for libtmux-0.55.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4382667d508610bdf71a7cc07d7a561d402fa2d5621cce299e7ae97b0cdcb93b
MD5 8e5cdbaa260d950e58d9f91e90b2035c
BLAKE2b-256 122e819d7414b96f19ec4cafda95555246bdb9766dd7c0519b5b1bf4495789f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for libtmux-0.55.1-py3-none-any.whl:

Publisher: tests.yml on tmux-python/libtmux

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page