Skip to content

Commit 70bccee

Browse files
committed
docs(README): restore interactive examples and doctest coverage
why: The refreshed README removed valuable interactive examples that help users learn the API through hands-on experimentation. what: - Restore ptpython/ipython REPL tips for exploring the API - Add tmuxp shell shortcut tip for power users - Restore doctest examples: session/window control, pane splitting - Add capture-pane output example with doctest +SKIP - Add hierarchy traversal examples (pane.window.session) - Add architecture/about link to project links - Emphasize The Tao of tmux book link - Remove duplicate "Raw tmux when you need it" section - All 12 README doctests pass via `uv run pytest README.md --doctest-glob="*.md"`
1 parent cc08ee5 commit 70bccee

File tree

1 file changed

+127
-25
lines changed

1 file changed

+127
-25
lines changed

README.md

Lines changed: 127 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,136 @@ libtmux = "0.49.*"
8282

8383
## 🚀 Quickstart
8484

85-
Create and drive a tmux session entirely from Python.
85+
### Open a tmux session
86+
87+
First, start a tmux session to connect to:
88+
89+
```console
90+
$ tmux new-session -s foo -n bar
91+
```
92+
93+
### Pilot your tmux session via Python
94+
95+
Use [ptpython], [ipython], etc. for a nice REPL with autocompletions:
96+
97+
```console
98+
$ pip install --user ptpython
99+
$ ptpython
100+
```
101+
102+
Connect to a live tmux session:
86103

87104
```python
88-
import libtmux
89-
import time
105+
>>> import libtmux
106+
>>> svr = libtmux.Server()
107+
>>> svr
108+
Server(socket_path=/tmp/tmux-.../default)
109+
```
90110

91-
# Connect to tmux (starts one if not running)
92-
server = libtmux.Server()
93-
# Unique name avoids collisions on repeated runs
94-
session_name = f"libtmux-demo-{int(time.time())}"
95-
session = server.new_session(session_name=session_name, window_name="workspace")
111+
**Tip:** You can also use [tmuxp]'s [`tmuxp shell`] to drop straight into your
112+
current tmux server / session / window / pane.
113+
114+
[ptpython]: https://github.com/prompt-toolkit/ptpython
115+
[ipython]: https://ipython.org/
116+
[`tmuxp shell`]: https://tmuxp.git-pull.com/cli/shell.html
117+
118+
### Run any tmux command
119+
120+
Every object has a `.cmd()` escape hatch that honors socket name and path:
121+
122+
```python
123+
>>> server = Server(socket_name='libtmux_doctest')
124+
>>> server.cmd('display-message', 'hello world')
125+
<libtmux...>
126+
```
127+
128+
Create a new session:
129+
130+
```python
131+
>>> server.cmd('new-session', '-d', '-P', '-F#{session_id}').stdout[0]
132+
'$...'
133+
```
134+
135+
### List and filter sessions
136+
137+
```python
138+
>>> server.sessions
139+
[Session($... ...), ...]
140+
```
141+
142+
Filter by attribute:
143+
144+
```python
145+
>>> server.sessions.filter(history_limit='2000')
146+
[Session($... ...), ...]
147+
```
148+
149+
Direct lookup:
150+
151+
```python
152+
>>> server.sessions.get(session_id=session.session_id)
153+
Session($... ...)
154+
```
155+
156+
### Control sessions and windows
157+
158+
```python
159+
>>> session.rename_session('my-session')
160+
Session($... my-session)
161+
```
162+
163+
Create new window in the background (don't switch to it):
164+
165+
```python
166+
>>> bg_window = session.new_window(attach=False, window_name="bg-work")
167+
>>> bg_window
168+
Window(@... ...:bg-work, Session($... ...))
169+
170+
>>> session.windows.filter(window_name__startswith="bg")
171+
[Window(@... ...:bg-work, Session($... ...))]
172+
173+
>>> session.windows.get(window_name__startswith="bg")
174+
Window(@... ...:bg-work, Session($... ...))
175+
176+
>>> bg_window.kill()
177+
```
178+
179+
### Split windows and send keys
180+
181+
```python
182+
>>> pane = window.split(attach=False)
183+
>>> pane
184+
Pane(%... Window(@... ...:..., Session($... ...)))
185+
```
96186

97-
window = session.active_window
98-
left = window.active_pane
99-
right = window.split_window(vertical=True)
187+
Type inside the pane (send keystrokes):
188+
189+
```python
190+
>>> pane.send_keys('echo hello')
191+
>>> pane.send_keys('echo hey', enter=False)
192+
>>> pane.enter()
193+
Pane(%... ...)
194+
```
100195

101-
# Send commands to panes
102-
left.send_keys("vim", enter=True)
103-
right.send_keys("htop", enter=True)
196+
### Capture pane output
104197

105-
print(f"✅ Session ready. Attach with: tmux attach -t {session_name}")
198+
```python
199+
>>> pane.clear()
200+
Pane(%... ...)
201+
>>> pane.send_keys("echo 'hello world'", enter=True)
202+
>>> pane.cmd('capture-pane', '-p').stdout # doctest: +SKIP
203+
["$ echo 'hello world'", 'hello world', '$']
204+
```
205+
206+
### Traverse the hierarchy
207+
208+
Navigate from pane up to window to session:
209+
210+
```python
211+
>>> pane.window
212+
Window(@... ...:..., Session($... ...))
213+
>>> pane.window.session
214+
Session($... ...)
106215
```
107216

108217
## Core concepts
@@ -132,15 +241,6 @@ pane.send_keys("echo 'hello from libtmux'", enter=True)
132241
| libtmux | Python API over tmux | Programmatic control, automation, testing |
133242
| tmuxp | App on top of libtmux | Declarative tmux workspaces from YAML / TOML |
134243

135-
## Raw tmux when you need it
136-
137-
```python
138-
server = libtmux.Server(socket_name="libtmux_doctest")
139-
result = server.cmd("display-message", "hello world")
140-
print(result.stdout) # ['hello world']
141-
print(result.returncode) # 0 on success
142-
```
143-
144244
## Testing & fixtures
145245

146246
- pytest plugin provides fresh tmux server/session/window/pane fixtures
@@ -158,21 +258,23 @@ print(result.returncode) # 0 on success
158258

159259
- Docs: [docs]
160260
- API reference: [api]
261+
- Architecture: [architecture]
161262
- Changelog: [history]
162263
- Migration notes: [migration]
163264
- Issues: [issues]
164265
- Test coverage: [coverage]
165266
- Releases: [releases]
166267
- License: [license]
167268
- Support: [support]
168-
- The Tao of tmux book: [tao]
269+
- **[The Tao of tmux][tao]** — deep-dive book on tmux fundamentals
169270

170271
## Contributing & support
171272

172273
Contributions are welcome. Please open an issue or PR if you find a bug or want to improve the API or docs. If libtmux helps you ship, consider sponsoring development via [support].
173274

174275
[docs]: https://libtmux.git-pull.com
175276
[api]: https://libtmux.git-pull.com/api.html
277+
[architecture]: https://libtmux.git-pull.com/about.html
176278
[history]: https://libtmux.git-pull.com/history.html
177279
[migration]: https://libtmux.git-pull.com/migration.html
178280
[issues]: https://github.com/tmux-python/libtmux/issues

0 commit comments

Comments
 (0)