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 Python 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 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

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

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
Search in

Type to search.

Reference previewOpen reference