Skip to main content

⚙️ libtmux

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

libtmux logo

tests libtmux libtmux.org 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
  • Locate yourself from inside a pane with from_env()
  • 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.3a
  • Python: >= 3.11, < 4.0 (CPython and PyPy)

Maintenance-only backports (no new fixes):

📦 Installation

Install libtmux into the Python environment that will import it:

$ python -m pip install libtmux

With uv in a project:

$ uv add libtmux

For an ad hoc Python REPL:

$ uv run \
    --with libtmux \
    python

From the main branch (bleeding edge):

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

Tip: libtmux is pre-1.0, and a minor release can change the API. Pin the minor series your project tests against. Print the installed version:

$ python -c 'import libtmux; print(libtmux.__version__)'

Then pin its series, writing 0.N for that version's first two numbers:

requirements.txt:

libtmux==0.N.*

pyproject.toml:

[project]
dependencies = ["libtmux==0.N.*"]

🚀 Quickstart

Create a server on a temporary socket. The context managers stop the server and remove the temporary directory when the block exits:

>>> import tempfile
>>> import libtmux
>>> with tempfile.TemporaryDirectory() as directory:
...     with libtmux.Server(socket_path=f"{directory}/tmux.sock", kill_on_exit=True) as server:
...         session = server.new_session(session_name="demo")
...         window = session.active_window
...         pane = window.active_pane
...         pane.send_keys("echo hello")
...         print(session.session_name)
demo

The shorter reference examples below use server, session, window, pane, and imported classes supplied by this repository's doctest setup. In your own code, create those objects as above or select them from an existing server. monkeypatch in the environment example is a pytest fixture.

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=.../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($... ...)

Know where you're running

Learn more about Locating yourself

Code running inside a pane — a script in a split, a tmux hook, an agent — can ask where it is. tmux writes TMUX and TMUX_PANE into every pane it spawns, and Server, Session, Window, and Pane each read them back:

>>> socket_path = server.cmd(
...     "display-message", "-p", "-t", session.session_id, "#{socket_path}"
... ).stdout[0]
>>> monkeypatch.setenv("TMUX", f"{socket_path},1,{session.session_id}")
>>> monkeypatch.setenv("TMUX_PANE", pane.pane_id)

>>> Pane.from_env()
Pane(%... ...)
>>> Session.from_env().session_name == session.session_name
True

In a real pane tmux has already set those two variables, so from_env() takes no arguments and there is nothing to arrange — this README is not running in a pane, so the example sets them first. Outside tmux there is no pane to return, and from_env() raises NotInsideTmux.

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 server and session fixtures per test, each on an isolated tmux socket; derive windows and panes from session
  • 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

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

AI policy

Use these packages with AI tools as you like. When contributing, check what you submit and wait for a maintainer's invitation before preparing a pull request. See the AI policy, also kept as AI_POLICY.md.

Attribution

Please use the following BibTeX template to cite libtmux in scientific discourse:

@misc{libtmux,
   author = {Tony Narlock},
   year = {2016},
   note = {https://libtmux.git-pull.com},
   title = {libtmux: python wrapper for tmux}
}

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.

Metadata

Release files for libtmux 0.70.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libtmux 0.70.0
File Size Uploaded Provenance Actions
libtmux-0.70.0.tar.gz 1.5 MB With provenance Details for libtmux-0.70.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for libtmux 0.70.0
File Interpreter ABI Platform Actions
libtmux-0.70.0-py3-none-any.whl Python 3 none any Details for libtmux-0.70.0-py3-none-any.whl

Total release size: 1.7 MB

Release files / libtmux-0.70.0.tar.gz

Download URL libtmux-0.70.0.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
1496d6253aea98321a99b5e414f378154967a570ffd1f5dc9f1effaa01f782c3
BLAKE2b-256 checksum
How to use checksums
51f1a44043193fd4c212858cdca04b8e628ce557726bf81f04fdc9765d4c2ac7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / libtmux-0.70.0-py3-none-any.whl

Download URL libtmux-0.70.0-py3-none-any.whl
Size 189.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9fcdccc64bb354ca6e243e942484b58f4f950b4a14ff4a324ae2dc4005afe1b2
BLAKE2b-256 checksum
How to use checksums
c9f69273ce6bba65485ad2c6d95eb287798913b463cfddd6c7737a01fb7587f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for libtmux 0.70.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for libtmux-0.70.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.70.0 This release

2 release files Full provenance

0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page