# libtmux
`libtmux` is a [typed](https://docs.python.org/3/library/typing.html) Python library that provides a wrapper for interacting programmatically with tmux, a terminal multiplexer. You can use it to manage tmux servers,
sessions, windows, and panes. Additionally, `libtmux` powers [tmuxp], a tmux workspace manager.
[![Python Package](https://img.shields.io/pypi/v/libtmux.svg)](https://pypi.org/project/libtmux/)
[![Docs](https://github.com/tmux-python/libtmux/workflows/docs/badge.svg)](https://libtmux.git-pull.com/)
[![Build Status](https://github.com/tmux-python/libtmux/workflows/tests/badge.svg)](https://github.com/tmux-python/tmux-python/actions?query=workflow%3A%22tests%22)
[![Code Coverage](https://codecov.io/gh/tmux-python/libtmux/branch/master/graph/badge.svg)](https://codecov.io/gh/tmux-python/libtmux)
[![License](https://img.shields.io/github/license/tmux-python/libtmux.svg)](https://github.com/tmux-python/libtmux/blob/master/LICENSE)
libtmux builds upon tmux's
[target](http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS) and
[formats](http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#FORMATS) to
create an object mapping to traverse, inspect and interact with live
tmux sessions.
View the [documentation](https://libtmux.git-pull.com/),
[API](https://libtmux.git-pull.com/api.html) information and
[architectural details](https://libtmux.git-pull.com/about.html).
# Install
```console
$ pip install --user libtmux
```
# Open a tmux session
Session name `foo`, window name `bar`
```console
$ tmux new-session -s foo -n bar
```
# Pilot your tmux session via python
```console
$ python
```
Use [ptpython], [ipython], etc. for a nice shell with autocompletions:
```console
$ pip install --user ptpython
```
```console
$ ptpython
```
Connect to a live tmux session:
```python
>>> import libtmux
>>> svr = libtmux.Server()
>>> svr
Server(socket_path=/tmp/tmux-.../default)
```
Tip: You can also use [tmuxp]'s [`tmuxp shell`] to drop straight into your
current tmux server / session / window pane.
[tmuxp]: https://tmuxp.git-pull.com/
[`tmuxp shell`]: https://tmuxp.git-pull.com/cli/shell.html
[ptpython]: https://github.com/prompt-toolkit/ptpython
[ipython]: https://ipython.org/
Run any tmux command, respective of context:
Honors tmux socket name and path:
```python
>>> server = Server(socket_name='libtmux_doctest')
>>> server.cmd('display-message', 'hello world')
<libtmux...>
```
New session:
```python
>>> server.cmd('new-session', '-d', '-P', '-F#{session_id}').stdout[0]
'$2'
```
```python
>>> session.cmd('new-window', '-P').stdout[0]
'libtmux...:2.0'
```
From raw command output, to a rich `Window` object (in practice and as shown
later, you'd use `Session.new_window()`):
```python
>>> Window.from_window_id(window_id=session.cmd('new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)
Window(@2 2:..., Session($1 libtmux_...))
```
Create a pane from a window:
```python
>>> window.cmd('split-window', '-P', '-F#{pane_id}').stdout[0]
'%2'
```
Raw output directly to a `Pane`:
```python
>>> Pane.from_pane_id(pane_id=window.cmd('split-window', '-P', '-F#{pane_id}').stdout[0], server=window.server)
Pane(%... Window(@1 1:..., Session($1 libtmux_...)))
```
List sessions:
```python
>>> server.sessions
[Session($1 ...), Session($0 ...)]
```
Filter sessions by attribute:
```python
>>> server.sessions.filter(history_limit='2000')
[Session($1 ...), Session($0 ...)]
```
Direct lookup:
```python
>>> server.sessions.get(session_id="$1")
Session($1 ...)
```
Filter sessions:
```python
>>> server.sessions[0].rename_session('foo')
Session($1 foo)
>>> server.sessions.filter(session_name="foo")
[Session($1 foo)]
>>> server.sessions.get(session_name="foo")
Session($1 foo)
```
Control your session:
```python
>>> session
Session($1 ...)
>>> session.rename_session('my-session')
Session($1 my-session)
```
Create new window in the background (don't switch to it):
```python
>>> bg_window = session.new_window(attach=False, window_name="ha in the bg")
>>> bg_window
Window(@... 2:ha in the bg, Session($1 ...))
# Session can search the window
>>> session.windows.filter(window_name__startswith="ha")
[Window(@... 2:ha in the bg, Session($1 ...))]
# Directly
>>> session.windows.get(window_name__startswith="ha")
Window(@... 2:ha in the bg, Session($1 ...))
# Clean up
>>> bg_window.kill()
```
Close window:
```python
>>> w = session.active_window
>>> w.kill()
```
Grab remaining tmux window:
```python
>>> window = session.active_window
>>> window.split(attach=False)
Pane(%2 Window(@1 1:... Session($1 ...)))
```
Rename window:
```python
>>> window.rename_window('libtmuxower')
Window(@1 1:libtmuxower, Session($1 ...))
```
Split window (create a new pane):
```python
>>> pane = window.split()
>>> pane = window.split(attach=False)
>>> pane.select()
Pane(%3 Window(@1 1:..., Session($1 ...)))
>>> window = session.new_window(attach=False, window_name="test")
>>> window
Window(@2 2:test, Session($1 ...))
>>> pane = window.split(attach=False)
>>> pane
Pane(%5 Window(@2 2:test, Session($1 ...)))
```
Type inside the pane (send key strokes):
```python
>>> pane.send_keys('echo hey send now')
>>> pane.send_keys('echo hey', enter=False)
>>> pane.enter()
Pane(%1 ...)
```
Grab the output of pane:
```python
>>> pane.clear() # clear the pane
Pane(%1 ...)
>>> pane.send_keys("cowsay 'hello'", enter=True)
>>> print('\n'.join(pane.cmd('capture-pane', '-p').stdout)) # doctest: +SKIP
$ cowsay 'hello'
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
...
```
Traverse and navigate:
```python
>>> pane.window
Window(@1 1:..., Session($1 ...))
>>> pane.window.session
Session($1 ...)
```
# Python support
Unsupported / no security releases or bug fixes:
- Python 2.x: The backports branch is
[`v0.8.x`](https://github.com/tmux-python/libtmux/tree/v0.8.x).
# Donations
Your donations fund development of new features, testing and support.
Your money will go directly to maintenance and development of the
project. If you are an individual, feel free to give whatever feels
right for the value you get out of the project.
See donation options at <https://git-pull.com/support.html>.
# Project details
- tmux support: 1.8+
- python support: >= 3.8, pypy, pypy3
- Source: <https://github.com/tmux-python/libtmux>
- Docs: <https://libtmux.git-pull.com>
- API: <https://libtmux.git-pull.com/api.html>
- Changelog: <https://libtmux.git-pull.com/history.html>
- Issues: <https://github.com/tmux-python/libtmux/issues>
- Test Coverage: <https://codecov.io/gh/tmux-python/libtmux>
- pypi: <https://pypi.python.org/pypi/libtmux>
- Open Hub: <https://www.openhub.net/p/libtmux-python>
- Repology: <https://repology.org/project/python:libtmux/versions>
- License: [MIT](http://opensource.org/licenses/MIT).
Raw data
{
"_id": null,
"home_page": "http://github.com/tmux-python/libtmux/",
"name": "libtmux",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "tmux, session manager, terminal, ncurses",
"author": "Tony Narlock",
"author_email": "tony@git-pull.com",
"download_url": "https://files.pythonhosted.org/packages/4a/51/b2543613a0f85247559073bc69cdca4cc0ccb590a45c98ac3400a51ba877/libtmux-0.37.0.tar.gz",
"platform": null,
"description": "# libtmux\n\n`libtmux` is a [typed](https://docs.python.org/3/library/typing.html) Python library that provides a wrapper for interacting programmatically with tmux, a terminal multiplexer. You can use it to manage tmux servers,\nsessions, windows, and panes. Additionally, `libtmux` powers [tmuxp], a tmux workspace manager.\n\n[![Python Package](https://img.shields.io/pypi/v/libtmux.svg)](https://pypi.org/project/libtmux/)\n[![Docs](https://github.com/tmux-python/libtmux/workflows/docs/badge.svg)](https://libtmux.git-pull.com/)\n[![Build Status](https://github.com/tmux-python/libtmux/workflows/tests/badge.svg)](https://github.com/tmux-python/tmux-python/actions?query=workflow%3A%22tests%22)\n[![Code Coverage](https://codecov.io/gh/tmux-python/libtmux/branch/master/graph/badge.svg)](https://codecov.io/gh/tmux-python/libtmux)\n[![License](https://img.shields.io/github/license/tmux-python/libtmux.svg)](https://github.com/tmux-python/libtmux/blob/master/LICENSE)\n\nlibtmux builds upon tmux's\n[target](http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS) and\n[formats](http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#FORMATS) to\ncreate an object mapping to traverse, inspect and interact with live\ntmux sessions.\n\nView the [documentation](https://libtmux.git-pull.com/),\n[API](https://libtmux.git-pull.com/api.html) information and\n[architectural details](https://libtmux.git-pull.com/about.html).\n\n# Install\n\n```console\n$ pip install --user libtmux\n```\n\n# Open a tmux session\n\nSession name `foo`, window name `bar`\n\n```console\n$ tmux new-session -s foo -n bar\n```\n\n# Pilot your tmux session via python\n\n```console\n$ python\n```\n\nUse [ptpython], [ipython], etc. for a nice shell with autocompletions:\n\n```console\n$ pip install --user ptpython\n```\n\n```console\n$ ptpython\n```\n\nConnect to a live tmux session:\n\n```python\n>>> import libtmux\n>>> svr = libtmux.Server()\n>>> svr\nServer(socket_path=/tmp/tmux-.../default)\n```\n\nTip: You can also use [tmuxp]'s [`tmuxp shell`] to drop straight into your\ncurrent tmux server / session / window pane.\n\n[tmuxp]: https://tmuxp.git-pull.com/\n[`tmuxp shell`]: https://tmuxp.git-pull.com/cli/shell.html\n[ptpython]: https://github.com/prompt-toolkit/ptpython\n[ipython]: https://ipython.org/\n\nRun any tmux command, respective of context:\n\nHonors tmux socket name and path:\n\n```python\n>>> server = Server(socket_name='libtmux_doctest')\n>>> server.cmd('display-message', 'hello world')\n<libtmux...>\n```\n\nNew session:\n\n```python\n>>> server.cmd('new-session', '-d', '-P', '-F#{session_id}').stdout[0]\n'$2'\n```\n\n```python\n>>> session.cmd('new-window', '-P').stdout[0]\n'libtmux...:2.0'\n```\n\nFrom raw command output, to a rich `Window` object (in practice and as shown\nlater, you'd use `Session.new_window()`):\n\n```python\n>>> Window.from_window_id(window_id=session.cmd('new-window', '-P', '-F#{window_id}').stdout[0], server=session.server)\nWindow(@2 2:..., Session($1 libtmux_...))\n```\n\nCreate a pane from a window:\n\n```python\n>>> window.cmd('split-window', '-P', '-F#{pane_id}').stdout[0]\n'%2'\n```\n\nRaw output directly to a `Pane`:\n\n```python\n>>> Pane.from_pane_id(pane_id=window.cmd('split-window', '-P', '-F#{pane_id}').stdout[0], server=window.server)\nPane(%... Window(@1 1:..., Session($1 libtmux_...)))\n```\n\nList sessions:\n\n```python\n>>> server.sessions\n[Session($1 ...), Session($0 ...)]\n```\n\nFilter sessions by attribute:\n\n```python\n>>> server.sessions.filter(history_limit='2000')\n[Session($1 ...), Session($0 ...)]\n```\n\nDirect lookup:\n\n```python\n>>> server.sessions.get(session_id=\"$1\")\nSession($1 ...)\n```\n\nFilter sessions:\n\n```python\n>>> server.sessions[0].rename_session('foo')\nSession($1 foo)\n>>> server.sessions.filter(session_name=\"foo\")\n[Session($1 foo)]\n>>> server.sessions.get(session_name=\"foo\")\nSession($1 foo)\n```\n\nControl your session:\n\n```python\n>>> session\nSession($1 ...)\n\n>>> session.rename_session('my-session')\nSession($1 my-session)\n```\n\nCreate new window in the background (don't switch to it):\n\n```python\n>>> bg_window = session.new_window(attach=False, window_name=\"ha in the bg\")\n>>> bg_window\nWindow(@... 2:ha in the bg, Session($1 ...))\n\n# Session can search the window\n>>> session.windows.filter(window_name__startswith=\"ha\")\n[Window(@... 2:ha in the bg, Session($1 ...))]\n\n# Directly\n>>> session.windows.get(window_name__startswith=\"ha\")\nWindow(@... 2:ha in the bg, Session($1 ...))\n\n# Clean up\n>>> bg_window.kill()\n```\n\nClose window:\n\n```python\n>>> w = session.active_window\n>>> w.kill()\n```\n\nGrab remaining tmux window:\n\n```python\n>>> window = session.active_window\n>>> window.split(attach=False)\nPane(%2 Window(@1 1:... Session($1 ...)))\n```\n\nRename window:\n\n```python\n>>> window.rename_window('libtmuxower')\nWindow(@1 1:libtmuxower, Session($1 ...))\n```\n\nSplit window (create a new pane):\n\n```python\n>>> pane = window.split()\n>>> pane = window.split(attach=False)\n>>> pane.select()\nPane(%3 Window(@1 1:..., Session($1 ...)))\n>>> window = session.new_window(attach=False, window_name=\"test\")\n>>> window\nWindow(@2 2:test, Session($1 ...))\n>>> pane = window.split(attach=False)\n>>> pane\nPane(%5 Window(@2 2:test, Session($1 ...)))\n```\n\nType inside the pane (send key strokes):\n\n```python\n>>> pane.send_keys('echo hey send now')\n\n>>> pane.send_keys('echo hey', enter=False)\n>>> pane.enter()\nPane(%1 ...)\n```\n\nGrab the output of pane:\n\n```python\n>>> pane.clear() # clear the pane\nPane(%1 ...)\n>>> pane.send_keys(\"cowsay 'hello'\", enter=True)\n>>> print('\\n'.join(pane.cmd('capture-pane', '-p').stdout)) # doctest: +SKIP\n$ cowsay 'hello'\n _______\n< hello >\n -------\n \\ ^__^\n \\ (oo)\\_______\n (__)\\ )\\/\\\n ||----w |\n || ||\n...\n```\n\nTraverse and navigate:\n\n```python\n>>> pane.window\nWindow(@1 1:..., Session($1 ...))\n>>> pane.window.session\nSession($1 ...)\n```\n\n# Python support\n\nUnsupported / no security releases or bug fixes:\n\n- Python 2.x: The backports branch is\n [`v0.8.x`](https://github.com/tmux-python/libtmux/tree/v0.8.x).\n\n# Donations\n\nYour donations fund development of new features, testing and support.\nYour money will go directly to maintenance and development of the\nproject. If you are an individual, feel free to give whatever feels\nright for the value you get out of the project.\n\nSee donation options at <https://git-pull.com/support.html>.\n\n# Project details\n\n- tmux support: 1.8+\n- python support: >= 3.8, pypy, pypy3\n- Source: <https://github.com/tmux-python/libtmux>\n- Docs: <https://libtmux.git-pull.com>\n- API: <https://libtmux.git-pull.com/api.html>\n- Changelog: <https://libtmux.git-pull.com/history.html>\n- Issues: <https://github.com/tmux-python/libtmux/issues>\n- Test Coverage: <https://codecov.io/gh/tmux-python/libtmux>\n- pypi: <https://pypi.python.org/pypi/libtmux>\n- Open Hub: <https://www.openhub.net/p/libtmux-python>\n- Repology: <https://repology.org/project/python:libtmux/versions>\n- License: [MIT](http://opensource.org/licenses/MIT).\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Typed library that provides an ORM wrapper for tmux, a terminal multiplexer.",
"version": "0.37.0",
"project_urls": {
"Bug Tracker": "https://github.com/tmux-python/libtmux/issues",
"Changes": "https://github.com/tmux-python/libtmux/blob/master/CHANGES",
"Documentation": "https://libtmux.git-pull.com",
"Homepage": "http://github.com/tmux-python/libtmux/",
"Repository": "https://github.com/tmux-python/libtmux"
},
"split_keywords": [
"tmux",
" session manager",
" terminal",
" ncurses"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "aa210703fd85a278f08cabca0db0a159a05511ae5216fd38921c05a89270c9b8",
"md5": "95e027c948299f170427481f1d18a65e",
"sha256": "7e8cbab30b033d132b6fca5dddb575bb7f6a1fd802328e7174f9b49023556376"
},
"downloads": -1,
"filename": "libtmux-0.37.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "95e027c948299f170427481f1d18a65e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 56197,
"upload_time": "2024-04-21T11:01:35",
"upload_time_iso_8601": "2024-04-21T11:01:35.289806Z",
"url": "https://files.pythonhosted.org/packages/aa/21/0703fd85a278f08cabca0db0a159a05511ae5216fd38921c05a89270c9b8/libtmux-0.37.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4a51b2543613a0f85247559073bc69cdca4cc0ccb590a45c98ac3400a51ba877",
"md5": "ad5001db65922d348ac03c108bfa28a2",
"sha256": "21955c5dce6332db41abad5e26ae8c4062ef2b9a89099bd57a36f52be1d5270f"
},
"downloads": -1,
"filename": "libtmux-0.37.0.tar.gz",
"has_sig": false,
"md5_digest": "ad5001db65922d348ac03c108bfa28a2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 256265,
"upload_time": "2024-04-21T11:01:38",
"upload_time_iso_8601": "2024-04-21T11:01:38.460130Z",
"url": "https://files.pythonhosted.org/packages/4a/51/b2543613a0f85247559073bc69cdca4cc0ccb590a45c98ac3400a51ba877/libtmux-0.37.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-21 11:01:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tmux-python",
"github_project": "libtmux",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "libtmux"
}