PowerShellHome

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

libtmux for PowerShell

PowerShell-based tmux client

tmux client library for PowerShell. Control tmux, connect to and create servers, navigate sessions, windows, and panes, send keys, watch for changes via control mode, capture pane contents

Product

Control tmux from PowerShell code 0.x Released; the API may change. Read more Core Library home →

$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/modules/LibTmux/0.1.0/LibTmux.psd1"
# A private server: a socket and an empty config of its own, so this
# neither touches nor is changed by a tmux you already use.
# The process ID in the socket name keeps one run apart from the next.
$socket = 'libtmux-home-' + $PID
$server = New-TmuxServer -SocketName $socket -ConfigurationFile /dev/null
$session = $server | New-TmuxSession -Name demo
# Lay out the session: name its window, split it, add a second window.
$window = $session | Get-TmuxWindow | Where-Object Active
$editor = $window.RenameAsync('editor').GetAwaiter().GetResult()
$left = $editor | Get-TmuxPane | Where-Object Active
$right = $left | Split-TmuxPane -Horizontal
$logs = $session | New-TmuxWindow -Name logs
# Type a command into the new pane and wait for what it prints.
$right | Send-TmuxText -Text "printf 'hello from %s\n' tmux" -Enter
$found = $right | Wait-TmuxPaneText -Pattern 'hello from tmux' -Timeout 10
if (-not $found.Found) { throw 'the pane never showed the output' }
# Move the focus an attached client sees: the pane, then the window.
$request = [LibTmux.SelectPaneRequest]::new()
$right = $right.SelectAsync($request).GetAwaiter().GetResult()
$logs = $logs.SelectAsync().GetAwaiter().GetResult()
# Walk the objects: down from the server, then up from one pane.
$snapshot = $server | Get-TmuxSnapshot
foreach ($each in $snapshot.Sessions) {
"session $($each.Name)"
foreach ($window in $each.Windows) {
' window {0}:{1}: {2}' -f
$window.Index, $window.Name, ($window.Panes.Id -join ' ')
}
}
$pane = $snapshot.Panes | Where-Object Id -eq $right.Id
$window = $pane.Window
$owner = $pane.Session
"$($pane.Id) is in window $($window.Name) of session $($owner.Name)"
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/modules/LibTmux/0.1.0/LibTmux.psd1"
try {
# A private server: a socket and an empty config of its own, so this
# neither touches nor is changed by a tmux you already use.
# The process ID in the socket name keeps one run apart from the next.
$socket = 'libtmux-home-' + $PID
$server = New-TmuxServer -SocketName $socket -ConfigurationFile /dev/null
$session = $server | New-TmuxSession -Name demo
# Lay out the session: name its window, split it, add a second window.
$window = $session | Get-TmuxWindow | Where-Object Active
$editor = $window.RenameAsync('editor').GetAwaiter().GetResult()
$left = $editor | Get-TmuxPane | Where-Object Active
$right = $left | Split-TmuxPane -Horizontal
$logs = $session | New-TmuxWindow -Name logs
# Type a command into the new pane and wait for what it prints.
$right | Send-TmuxText -Text "printf 'hello from %s\n' tmux" -Enter
$found = $right | Wait-TmuxPaneText -Pattern 'hello from tmux' -Timeout 10
if (-not $found.Found) { throw 'the pane never showed the output' }
# Move the focus an attached client sees: the pane, then the window.
$request = [LibTmux.SelectPaneRequest]::new()
$right = $right.SelectAsync($request).GetAwaiter().GetResult()
$logs = $logs.SelectAsync().GetAwaiter().GetResult()
# Walk the objects: down from the server, then up from one pane.
$snapshot = $server | Get-TmuxSnapshot
foreach ($each in $snapshot.Sessions) {
"session $($each.Name)"
foreach ($window in $each.Windows) {
' window {0}:{1}: {2}' -f
$window.Index, $window.Name, ($window.Panes.Id -join ' ')
}
}
$pane = $snapshot.Panes | Where-Object Id -eq $right.Id
$window = $pane.Window
$owner = $pane.Session
"$($pane.Id) is in window $($window.Name) of session $($owner.Name)"
} catch {
$failure = [InvalidOperationException]::new(
"tmux quickstart failed: $($_.Exception.Message)", $_.Exception)
throw $failure
}
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/modules/LibTmux/0.1.0/LibTmux.psd1"
$session = $null
$failure = $null
try {
# A private server: a socket and an empty config of its own, so this
# neither touches nor is changed by a tmux you already use.
# The process ID in the socket name keeps one run apart from the next.
$socket = 'libtmux-home-' + $PID
$server = New-TmuxServer -SocketName $socket -ConfigurationFile /dev/null
$session = $server | New-TmuxSession -Name demo
# Lay out the session: name its window, split it, add a second window.
$window = $session | Get-TmuxWindow | Where-Object Active
$editor = $window.RenameAsync('editor').GetAwaiter().GetResult()
$left = $editor | Get-TmuxPane | Where-Object Active
$right = $left | Split-TmuxPane -Horizontal
$logs = $session | New-TmuxWindow -Name logs
# Type a command into the new pane and wait for what it prints.
$right | Send-TmuxText -Text "printf 'hello from %s\n' tmux" -Enter
$found = $right | Wait-TmuxPaneText -Pattern 'hello from tmux' -Timeout 10
if (-not $found.Found) { throw 'the pane never showed the output' }
# Move the focus an attached client sees: the pane, then the window.
$request = [LibTmux.SelectPaneRequest]::new()
$right = $right.SelectAsync($request).GetAwaiter().GetResult()
$logs = $logs.SelectAsync().GetAwaiter().GetResult()
# Walk the objects: down from the server, then up from one pane.
$snapshot = $server | Get-TmuxSnapshot
foreach ($each in $snapshot.Sessions) {
"session $($each.Name)"
foreach ($window in $each.Windows) {
' window {0}:{1}: {2}' -f
$window.Index, $window.Name, ($window.Panes.Id -join ' ')
}
}
$pane = $snapshot.Panes | Where-Object Id -eq $right.Id
$window = $pane.Window
$owner = $pane.Session
"$($pane.Id) is in window $($window.Name) of session $($owner.Name)"
} catch {
$failure = $_.Exception
throw
} finally {
if ($session) {
$cleanupErrors = @()
$session | Remove-TmuxSession -Confirm:$false `
-ErrorAction SilentlyContinue -ErrorVariable cleanupErrors
if ($cleanupErrors) {
if ($failure) {
$errors = @($failure) + $cleanupErrors.Exception
throw [AggregateException]::new(
'tmux operation and cleanup failed.',
[Exception[]]$errors)
}
throw $cleanupErrors[0]
}
}
}
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/modules/LibTmux/0.1.0/LibTmux.psd1"
$session = $null
$failure = $null
try {
# A private server: a socket and an empty config of its own, so this
# neither touches nor is changed by a tmux you already use.
# The process ID in the socket name keeps one run apart from the next.
$socket = 'libtmux-home-' + $PID
$server = New-TmuxServer -SocketName $socket -ConfigurationFile /dev/null
$session = $server | New-TmuxSession -Name demo
# Lay out the session: name its window, split it, add a second window.
$window = $session | Get-TmuxWindow | Where-Object Active
$editor = $window.RenameAsync('editor').GetAwaiter().GetResult()
$left = $editor | Get-TmuxPane | Where-Object Active
$right = $left | Split-TmuxPane -Horizontal
$logs = $session | New-TmuxWindow -Name logs
# Type a command into the new pane and wait for what it prints.
$right | Send-TmuxText -Text "printf 'hello from %s\n' tmux" -Enter
$found = $right | Wait-TmuxPaneText -Pattern 'hello from tmux' -Timeout 10
if (-not $found.Found) { throw 'the pane never showed the output' }
# Move the focus an attached client sees: the pane, then the window.
$request = [LibTmux.SelectPaneRequest]::new()
$right = $right.SelectAsync($request).GetAwaiter().GetResult()
$logs = $logs.SelectAsync().GetAwaiter().GetResult()
# Walk the objects: down from the server, then up from one pane.
$snapshot = $server | Get-TmuxSnapshot
foreach ($each in $snapshot.Sessions) {
"session $($each.Name)"
foreach ($window in $each.Windows) {
' window {0}:{1}: {2}' -f
$window.Index, $window.Name, ($window.Panes.Id -join ' ')
}
}
$pane = $snapshot.Panes | Where-Object Id -eq $right.Id
$window = $pane.Window
$owner = $pane.Session
"$($pane.Id) is in window $($window.Name) of session $($owner.Name)"
} catch {
$failure = [InvalidOperationException]::new(
"tmux quickstart failed: $($_.Exception.Message)", $_.Exception)
throw $failure
} finally {
if ($session) {
$cleanupErrors = @()
$session | Remove-TmuxSession -Confirm:$false `
-ErrorAction SilentlyContinue -ErrorVariable cleanupErrors
if ($cleanupErrors) {
if ($failure) {
$errors = @($failure) + $cleanupErrors.Exception
throw [AggregateException]::new(
'tmux operation and cleanup failed.',
[Exception[]]$errors)
}
throw $cleanupErrors[0]
}
}
}
Example details
No tmux server yet

tmux before the first call: no server is running.

No tmux server yet

tmux after New-TmuxServer: no server is running; the call sent no command.

%0 sh
[demo]

tmux after New-TmuxSession: added session demo.

%0 sh
[demo]

tmux after Get-TmuxWindow: unchanged.

%0 sh
[demo]

tmux after Window.RenameAsync: renamed window 0:sh to 0:editor.

%0 sh
[demo]

tmux after Get-TmuxPane: unchanged.

%0 sh
%1 sh
[demo]

tmux after Split-TmuxPane: added pane %1 to window 0:editor.

%0 sh
%1 sh
[demo]

tmux after New-TmuxWindow: added window 1:logs.

%0 sh
%1 sh
[demo]

tmux after Send-TmuxText: sent keys to pane %1.

%0 sh
%1 sh
$ printf 'hello from %s\n' tmux
hello from tmux
$
[demo]

tmux after Wait-TmuxPaneText: read pane %1.

%0 sh
%1 sh
$ printf 'hello from %s\n' tmux
hello from tmux
$
[demo]

tmux after Pane.SelectAsync: selected pane %1 in window 0:editor.

%2 sh
[demo]

tmux after Window.SelectAsync: selected window 1:logs.

%2 sh
[demo]

tmux after Get-TmuxSnapshot: unchanged.

%2 sh
[demo]

tmux after Server.Panes: unchanged, the call sent no command.

%2 sh
[demo]

tmux after Pane.Window: unchanged, the call sent no command.

%2 sh
[demo]

tmux after Pane.Session: unchanged, the call sent no command.

No sessions

tmux after Remove-TmuxSession: the server has exited.

  1. Before the first call

    No tmux server is running yet.

  2. New-TmuxServer returned Server

    {
    "socketName": "libtmux-home-12345",
    "configFile": "/dev/null"
    }

    The call sent no tmux command.

  3. New-TmuxSession returned Session

    {
    "id": "$0",
    "name": "demo"
    }
    3 tmux commands, 2 that change tmux
    1. new-session -P -F '#{pid}:#{start_time} #{session_id} #{window_id} #{pane_id} #{window_index}' -d -s demo 2693433:1794972755 $0 @0 %0 0
    2. attach-session -f ignore-size,no-output,new-layouts no output
    3. display-message -p -t $0 #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_ind… /dev/nullLTa755737243064b7d82612eLTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e0LTa755737243064b7d82612e1794972755LTa755737243064b7d82612eLTa75573…

    Before these, the library asked tmux about itself 4 times (its version, its daemon, how a control client reads escapes).

    1 notification from tmux while the call ran
    1. %session-changed $0 demo
  4. Get-TmuxWindow returned Window

    {
    "id": "@0",
    "index": 0,
    "name": "sh"
    }
    1 tmux command
    1. list-windows -t $0 -F #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_index}… /dev/nullLTa755737243064b7d82612e1LTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e0LTa755737243064b7d82612e1794972755LTa755737243064b7d82612eLTa7557…
  5. Window.RenameAsync returned Window

    {
    "id": "@0",
    "index": 0,
    "name": "editor"
    }
    2 tmux commands, 1 that changes tmux
    1. rename-window -t @0 -- editor no output
    2. display-message -p -t $0:0 #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_i… /dev/nullLTa755737243064b7d82612eLTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e0LTa755737243064b7d82612e1794972755LTa755737243064b7d82612eLTa75573…
    1 notification from tmux while the call ran
    1. %window-renamed @0 editor
  6. Get-TmuxPane returned Pane

    {
    "id": "%0"
    }
    1 tmux command
    1. list-panes -t $0:0 -F #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_index}… /dev/nullLTa755737243064b7d82612e1LTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e0LTa755737243064b7d82612e1794972755LTa755737243064b7d82612eLTa7557…
  7. Split-TmuxPane returned Pane

    {
    "id": "%1"
    }
    2 tmux commands, 1 that changes tmux
    1. split-window -P -F #{pane_id} -t %0 -h -d %1
    2. display-message -p -t %1 #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_ind… /dev/nullLTa755737243064b7d82612eLTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e0LTa755737243064b7d82612e1794972755LTa755737243064b7d82612eLTa75573…
    1 notification from tmux while the call ran
    1. %layout-change @0 {"V":2,"L":{"t":"h","w":80,"h":24,"x":0,"y":0,"c":[{"t":"p","w":40,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%0"},{"t":"p","w":39,"h":24,"x":41,"y":0,"i":1,"I":"%1"}]}} {"V":2,"L":{"t":"h","w":80,"h":24,"x":0,"y":0,"c":[{"t":"p","w":40,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%0"},{"t":"p","w":39,"h":24,"x":41,"y":0,"i":1,"I":"%1"}]}} *
  8. New-TmuxWindow returned Window

    {
    "id": "@1",
    "index": 1,
    "name": "logs"
    }
    2 tmux commands, 1 that changes tmux
    1. new-window -P -F #{window_id} -d -t $0: -n logs @1
    2. list-windows -t $0 -F #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_index}… /dev/nullLTa755737243064b7d82612e2LTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e1LTa755737243064b7d82612e1794972755LTa755737243064b7d82612eLTa7557…
    1 notification from tmux while the call ran
    1. %window-add @1
  9. Send-TmuxText returned void

    null
    2 tmux commands, 2 that change tmux
    1. send-keys -t %1 -l -- 'printf '\''hello from %s\n'\'' tmux' no output
    2. send-keys -t %1 Enter no output
  10. Wait-TmuxPaneText returned PaneWaitResult

    {
    "outcome": "PresentAtEntry",
    "found": true
    }
    13 tmux commands, 4 that change tmux
    1. attach-session -t $0 no output
    2. refresh-client -f new-layouts no output
    3. refresh-client -f ignore-size no output
    4. refresh-client -B libtmux-pane-dead:%*:#{pane_dead} no output
    5. list-panes -t $0:.%1 -F #{pane_id} -f #{==:#{pane_id},%1} %1
    6. display-message -t %1 -p -- '#{pane_pid} #{history_size} #{history_limit} #{pane_height} #{cursor_y} #{pane_dead} #{alternate_on} #{pane_width}' 2693947 0 2000 24 2 0 0 39
    7. capture-pane -t %1 -p $ printf 'hello from %s\n' tmux hello from tmux $
    8. capture-pane -t %1 -p -J $ printf 'hello from %s\n' tmux hello from tmux $
    9. display-message -t %1 -p -- '#{pane_pid} #{history_size} #{history_limit} #{pane_height} #{cursor_y} #{pane_dead} #{alternate_on} #{pane_width}' 2693947 0 2000 24 2 0 0 39
    10. display-message -t %1 -p -- '#{pane_pid} #{history_size} #{history_limit} #{pane_height} #{cursor_y} #{pane_dead} #{alternate_on} #{pane_width}' 2693947 0 2000 24 2 0 0 39
    11. capture-pane -t %1 -p $ printf 'hello from %s\n' tmux hello from tmux $
    12. capture-pane -t %1 -p -J $ printf 'hello from %s\n' tmux hello from tmux $
    13. display-message -t %1 -p -- '#{pane_pid} #{history_size} #{history_limit} #{pane_height} #{cursor_y} #{pane_dead} #{alternate_on} #{pane_width}' 2693947 0 2000 24 2 0 0 39

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

    4 notifications from tmux while the call ran
    1. %client-session-changed client-2694388 $0 demo
    2. %session-changed $0 demo
    3. %client-detached client-2694388
    4. %exit
  11. Pane.SelectAsync returned Pane

    {
    "id": "%1"
    }
    2 tmux commands, 1 that changes tmux
    1. select-pane -t %1 no output
    2. display-message -p -t $0:.%1 #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window… /dev/nullLTa755737243064b7d82612eLTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e0LTa755737243064b7d82612e1LTa755737243064b7d82612e1794972758LTa755737243064b7d82612eLTa75573…
    1 notification from tmux while the call ran
    1. %window-pane-changed @0 %1
  12. Window.SelectAsync returned Window

    {
    "id": "@1",
    "index": 1,
    "name": "logs"
    }
    2 tmux commands, 1 that changes tmux
    1. select-window -t @1 no output
    2. display-message -p -t $0:1 #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_i… /dev/nullLTa755737243064b7d82612eLTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e1LTa755737243064b7d82612e1LTa755737243064b7d82612e1794972758LTa755737243064b7d82612eLTa75573…
    1 notification from tmux while the call ran
    1. %session-window-changed $0 @1
  13. Get-TmuxSnapshot returned Server

    {
    "sessions": 1,
    "windows": 2,
    "panes": 3
    }
    3 tmux commands
    1. list-sessions -F #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_index}LTa75… /dev/nullLTa755737243064b7d82612e0LTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e1LTa755737243064b7d82612e1LTa755737243064b7d82612e1794972758LTa755737243064b7d82612eLTa7557…
    2. list-windows -a -F #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_index}LTa… /dev/nullLTa755737243064b7d82612e2LTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e1LTa755737243064b7d82612e1LTa755737243064b7d82612e1794972758LTa755737243064b7d82612eLTa7557…
    3. list-panes -a -F #{config_files}LTa755737243064b7d82612e#{line}LTa755737243064b7d82612e#{next_session_id}LTa755737243064b7d82612e#{pid}LTa755737243064b7d82612e#{socket_path}LTa755737243064b7d82612e#{start_time}LTa755737243064b7d82612e#{uid}LTa755737243064b7d82612e#{user}LTa755737243064b7d82612e#{version}LTa755737243064b7d82612e#{active_window_index}LTa755737243064b7d82612e#{last_window_index}LTa75… /dev/nullLTa755737243064b7d82612e2LTa755737243064b7d82612e$1LTa755737243064b7d82612e2693433LTa755737243064b7d82612e/tmp/tmux-1000/libtmux-home-12345LTa755737243064b7d82612e1794972755LTa755737243064b7d82612e1000LTa755737243064b7d82612edLTa755737243064b7d82612e3.8aLTa755737243064b7d82612e1LTa755737243064b7d82612e1LTa755737243064b7d82612e1794972758LTa755737243064b7d82612eLTa7557…

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

  14. Server.Panes returned Pane

    {
    "id": "%1"
    }

    The call sent no tmux command.

  15. Pane.Window returned Window

    {
    "id": "@0",
    "index": 0,
    "name": "editor"
    }

    The call sent no tmux command.

  16. Pane.Session returned Session

    {
    "id": "$0",
    "name": "demo"
    }

    The call sent no tmux command.

  17. Remove-TmuxSession returned void

    {
    "errors": 0
    }
    1 tmux command, 1 that changes tmux
    1. kill-session -t $0 2693433:1794972755
    1 notification from tmux while the call ran
    1. %exit
1/15
Run this example

PowerShell 7.4.20 with PSResourceGet 1.1.1, POSIX shell and tmux on Linux.

Save the program above as quickstart.ps1 in a new directory, along with the files below.

Cleanup is omitted; the private tmux server and its temporary files may remain. Select Cleanup to remove them when the program finishes. The program stops its private tmux server and removes any temporary files it created when it finishes. Error handling adds diagnostics; every version checks required results and deadlines.

Setup and run

pwsh -NoLogo -NoProfile -Command '
$ErrorActionPreference = "Stop"
$null = New-Item -ItemType Directory -Path ./modules -Force
Save-PSResource -Name LibTmux -Version 0.1.0 `
-Path ./modules -TrustRepository
'
unset TMUX TMUX_PANE
export ENV=/dev/null BASH_ENV=/dev/null SHELL=/bin/sh
pwsh -NoLogo -NoProfile -File quickstart.ps1

Expected output

session demo
window 0:editor: %0 %1
window 1:logs: %2
%1 is in window editor of session demo

Install LibTmux

From Git
PowerShell
PowerShell
Terminal
LibTmux 0.1.0 0.x PowerShell Gallery
PS> Install-Module -Name LibTmux
PS> Install-PSResource -Name LibTmux
$ git clone --branch v0.1.0 --depth 1 https://github.com/libtmux/libtmux-powershell.git
pwsh -NoProfile -File libtmux-powershell/eng/Build.ps1 -Restore

Requires PowerShell 7.4 or later and tmux on the same Unix host.

Requires PowerShell 7.4 or later and tmux on the same Unix host.

Pins the tagged source; the registry release is the default.

Workspace Manager

Load a workspace file concept Working example; may change without notice. Read more Workspace Manager home →

Preset
Main-horizontal layoutShort-hand panesTwo panesThree panesFour panesSetup before each paneEnvironmentStart directorySynchronized panesMain pane height as a percentageMinimal workspacetmux optionsSkip Enter for a commandSkip Enter for a paneWindow index

Main-horizontal layout

The main-horizontal layout gives the first pane the top of the window and splits the rest below it.

session_name: test window options
start_directory: '~'
windows:
- layout: main-horizontal
options:
main-pane-height: 5
panes:
- pane
- pane
- pane
window_name: editor
{
"session_name": "test window options",
"start_directory": "~",
"windows": [
{
"layout": "main-horizontal",
"options": {
"main-pane-height": 5
},
"panes": [
"pane",
"pane",
"pane"
],
"window_name": "editor"
}
]
}
Window 0, editor: 3 panes, main-horizontal
layout main-horizontal

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-window_options.yaml. Tested by window options fixture (tests/WorkspaceCorpus.Tests.ps1; a test runs the command).

Short-hand panes

A pane can be one command written as a string, and a command list can be one string.

session_name: shorthands
windows:
- window_name: long form
panes:
- shell_command:
- echo 'did you know'
- echo 'you can inline'
- shell_command: echo 'single commands'
- echo 'for panes'
{
"session_name": "shorthands",
"windows": [
{
"window_name": "long form",
"panes": [
{
"shell_command": [
"echo 'did you know'",
"echo 'you can inline'"
]
},
{
"shell_command": "echo 'single commands'"
},
"echo 'for panes'"
]
}
]
}
Window 0, long form: 3 panes, even-vertical❯ echo 'did you know'❯ echo 'you can inline'❯ echo 'single commands'❯ echo 'for panes'
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-shorthands.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Two panes

A window of two panes.

session_name: 2-pane-vertical
windows:
- window_name: my test window
panes:
- echo hello
- echo hello
{
"session_name": "2-pane-vertical",
"windows": [
{
"window_name": "my test window",
"panes": [
"echo hello",
"echo hello"
]
}
]
}
Window 0, my test window: 2 panes, even-vertical❯ echo hello❯ echo hello
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-2-pane-vertical.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Three panes

A window of three panes.

session_name: 3-panes
windows:
- window_name: dev window
layout: main-vertical
shell_command_before:
- cd ~/
panes:
- shell_command:
- cd /var/log
- ls -al | grep \.log
- echo hello
- echo hello
{
"session_name": "3-panes",
"windows": [
{
"window_name": "dev window",
"layout": "main-vertical",
"shell_command_before": [
"cd ~/"
],
"panes": [
{
"shell_command": [
"cd /var/log",
"ls -al | grep \\.log"
]
},
"echo hello",
"echo hello"
]
}
]
}
Window 0, dev window: 3 panes, main-vertical❯ cd /var/log❯ ls -al | grep \.log❯ echo hello❯ echo hello
layout main-vertical

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-3-pane.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Four panes

A window of four panes.

session_name: 4-pane-split
windows:
- window_name: dev window
layout: tiled
shell_command_before:
- cd ~/
panes:
- shell_command:
- cd /var/log
- ls -al | grep \.log
- echo hello
- echo hello
- echo hello
{
"session_name": "4-pane-split",
"windows": [
{
"window_name": "dev window",
"layout": "tiled",
"shell_command_before": [
"cd ~/"
],
"panes": [
{
"shell_command": [
"cd /var/log",
"ls -al | grep \\.log"
]
},
"echo hello",
"echo hello",
"echo hello"
]
}
]
}
Window 0, dev window: 4 panes, tiled❯ cd /var/log❯ ls -al | grep \.log❯ echo hello❯ echo hello❯ echo hello
layout tiled

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-4-pane.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Setup before each pane

shell_command_before runs ahead of a pane's own commands, at the session, window or pane level.

session_name: corpus
start_directory: ${PROJECT_ROOT}
options:
default-command: exec /bin/sh
'@corpus-session': yaml-original
environment:
CORPUS_SESSION: yaml-root
CORPUS_LAYER: session
shell_command_before:
- cmd: echo session >> order.txt
windows:
- window_name: yaml-editor
start_directory: window
layout: even-horizontal
focus: true
options:
'@corpus-window': '${OPTION_TAG}'
environment:
CORPUS_WINDOW: yaml-window
CORPUS_LAYER: window
shell_command_before:
- cmd: echo window >> order.txt
panes:
- shell_command:
- cmd: echo command >> order.txt
- 'echo "$CORPUS_SESSION|$CORPUS_WINDOW|$CORPUS_PANE|$CORPUS_LAYER" > environment.txt'
- tmux wait-for -S corpus-yaml-first
- start_directory: pane
focus: true
options:
'@corpus-pane': yaml
environment:
CORPUS_PANE: yaml-pane
CORPUS_LAYER: pane
shell_command_before:
- cmd: echo pane >> order.txt
shell_command:
- echo command >> order.txt
- 'echo "$CORPUS_SESSION|$CORPUS_WINDOW|$CORPUS_PANE|$CORPUS_LAYER" > environment.txt'
- tmux wait-for -S corpus-yaml-second
{
"session_name": "corpus",
"start_directory": "${PROJECT_ROOT}",
"options": {
"default-command": "exec /bin/sh",
"@corpus-session": "yaml-original"
},
"environment": {
"CORPUS_SESSION": "yaml-root",
"CORPUS_LAYER": "session"
},
"shell_command_before": [
{
"cmd": "echo session >> order.txt"
}
],
"windows": [
{
"window_name": "yaml-editor",
"start_directory": "window",
"layout": "even-horizontal",
"focus": true,
"options": {
"@corpus-window": "${OPTION_TAG}"
},
"environment": {
"CORPUS_WINDOW": "yaml-window",
"CORPUS_LAYER": "window"
},
"shell_command_before": [
{
"cmd": "echo window >> order.txt"
}
],
"panes": [
{
"shell_command": [
{
"cmd": "echo command >> order.txt"
},
"echo \"$CORPUS_SESSION|$CORPUS_WINDOW|$CORPUS_PANE|$CORPUS_LAYER\" > environment.txt",
"tmux wait-for -S corpus-yaml-first"
]
},
{
"start_directory": "pane",
"focus": true,
"options": {
"@corpus-pane": "yaml"
},
"environment": {
"CORPUS_PANE": "yaml-pane",
"CORPUS_LAYER": "pane"
},
"shell_command_before": [
{
"cmd": "echo pane >> order.txt"
}
],
"shell_command": [
"echo command >> order.txt",
"echo \"$CORPUS_SESSION|$CORPUS_WINDOW|$CORPUS_PANE|$CORPUS_LAYER\" > environment.txt",
"tmux wait-for -S corpus-yaml-second"
]
}
]
}
]
}
Window 0, yaml-editor: 2 panes, even-horizontal❯ echo command >> order.txt❯ echo "$CORPUS_SESSION|$CORPUS_WINDOW|$CORPUS_PANE|$CORPUS_LAYER" > environment.txt❯ tmux wait-for -S corpus-yaml-first❯ echo command >> order.txt❯ echo "$CORPUS_SESSION|$CORPUS_WINDOW|$CORPUS_PANE|$CORPUS_LAYER" > environment.txt❯ tmux wait-for -S corpus-yaml-second
layout even-horizontal

Workspace file: tests/fixtures/workspace/corpus.yaml. Tested by corpus fixture (tests/WorkspaceCorpus.Tests.ps1; a test runs the command).

Environment

environment sets variables for a session, a window or a pane.

session_name: test env vars
start_directory: "~"
environment:
FOO: SESSION
PATH: /tmp
windows:
- window_name: no_overrides
panes:
- pane
- window_name: window_overrides
environment:
FOO: WINDOW
panes:
- pane
- window_name: pane_overrides
panes:
- environment:
FOO: PANE
- window_name: both_overrides
environment:
FOO: WINDOW
panes:
- pane
- environment:
FOO: PANE
# This test case it just needed for warnings issued in old versions of tmux.
- window_name: both_overrides_on_first_pane
environment:
FOO: WINDOW
panes:
- environment:
FOO: PANE
{
"session_name": "test env vars",
"start_directory": "~",
"environment": {
"FOO": "SESSION",
"PATH": "/tmp"
},
"windows": [
{
"window_name": "no_overrides",
"panes": [
"pane"
]
},
{
"window_name": "window_overrides",
"environment": {
"FOO": "WINDOW"
},
"panes": [
"pane"
]
},
{
"window_name": "pane_overrides",
"panes": [
{
"environment": {
"FOO": "PANE"
}
}
]
},
{
"window_name": "both_overrides",
"environment": {
"FOO": "WINDOW"
},
"panes": [
"pane",
{
"environment": {
"FOO": "PANE"
}
}
]
},
{
"window_name": "both_overrides_on_first_pane",
"environment": {
"FOO": "WINDOW"
},
"panes": [
{
"environment": {
"FOO": "PANE"
}
}
]
}
]
}
Window 0, no_overrides: 1 pane, even-vertical
no layout set
Window 1, window_overrides: 1 pane, even-vertical
no layout set
Window 2, pane_overrides: 1 pane, even-vertical
no layout set
Window 3, both_overrides: 2 panes, even-vertical
no layout set
Window 4, both_overrides_on_first_pane: 1 pane, even-vertical
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-environment_vars.yaml. Tested by environment variables fixture (tests/WorkspaceCorpus.Tests.ps1; a test runs the command).

Start directory

start_directory chooses where a session, a window or a pane starts.

session_name: sample workspace
windows:
- panes:
- start_directory: /usr
- start_directory: /etc
{
"session_name": "sample workspace",
"windows": [
{
"panes": [
{
"start_directory": "/usr"
},
{
"start_directory": "/etc"
}
]
}
]
}
Window 0: 2 panes, even-vertical
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-first_pane_start_directory.yaml. Tested by first pane start directory fixture (tests/WorkspaceCorpus.Tests.ps1; a test runs the command).

Synchronized panes

options_after sets synchronize-panes once the panes exist, so typing in one pane reaches the others.

session_name: 2-pane-synchronized
windows:
- window_name: Two synchronized panes
panes:
- ssh server1
- ssh server2
options_after:
synchronize-panes: on
{
"session_name": "2-pane-synchronized",
"windows": [
{
"window_name": "Two synchronized panes",
"panes": [
"ssh server1",
"ssh server2"
],
"options_after": {
"synchronize-panes": "on"
}
}
]
}
Window 0, Two synchronized panes: 2 panes, even-vertical❯ ssh server1❯ ssh server2
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-2-pane-synchronized.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Main pane height as a percentage

main-pane-height takes a percentage of the window as well as a row count.

session_name: main-pane-height
start_directory: "~"
windows:
- layout: main-horizontal
options:
main-pane-height: 67%
panes:
- shell_command:
- top
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
window_name: my window name
{
"session_name": "main-pane-height",
"start_directory": "~",
"windows": [
{
"layout": "main-horizontal",
"options": {
"main-pane-height": "67%"
},
"panes": [
{
"shell_command": [
"top"
],
"start_directory": "~"
},
{
"shell_command": [
"echo \"hey\""
]
},
{
"shell_command": [
"echo \"moo\""
]
}
],
"window_name": "my window name"
}
]
}
Window 0, my window name: 3 panes, main-horizontal❯ top❯ echo "hey"❯ echo "moo"
layout main-horizontal

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-main-pane-height-percentage.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Minimal workspace

A session name and one window with one blank pane.

session_name: My tmux session
windows:
- panes:
-
{
"session_name": "My tmux session",
"windows": [
{
"panes": [
null
]
}
]
}
Window 0: 1 pane, even-vertical
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-minimal.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

tmux options

global_options, options and a window's options set tmux options.

session_name: test window options
start_directory: "~"
global_options:
default-shell: /bin/sh
default-command: /bin/sh
options:
main-pane-height: ${MAIN_PANE_HEIGHT} # works with env variables
windows:
- layout: main-horizontal
options:
automatic-rename: on
panes:
- shell_command:
- man echo
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
{
"session_name": "test window options",
"start_directory": "~",
"global_options": {
"default-shell": "/bin/sh",
"default-command": "/bin/sh"
},
"options": {
"main-pane-height": "${MAIN_PANE_HEIGHT}"
},
"windows": [
{
"layout": "main-horizontal",
"options": {
"automatic-rename": "on"
},
"panes": [
{
"shell_command": [
"man echo"
],
"start_directory": "~"
},
{
"shell_command": [
"echo \"hey\""
]
},
{
"shell_command": [
"echo \"moo\""
]
}
]
}
]
}
Window 0: 3 panes, main-horizontal❯ man echo❯ echo "hey"❯ echo "moo"
layout main-horizontal

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-options.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Skip Enter for a command

enter: false types a command without running it.

session_name: Skip command execution (command-level)
windows:
- panes:
- shell_command:
# You can see this
- echo "___$((11 + 1))___"
# This is skipped
- cmd: echo "___$((1 + 3))___"
enter: false
{
"session_name": "Skip command execution (command-level)",
"windows": [
{
"panes": [
{
"shell_command": [
"echo \"___$((11 + 1))___\"",
{
"cmd": "echo \"___$((1 + 3))___\"",
"enter": false
}
]
}
]
}
]
}
Window 0: 1 pane, even-vertical❯ echo "___$((11 + 1))___"❯ echo "___$((1 + 3))___"
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-skip-send.yaml. Tested by skip-send fixture (tests/WorkspaceCorpus.Tests.ps1; built by the library in a test; the command is not run).

Skip Enter for a pane

enter: false on a pane types its commands without running them.

session_name: Skip command execution (pane-level)
windows:
- panes:
- shell_command: echo "___$((1 + 3))___"
enter: false
- shell_command:
- echo "___$((1 + 3))___"\;
- echo "___$((1 + 3))___"
enter: false
{
"session_name": "Skip command execution (pane-level)",
"windows": [
{
"panes": [
{
"shell_command": "echo \"___$((1 + 3))___\"",
"enter": false
},
{
"shell_command": [
"echo \"___$((1 + 3))___\"\\;",
"echo \"___$((1 + 3))___\""
],
"enter": false
}
]
}
]
}
Window 0: 2 panes, even-vertical❯ echo "___$((1 + 3))___"❯ echo "___$((1 + 3))___"\;❯ echo "___$((1 + 3))___"
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-skip-send-pane-level.yaml. Tested by skip-send pane-level fixture (tests/WorkspaceCorpus.Tests.ps1; built by the library in a test; the command is not run).

Window index

window_index places a window at an index; a window without one takes the lowest free index.

session_name: Window index example
windows:
- window_name: zero
panes:
- echo "this window's index will be zero"
- window_name: five
panes:
- echo "this window's index will be five"
window_index: 5
- window_name: one
panes:
- echo "this window's index will be one"
{
"session_name": "Window index example",
"windows": [
{
"window_name": "zero",
"panes": [
"echo \"this window's index will be zero\""
]
},
{
"window_name": "five",
"panes": [
"echo \"this window's index will be five\""
],
"window_index": 5
},
{
"window_name": "one",
"panes": [
"echo \"this window's index will be one\""
]
}
]
}
Window 0, zero: 1 pane, even-vertical❯ echo "this window's index will be zero"
no layout set
Window 1, one: 1 pane, even-vertical❯ echo "this window's index will be one"
no layout set
Window 5, five: 1 pane, even-vertical❯ echo "this window's index will be five"
no layout set

Workspace file: tests/fixtures/workspace/tmuxp-v1.74.0-window-index.yaml. Tested by tmuxp example catalog (tests/WorkspaceFiles.Tests.ps1; loaded by the library in a test; the command is not run).

Install LibTmux.Workspace

The workspace package is separate from the core library.

PowerShell
PowerShell
LibTmux.Workspace 0.1.0 concept PowerShell Gallery
PS> Install-Module -Name LibTmux.Workspace
PS> Install-PSResource -Name LibTmux.Workspace

Installs the matching LibTmux core dependency. Run PowerShell and tmux on the same Unix host.

Installs the matching LibTmux core dependency. Run PowerShell and tmux on the same Unix host.

Build the workspace from a program
examples/Guides.ps1Excerpt · lines 640–647
$workspace = LibTmux.Workspace\Import-TmuxWorkspace -Yaml @'
session_name: readme-workspace-preview
windows:
- window_name: editor
panes:
- shell_command: exec /bin/sh
- shell_command: exec /bin/sh
'@

Excerpt of examples/Guides.ps1. This program is tested on every change.

Packages and integrations

API references

Esc
Search in

Type to search.

Reference previewOpen reference