2

Is there any way I can control a tmux server and send commands to it like switching to a specific window in a session, or make some queries about the panes through the socket it creates?

I've looked into libtmux for python and it appears to be lacking in some ways. Is there an official reference for the tmux api where I could look? The official tmux package on my distro contains only a single tmux binary.

Is there any way other than reading the source to find out how one can control tmux through its socket? Are there any other terminal multiplexers which make it easy/ are intended to make it easy?

3
+50

It is not difficult to do the tasks you ask using python-tmux.

E.g. if you start a new server with session name foo

tmux new-session -s foo

you can attach to it via python tmux (assuming the python library is installed) from ipython via

import libtmux
server = libtmux.Server()
session = server.find_where({ "session_name": "foo" })

Then you can watch in your tmux window the action of commands e.g.

session.cmd("send-keys","x")

will send a keystroke "x". The pane list you asked for can be queried via

session.cmd("list-panes").stdout

and you can switch to a specific window (say nr. 1) with

session.cmd("select-window","-t","1").stdout

You do not have to read the source code of tmux to learn this. All these commands are documented in the man page of tmux. If this is not sufficient for you, you need to be more specific what you mean by python-libtmux being "lacking in some way".

  • How do I send control characters? According to tmux man page C- will be interpreted as ctrl. libtmux passes it as a literal. – saga Apr 16 '18 at 10:48
  • The point I was trying to make is that I couldn't find the api to talk to a tmux server anywhere on the net. It also suggests that tmux wasn't supposed to be scriptable. – saga Apr 16 '18 at 10:53
  • 1
    Man page: "if the string is not recognised as a key, it is sent as a series of characters". That is, session.cmd("send-keys","C-a","x") will send Ctrl-a, x, whereas session.cmd("send-keys","C-ax") will send C, -, a, x. Also note that you cannot use C-b to send the prefix key to tmux, it will be received by the app inside the terminal. – alphanum Apr 16 '18 at 11:28
  • Apparently also the python bindings talk to tmux only via the binary. Presumably the socket api is not indended to be used by other programs. I would try to ask about this on the tmux mailing list groups.google.com/forum/#!forum/tmux-users – alphanum Apr 16 '18 at 11:40
  • I still have a comment on the part about controlling tmux directly through its socket. Presumably the best way to achieve this would be to refactor tmux into a official library/API+program structure. This would necessitate developers to keep the bindings up to date (and hopefully stable). If you were to start implementing your own custom bindings from studying the source code you might run into having to deal with changes in tmux internal protocols on a regular basis. Perhaps the official developers are open to discussions on this, if say, it may be required for performance reasons,etc. – alphanum Apr 23 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.