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

Filter and query tmux objects

Needs libtmux for PowerShell 0.x Install

Use Where-Object to filter captured objects locally. Use structured criteria when a selection needs reuse, serialization or an inspectable source plan. Both return native tmux objects that feed other cmdlets.

Import LibTmux and select an existing endpoint as $server with New-TmuxServer. The examples borrow it and create or remove no sessions. Read the daemon version for source planning, then capture its complete pane graph:

Terminal window
$captured = $server |
Get-TmuxServer -ErrorAction Stop |
Get-TmuxSnapshot -ErrorAction Stop

Start with PowerShell

Select panes at least 50 columns wide. Property access and filtering use the captured data and never contact tmux.

Terminal window
$captured.Panes | Where-Object { $_.Width -ge 50 -and $_.Height -gt 0 }

For reuse, construct the same condition as a native QueryDocument:

Terminal window
$query = New-TmuxQuery -Target Pane -Criteria @{
Width = @{ Ge = 50 }
Height = @{ Gt = 0 }
}

Apply it to the captured panes. Input order, duplicates and object identity stay intact, and parent and child relations keep the complete graph.

Terminal window
$captured.Panes | Select-TmuxPane -Query $query

Criteria vocabulary

Get-TmuxQueryField returns native catalog descriptors. Inspect the numeric width field and its supported wire operations:

Terminal window
Get-TmuxQueryField -Target Pane | Where-Object WireName -CEQ 'pane_width'
  • Criteria accept a field’s catalog wire name or its native property path: Width and pane_width name the same field.
  • Field and operator keys ignore case. Giving both aliases in one map is an error.
  • A bare value means equality, and the entries of a map combine with And.

The catalog’s Operators use JSON wire names. In criteria maps use these PowerShell keys; an unsupported field and operator pair fails at construction.

Criteria keyNative wire operationValue
Bare value or Eq; Neequal; notEqualNative scalar value
Lt, Le, Gt, GelessThan, lessThanOrEqual, greaterThan, greaterThanOrEqualInt64-representable integer
StartsWith, EndsWith, ContainsstartsWithOrdinal, endsWithOrdinal, containsOrdinalString
EqualIgnoreCasestringEqualOrdinalIgnoreCaseString
StartsWithIgnoreCase, EndsWithIgnoreCase, ContainsIgnoreCasestartsWithOrdinalIgnoreCase, endsWithOrdinalIgnoreCase, containsOrdinalIgnoreCaseString
In, NotInor of equal, optionally notArray or list of scalar values
IsNull, IsNotNullequal or notEqual with null$true only
RegexregexMap with Pattern and optional Options list
Some, Every, Noneany, all, or negated anyChild criteria for a collection relation
Is, IsNotrelated, optionally negatedChild criteria for a to-one relation
And, Or, Notand, or, notCriteria lists for And/Or; one criteria map for Not

Strings

  • Equality, membership and substring matching are ordinal and case-sensitive. In PowerShell compare with -ceq or -cin, not -eq or -in.
  • EqualIgnoreCase, StartsWithIgnoreCase, EndsWithIgnoreCase and ContainsIgnoreCase ask for ordinal case-insensitive matching.
  • Nothing expands wildcards or normalizes strings.
  • The catalog also lists stringEqualOrdinal; in criteria that is bare string equality or Eq.

Values

  • Integers are not coerced from strings, Booleans, enums, floating-point or decimal values.
  • IDs take the matching native ID type or a correctly prefixed string. Single- quote session IDs such as '$0'.
  • $null means explicit absence, and an omitted field is unconstrained. An empty string is a value.
  • ScriptBlocks belong in Where-Object; they are not translated into criteria.

Collections

  • A collection property has a scalar path and a relation path. Use 'Windows.Count' for a numeric comparison and Windows with Some, Every or None.
  • A condition that needs both paths takes an explicit And with separate maps.
  • ActivePane and ActivePane.Value name the same captured to-one relation.
  • An uncaptured relation raises an error; it never becomes empty data.

Limits

These construction limits count the nodes that membership and operator maps expand into:

LimitMaximum
Predicate depth32
Predicate node occurrences512
String length4,096 Unicode scalar values
Regex pattern length1,024 Unicode scalar values
Encoded JSON262,144 UTF-8 bytes
Membership entries128

Cycles, duplicate aliases, invalid Unicode and values past these limits are rejected. A document owns its values, so editing the original hashtable or list does not change a query. Serialization uses one schema, libtmux-query version 2, and the module includes its JSON schema file.

Match captured relationships

Select windows with at least one pane that meets both size conditions:

Terminal window
$captured.Windows | Select-TmuxWindow -Criteria @{
Panes = @{ Some = @{ Width = @{ Ge = 50 }; Height = @{ Gt = 0 } } }
}
  • Both conditions apply to the same pane. Two separate Some clauses may match different panes.
  • On an empty captured collection Every is true, Some is false and None is true.
  • Is and IsNot traverse one captured related object.
  • Linked windows keep their session and index placement, so a pane ID can appear in several input placements. Selection does not deduplicate them.

Combine alternatives and exclusions to select API or log windows while excluding scratch windows:

Terminal window
$captured.Windows | Select-TmuxWindow -Criteria @{
Or = @(@{ Name = @{ StartsWith = 'api-' } }, @{ Name = 'logs' })
Not = @{ Name = @{ Contains = 'scratch' } }
}

For a regular expression, pass an explicit pattern and semantic option list:

Terminal window
$captured.Windows | Select-TmuxWindow -Criteria @{
Name = @{ Regex = @{ Pattern = '^API-'; Options = @('IgnoreCase') } }
}

Regex uses the .NET dialect, with CultureInvariant always on. Options accept IgnoreCase, Multiline, ExplicitCapture, Singleline, IgnorePatternWhitespace and CultureInvariant. Execution-only flags such as Compiled are rejected, and a running match has a one-second timeout. This is the .NET dialect, not a promise of regex compatibility with other languages.

Store a query or acquire a fresh result

ConvertTo-TmuxQueryJson returns a JSON string suitable for storage. Restore that string with New-TmuxQuery -Json; this round-trip does not read tmux:

Terminal window
New-TmuxQuery -Json ($query | ConvertTo-TmuxQueryJson)

Prepare a plan using the daemon version from the earlier capture. Planning and inspecting $queryPlan perform no I/O:

Terminal window
$queryPlan = $query | Get-TmuxQueryPlan -DaemonVersion $captured.DaemonVersion

PushedPredicate, ResidualPredicate, RequiredFields, RequiredSnapshotDepth and FallbackReasons explain how it runs. A plan reads no data.

  • -Pushdown Auto uses exact supported source evaluation.
  • Never evaluates locally.
  • Require rejects any local residual.

Execute it against the selected endpoint when a fresh result is needed:

Terminal window
$server | Invoke-TmuxQuery -Plan $queryPlan -AsResult -ErrorAction Stop
  • -AsResult emits one QueryResult, even with no matches. Its indexed rows refer to the complete Snapshot; filtering does not prune that graph. Without it, matching objects flow down the pipeline one by one.
  • Invoke-TmuxQuery -Query $query plans against a freshly observed daemon version instead.
  • Execution never starts an absent daemon, and a prepared plan must match the observed version.
  • Acquisition is not an atomic transaction.

-NativeFilter with -Target is a separate expert escape hatch for raw tmux format filtering. It cannot combine with -Query, -Plan, -Pushdown or -AsResult, and its rows promise no complete structured-query snapshot.

Require exactly one match

Resolve the captured development session before handing it to another command:

Terminal window
$captured.Sessions |
Select-TmuxSession -Criteria @{ Name = 'development' } -ExactlyOne
  • -ExactlyOne holds the match until finite input completes.
  • Zero or several matches end with Tmux.NoMatch or Tmux.MultipleMatches.
  • Invalid inputs, unavailable relations, evaluation errors and a pipeline stop suppress the held object.
  • An infinite stream cannot establish exact cardinality.

The guarantee covers the objects the selector received. A non-terminating error upstream can skip a source and still leave one match, so use -ErrorAction Stop on every upstream acquisition when you need complete input before a mutation. It does not roll back changes another command already made. Local selection never refreshes captured objects; acquire again to see changes.

Esc
Search in

Type to search.

Reference previewOpen reference