Core Library0.xGuides

Choose documentation 2

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

Create sessions, windows and panes

Needs libtmux for PowerShell 0.x Install

New-TmuxSession, New-TmuxWindow and Split-TmuxPane create tmux objects and emit a LibTmux.Session, LibTmux.Window or LibTmux.Pane. Each takes its owner directly or from the pipeline: a server for a session, a session for a window and a pane for a split. Read tmux objects shows how to choose the owner.

  • Preview: creation supports -WhatIf and -Confirm. Confirmation comes before discovery or dispatch, even when the first session would start a daemon. A preview emits no object.
  • Ownership: creating objects does not give the module the daemon or schedule teardown. Use an endpoint you control and remove what you create.

Create a detached session

The following command assumes $server is an endpoint you own. It creates a detached session with the requested window name and dimensions:

Terminal window
$session = $server |
New-TmuxSession -Name 'work' -WindowName 'editor' -Width 100 -Height 30
  • Without -Name, tmux chooses one. An existing name is an error: the command never replaces or attaches to a session.
  • Without dimensions, tmux’s defaults apply, and its window-size policy still governs them.
  • Names and working directories keep tmux’s format expansion, including the meaning of #.

Rename a session or window

Rename-TmuxSession and Rename-TmuxWindow take a handle from the pipeline, as a read such as Get-TmuxSession or Get-TmuxWindow returns it, and a new -Name. They emit nothing unless -PassThru asks for the replacement handle, which carries the new name. The handle you passed in keeps the name it captured, so assign the replacement when you need the new one.

A rename supports -WhatIf, which contacts nothing, and an empty name is rejected before tmux is called. As with creation, tmux expands the name as a format. The error IDs are Tmux.SessionRenameFailed and Tmux.WindowRenameFailed.

Create a window or split

Create a window at an explicit nonnegative index and make it current:

Terminal window
$window = $session | New-TmuxWindow -Name 'tools' -Index 5 -Activate

Without -Index, tmux chooses a free one; an occupied index is an error. A new window stays unselected unless -Activate is present, and selecting one does not attach your terminal.

Given a selected native $pane, place a new pane to its left and request twenty columns:

Terminal window
$newPane = $pane | Split-TmuxPane -Horizontal -Before -Size 20
  • Direction: a split puts the new pane below the target. -Horizontal makes a left/right split, and -Before puts the pane above or to the left.
  • Size: -Size takes positive cells and -Percentage takes 1 through 100. With neither, tmux’s default applies. The geometry must fit the window.
  • Shape: -FullWindow spans the whole window, and -Zoom zooms the new pane.
  • Selection: -Activate selects the new pane; otherwise the active pane stays.

Commands, directories and environment

All three cmdlets accept -Command, -StartDirectory and -Environment. -Command is one tmux shell-command string that tmux runs through its shell, so quote shell syntax deliberately and keep untrusted input out of it. Without it, tmux uses its default command or shell. For example:

Terminal window
$window = $session | New-TmuxWindow -Name 'shell' -Command 'exec /bin/sh'
  • -StartDirectory is a filesystem path that the core and tmux interpret. Use an absolute path. The core expands ~ and ~/. PowerShell provider paths are not resolved, your current directory does not change, and tmux may expand formats in the path.
  • -Environment takes a dictionary of string keys and values. Empty values are kept. Numeric and null values fail before dispatch with Tmux.InvalidCreation, as do empty names, names with = or NUL, and values with NUL. Validation applies to -WhatIf too.
  • Entries are copied before any owner is processed. They become tmux’s per-session or per-process environment and leave the PowerShell host environment alone. Spaces, quotes and semicolons in a value stay in that one argument.
Terminal window
$window = $session | New-TmuxWindow -Environment @{
APP_MODE = 'development'
OPTIONAL = ''
}

Failures and results

  • Success: emits the created object as the core captured it. Keep it when you need its metadata later; an existing owner handle is not refreshed in place.
  • Empty owner pipeline: emits nothing. Wrap a pipeline in @(...) when you need an array for zero, one or many results.
  • Errors: Tmux.SessionCreateFailed, Tmux.WindowCreateFailed and Tmux.PaneSplitFailed keep the original exception and the failed owner. Later owners continue under -ErrorAction Continue, and -ErrorAction Stop stops at the first error.
  • Cancellation: reaches the core operation. It does not roll back a creation or stop a command already running in a pane. No mutation is retried.
Esc
Search in

Type to search.

Reference previewOpen reference