File: examples.md

package info (click to toggle)
tmuxp 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,688 kB
  • sloc: python: 8,049; makefile: 202; sh: 14
file content (802 lines) | stat: -rw-r--r-- 14,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
(examples)=

# Examples

## Short hand / inline style

tmuxp has a short-hand syntax for those who wish to keep their workspace
punctual.

::::{sidebar} short hand

```{eval-rst}
.. aafig::
   :textual:

    +-------------------+
    | 'did you know'    |
    | 'you can inline'  |
    +-------------------+
    | 'single commands' |
    |                   |
    +-------------------+
    | 'for panes'       |
    |                   |
    +-------------------+
```

::::

````{tab} YAML

```{literalinclude} ../../examples/shorthands.yaml
:language: yaml

```


````

````{tab} JSON

```{literalinclude} ../../examples/shorthands.json
:language: json

```

````

## Blank panes

No need to repeat `pwd` or a dummy command. A `null`, `'blank'`,
`'pane'` are valid.

Note `''` counts as an empty carriage return.

````{tab} YAML

```{literalinclude} ../../examples/blank-panes.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/blank-panes.json
:language: json

```

````

## 2 panes

::::{sidebar} 2 pane

```{eval-rst}
.. aafig::

    +-----------------+
    | $ pwd           |
    |                 |
    |                 |
    +-----------------+
    | $ pwd           |
    |                 |
    |                 |
    +-----------------+
```

::::

````{tab} YAML

```{literalinclude} ../../examples/2-pane-vertical.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/2-pane-vertical.json
:language: json

```

````

## 3 panes

::::{sidebar} 3 panes

```{eval-rst}
.. aafig::

    +-----------------+
    | $ pwd           |
    |                 |
    |                 |
    +--------+--------+
    | $ pwd  | $ pwd  |
    |        |        |
    |        |        |
    +--------+--------+
```

::::

````{tab} YAML

```{literalinclude} ../../examples/3-pane.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/3-pane.json
:language: json

```

````

## 4 panes

::::{sidebar} 4 panes

```{eval-rst}
.. aafig::

    +--------+--------+
    | $ pwd  | $ pwd  |
    |        |        |
    |        |        |
    +--------+--------+
    | $ pwd  | $ pwd  |
    |        |        |
    |        |        |
    +--------+--------+
```

::::

````{tab} YAML

```{literalinclude} ../../examples/4-pane.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/4-pane.json
:language: json

```

````

## Start Directory

Equivalent to `tmux new-window -c <start-directory>`.

````{tab} YAML

```{literalinclude} ../../examples/start-directory.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/start-directory.json
:language: json

```

````

## Environment variable replacing

tmuxp will replace environment variables wrapped in curly brackets
for values of these settings:

- `start_directory`
- `before_script`
- `session_name`
- `window_name`
- `shell_command_before`
- `global_options`
- `options` in session scope and window scope

tmuxp replaces these variables before-hand with variables in the
terminal `tmuxp` invokes in.

In this case of this example, assuming the username "user":

```console
$ MY_ENV_VAR=foo tmuxp load examples/env-variables.yaml
```

and your session name will be `session - user (foo)`.

Shell variables in `shell_command` do not support this type of
concatenation. `shell_command` and `shell_command_before` both
support normal shell variables, since they are sent into panes
automatically via `send-key` in `tmux(1)`. See `ls $PWD` in
example.

If you have a special case and would like to see behavior changed,
please make a ticket on the [issue tracker][issue tracker].

[issue tracker]: https://github.com/tmux-python/tmuxp/issues

````{tab} YAML

```{literalinclude} ../../examples/env-variables.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/env-variables.json
:language: json

```

````

## Environment variables

tmuxp will set session, window and pane environment variables.

```{note}

Setting environment variables for windows and panes requires tmuxp 1.19 or
newer and tmux 3.0 or newer.
```

````{tab} YAML

```{literalinclude} ../../examples/session-environment.yaml
:language: yaml

```
````

````{tab} JSON

```{literalinclude} ../../examples/session-environment.json
:language: json

```

````

## Focusing

tmuxp allows `focus: true` for assuring windows and panes are attached /
selected upon loading.

````{tab} YAML

```{literalinclude} ../../examples/focus-window-and-panes.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/focus-window-and-panes.json
:language: json

```

````

## Terminal History

tmuxp allows `suppress_history: false` to override the default command /
suppression when building the workspace.
This will add the `shell_command` to the shell history in the pane.
The suppression of the `shell_command` commands from the shell's history
occurs by prefixing the commands with a space when `suppress_history: true`.
Accordingly, this functionality depends on the shell being appropriately
configured: bash requires the shell variable `HISTCONTROL` to be set and
include either of the values `ignorespace` or `ignoreboth` (to also ignore
sequential duplicate commands), and zsh requires `setopt HIST_IGNORE_SPACE`.

````{tab} YAML

```{literalinclude} ../../examples/suppress-history.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/suppress-history.json
:language: json

```

````

(enter)=

## Skip command execution

See more at {ref}`enter`.

:::{note}

_Experimental setting_: behavior and api is subject to change until stable.

:::

```{versionadded} 1.10.0
`enter: false` option. Pane-level support.
```

Omit sending {kbd}`enter` to key commands. Equivalent to
[`send_keys(enter=False)`](libtmux.Pane.send_keys).

````{tab} YAML

```{literalinclude} ../../examples/skip-send.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/skip-send.json
:language: json

```

````

````{tab} YAML (pane-level)

```{literalinclude} ../../examples/skip-send-pane-level.yaml
:language: yaml

```

````

````{tab} JSON (pane-level)

```{literalinclude} ../../examples/skip-send-pane-level.json
:language: json

```

````

(sleep)=

## Pausing commands

:::{note}

_Experimental setting_: behavior and api is subject to change until stable.

:::

```{versionadded} 1.10.0
`sleep_before` and `sleep_after` options added. Pane and command-level support.
```

```{warning}
**Blocking.** This will delay loading as it runs synchronously for each pane as [`asyncio`](asyncio)) is not implemented yet.
```

Omit sending {kbd}`enter` to key commands. Equivalent to having
a [`time.sleep`](time.sleep) before and after [`send_keys`](libtmux.Pane.send_keys).

This is especially useful for expensive commands where the terminal needs some breathing room (virtualenv, poetry, pipenv, uv, sourcing a configuration, launching a tui app, etc).

````{tab} Virtualenv

```{literalinclude} ../../examples/sleep-virtualenv.yaml
:language: yaml

```
````

````{tab} YAML

```{literalinclude} ../../examples/sleep.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/sleep.json
:language: json

```

````

````{tab} YAML (pane-level)

```{literalinclude} ../../examples/sleep-pane-level.yaml
:language: yaml

```

````

````{tab} JSON (pane-level)

```{literalinclude} ../../examples/sleep-pane-level.json
:language: json

```

````

## Window Index

You can specify a window's index using the `window_index` property. Windows
without `window_index` will use the lowest available window index.

````{tab} YAML

```{literalinclude} ../../examples/window-index.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/window-index.json
:language: json

```

````

## Shell per pane

Every pane can have its own shell or application started. This allows for usage
of the `remain-on-exit` setting to be used properly, but also to have
different shells on different panes.

````{tab} YAML

```{literalinclude} ../../examples/pane-shell.yaml
:language: yaml

```

````

````{tab} JSON

```{literalinclude} ../../examples/pane-shell.json
:language: json

```

````

## Set tmux options

Works with global (server-wide) options, session options
and window options.

Including `automatic-rename`, `default-shell`,
`default-command`, etc.

````{tab} YAML

```{literalinclude} ../../examples/options.yaml
:language: yaml

```
````

````{tab} JSON
```{literalinclude} ../../examples/options.json
:language: json

```
````

## Set window options after pane creation

Apply window options after panes have been created. Useful for
`synchronize-panes` option after executing individual commands in each
pane during creation.

````{tab} YAML
```{literalinclude} ../../examples/2-pane-synchronized.yaml
:language: yaml

```
````

````{tab} JSON
```{literalinclude} ../../examples/2-pane-synchronized.json
:language: json

```
````

## Main pane height

### Percentage

:::{versionadded} 1.46.0

Before this, tmuxp layouts would not detect the terminal's size.

:::

````{tab} YAML
```{literalinclude} ../../examples/main-pane-height-percentage.yaml
:language: yaml

```
````

````{tab} JSON
```{literalinclude} ../../examples/main-pane-height-percentage.json
:language: json

```
````

### Rows

````{tab} YAML
```{literalinclude} ../../examples/main-pane-height.yaml
:language: yaml

```
````

````{tab} JSON
```{literalinclude} ../../examples/main-pane-height.json
:language: json

```
````

## Super-advanced dev environment

:::{seealso}

{ref}`tmuxp-developer-config` in the {ref}`developing` section.

:::

````{tab} YAML
```{literalinclude} ../../.tmuxp.yaml
:language: yaml

```
````

````{tab} JSON
```{literalinclude} ../../.tmuxp.json
:language: json

```
````

## Multi-line commands

You can use YAML's multiline syntax to easily split multiple
commands into the same shell command: https://stackoverflow.com/a/21699210

````{tab} YAML
```yaml
session_name: my project
shell_command_before:
- >
  [ -d `.venv/bin/activate` ] &&
  source .venv/bin/activate &&
  reset
- sleep 1
windows:
- window_name: first window
  layout: main-horizontal
  focus: true
  panes:
  - focus: True
  - blank
  - >
    uv run ./manage.py migrate &&
    npm -C js run start
  - uv run ./manage.py runserver
  options:
    main-pane-height: 35
```
````

## Bootstrap project before launch

You can use `before_script` to run a script before the tmux session
starts building. This can be used to start a script to create a virtualenv
or download a virtualenv/rbenv/package.json's dependency files before
tmuxp even begins building the session.

It works by using the [Exit Status][exit status] code returned by a script. Your
script can be any type, including bash, python, ruby, etc.

A successful script will exit with a status of `0`.

Important: the script file must be chmod executable `+x` or `755`.

Run a python script (and check for it's return code), the script is
_relative to the `.tmuxp.yaml`'s root_ (Windows and panes omitted in
this example):

````{tab} YAML
```yaml
session_name: my session
before_script: ./bootstrap.py
# ... the rest of your workspace

```
````

````{tab} JSON
```json
{
    "session_name": "my session",
    "before_script": "./bootstrap.py"
}

```
````

Run a shell script + check for return code on an absolute path. (Windows
and panes omitted in this example)

````{tab} YAML

```yaml
session_name: another example
before_script: /absolute/path/this.sh # abs path to shell script
# ... the rest of your workspace

```
````

````{tab} JSON

```json
{
    "session_name": "my session",
    "before_script": "/absolute/path/this.sh"
}

```
````

[exit status]: http://tldp.org/LDP/abs/html/exit-status.html

## Per-project tmuxp workspaces

You can load your software project in tmux by placing a `.tmuxp.yaml` or
`.tmuxp.json` in the project's workspace and loading it.

tmuxp supports loading workspace via absolute filename with `tmuxp load`
and via `$ tmuxp load .` if workspace is in the directory.

```console

$ tmuxp load ~/workspaces/myproject.yaml

```

See examples of `tmuxp` in the wild. Have a project workspace to show off?
Edit this page.

- <https://github.com/tony/dockerfiles/blob/master/.tmuxp.yaml>
- <https://github.com/vcs-python/vcspull/blob/master/.tmuxp.yaml>
- <https://github.com/tony/sphinxcontrib-github/blob/master/.tmuxp.yaml>

You can use `start_directory: ./` to make the directories relative to
the workspace file / project root.

## Bonus: pipenv auto-bootstrapping

:::{versionadded} 1.3.4

`before_script` CWD's into the root (session)-level
`start_directory`.

:::

If you use [pipenv] / [poetry] / [uv], you can use a script like this to ensure
your packages are installed:

````{tab} YAML

```yaml
# assuming your .tmuxp.yaml is in your project root directory
session_name: my pipenv project
start_directory: ./
before_script: pipenv install --dev --skip-lock # ensure dev deps install
windows:
- window_name: django project
  focus: true
  panes:
  - blank
  - pipenv run ./manage.py runserver

```
````

You can also source yourself into the virtual environment using
`shell_command_before`:

````{tab} YAML

```yaml
# assuming your .tmuxp.yaml is in your project root directory
session_name: my pipenv project
start_directory: ./
before_script: pipenv install --dev --skip-lock # ensure dev deps install
shell_command_before:
- '[ -d `pipenv --venv` ] && source `pipenv --venv`/bin/activate && reset'
windows:
- window_name: django project
  focus: true
  panes:
  - blank
  - ./manage.py runserver

```
````

[pipenv]: https://docs.pipenv.org/
[poetry]: https://python-poetry.org/
[uv]: https://github.com/astral-sh/uv

## Kung fu

:::{note}

tmuxp sessions can be scripted in python. The first way is to use the
ORM in the {ref}`API`. The second is to pass a {py:obj}`dict` into
{class}`~tmuxp.workspace.builder.WorkspaceBuilder` with a correct schema.
See: {meth}`tmuxp.validation.validate_schema`.

:::

Add yours? Submit a pull request to the [github][github] site!

[github]: https://github.com/tmux-python/tmuxp