
Libtmux – pilot your tmux session via python - git-pull
https://github.com/tony/libtmux
======
mnkmnk
Where could this be used?

~~~
git-pull
Hi, thanks for your question. I'm going to cross-post this from a reddit
comment that asked the same thing.

tmux[1] is oriented toward commandline users on posix-like systems. libtmux is
a python library to help you script and automate tmux. it abstracts the
concept of the server, its sessions, their windows and panes [2] into a
herarchy you can easily navigate, even with a live tmux session.

I don't know if you use cli, tmux, or a tmux session manager (tmuxinator,
teamocil, tmuxp) already, so its hard to tailor this answer specifically.

 _command line_

a good deal of programmers, sysadmins, etc. use the terminal routinely
throughout their day. some even go so far as to swear off a graphical IDE all
together, and use screen or tmux with a combination of an editor like vim,
emacs and others.

if you primarily use windows and don't use linux/bsd/os x terminal often, if
you ever do, i highly recommend giving screen[3] or tmux a shot.

 _tmux_

the benefit to using the terminal means tmux lets you do a couple things:

\- split your terminal into multiple PTY's (panes) and windows, copy and paste
between them. No GUI required \- "detach" the tmux sesssion, sending it (and
any shells/applications running in the panes) to the background, and come back
later to reattach \- it is also seamless to SSH into a server and work from
there, since there's no GUI involved, you can use the same workflow on OS X,
Linux or BSD machines.

I write in detail about tmux here:
[http://tmuxp.readthedocs.io/en/latest/about_tmux.html](http://tmuxp.readthedocs.io/en/latest/about_tmux.html)

 _tmux configuration_

a good amount of developers use screen or tmux on a regular basis. over time,
a variety of plugins have popped up. tmuxinator[4], teamocil[5] and tmuxp[6]
(my own, written in python) being ways to load pre-configured sessions from a
YAML file.

 _scripting / automating tmux with libtmux_

so here is where libtmux comes in, it can be used to control tmux via python
objects, which can be very handy in situations where you want to interface
with tmux at a low level expressively.

it is possible to script things via shell, but there's no way to know the
state or easily traverse through the tree of Server -> Sessions -> Windows ->
Panes.

tmuxp uses libtmux under the hood a way to build sessions[7]. other developers
have used tmuxp[8]* for scripting tmux to the workflow of their projects.

* note: I link to `import tmuxp` because the API would formerly be in that project for the past few years. libtmux decouples the core into its own library.

[1] [https://tmux.github.io/](https://tmux.github.io/) [2]
[http://libtmux.readthedocs.io/api.html](http://libtmux.readthedocs.io/api.html)
[3]
[https://www.gnu.org/software/screen/](https://www.gnu.org/software/screen/)
[4]
[https://github.com/tmuxinator/tmuxinator](https://github.com/tmuxinator/tmuxinator)
[5]
[https://github.com/remiprev/teamocil](https://github.com/remiprev/teamocil)
[6] [https://github.com/tony/tmuxp](https://github.com/tony/tmuxp) [7]
[https://github.com/tony/tmuxp/blob/master/tmuxp/workspacebui...](https://github.com/tony/tmuxp/blob/master/tmuxp/workspacebuilder.py)
[8]
[https://github.com/search?utf8=%E2%9C%93&q=%22import+tmuxp%2...](https://github.com/search?utf8=%E2%9C%93&q=%22import+tmuxp%22&type=Code&ref=searchresults)

~~~
mnkmnk
I use tmux a lot. I just don't understand why I would want to script tmux. I
always thought it was supposed to be used interactively.

~~~
git-pull
> I use tmux a lot. I just don't understand why I would want to script tmux.

That's why I held off on decoupling it. I was surprised by us having at least
10 or so people using tmuxp's API purely as a scripting library.

> I always thought it was supposed to be used interactively.

Scripting doesn't preclude interactive use. Teamocil, tmuxinator, tmuxp are
all scripting under the hood. However, a yaml file only loads a session up, an
API with fresh access to the latest details of a session, window, pane makes
scripting _radically_ more easier than doing it through shell.

