libtmux convenient access to move around the hierachy of sessions, windows and panes in tmux.

This is done by libtmux’s object abstraction of targets (the -t argument) and the permanent internal ID’s tmux gives to objects.

Open two terminals:

Terminal one: start tmux in a seperate terminal:

$ tmux

Terminal two, python or ptpython if you have it:

$ python

Import libtmux:

import libtmux

Attach default tmux Server to t:

>>> t = libtmux.Server();
>>> t
<libtmux.server.Server object at 0x10edd31d0>

Get first session Session to session:

>>> session = t.sessions[0]
>>> session
Session($0 libtmux)

Get a list of sessions:

>>> t.sessions
[Session($0 libtmux), Session($1 tmuxp)]

Iterate through sessions in a server:

>>> for sess in t.sessions:
...     print(sess)

Session($0 libtmux)
Session($1 tmuxp)

Grab a Window from a session:

Window(@1 1:libtmux, Session($0 libtmux))

Grab the currently focused window from session:

>>> session.attached_window
>>> Window(@2 2:docs, Session($0 libtmux))grab the currently focused {class}`Pane` from session:
>>> session.attached_pane
Pane(%5 Window(@2 2:docs, Session($0 libtmux)))

Assign the attached Pane to p:

>>> p = session.attached_pane

Access the window/server of a pane:

>>> p.window
Window(@2 2:docs, Session($0 libtmux))

>>> p.server
<libtmux.server.Server object at 0x104191a10>