Core Library0.xAPI Reference

Choose documentation 3

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.

method libtmux-go latest

Server.SearchSessions

SearchSessions returns a newly materialized snapshot projection of sessions selected by tmux's live -f expression. A nil filter omits -f; a nonnil empty filter remains an explicit empty expression.

Source and package
Qualified name
tmux.Server.SearchSessions
Package
tmux
Declared in
Server
Distribution
github.com/libtmux/libtmux-go/tmux
Source
tmux/search.go
Other languages
Perl Erlang PowerShell Julia Elixir Haskell Common Lisp Python Ruby Lua TypeScript Rust Java C# C++ Swift Kotlin Scala OCaml F# Clojure
func (s Server) SearchSessions( ctx context.Context, filter *TmuxFilter, ) ([]Session, error)

Examples#

Filter live sessions by name using tmux's format expression syntax. A nil filter lists all sessions. No matches returns an empty slice; the returned session records do not capture window relations. Use Go 1.26 or newer and tmux 3.3a or newer on a Unix system, with tmux and cat on PATH. In a new directory, create a module and add the library from the Go module proxy:

Terminal window
$ go mod init example.com/libtmux-api && \
go get github.com/libtmux/libtmux-go@latest

Save this complete example as example_test.go. It creates a private temporary socket and stops its server before removing the directory. Cleanup failure fails the example and retains the socket directory. View source

package tmux_test
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"time"
"github.com/libtmux/libtmux-go/tmux"
)
// Filter live sessions by name using tmux's format expression syntax.
// A nil filter lists all sessions. No matches returns an empty slice;
// the returned session records do not capture window relations.
func ExampleServer_SearchSessions_complete() {
run := func() (runErr error) {
directory, err := os.MkdirTemp("", "libtmux-go-api-")
if err != nil {
return fmt.Errorf("create socket directory: %w", err)
}
server, err := tmux.NewServer(tmux.ServerOptions{
SocketPath: filepath.Join(directory, "tmux.sock"),
ConfigFile: "/dev/null",
})
if err != nil {
return errors.Join(err, os.RemoveAll(directory))
}
defer func() {
// Cleanup has its own deadline because the operation may have timed out.
cleanup, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Kill(cleanup); err != nil && !errors.Is(err, tmux.ErrNoServer) {
runErr = errors.Join(runErr, fmt.Errorf("stop server at %s: %w", directory, err))
return
}
runErr = errors.Join(runErr, os.RemoveAll(directory))
}()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for _, name := range []string{"work-one", "work-two", "scratch"} {
if _, err = server.NewSession(ctx, tmux.NewSessionRequest{
Name: name, Command: "cat", Width: 100, Height: 30,
}); err != nil {
return fmt.Errorf("create %s session: %w", name, err)
}
}
filter := tmux.TmuxFilter("#{m:work-*,#{session_name}}")
matched, err := server.SearchSessions(ctx, &filter)
if err != nil {
return fmt.Errorf("filter sessions: %w", err)
}
names := make([]string, 0, len(matched))
for _, session := range matched {
name, ok := session.Name()
if !ok {
return errors.New("matched session has no name")
}
if _, loaded := session.Windows(); loaded {
return errors.New("live session search unexpectedly loaded windows")
}
names = append(names, name)
}
slices.Sort(names)
fmt.Println("matched:", names)
all, err := server.SearchSessions(ctx, nil)
if err != nil {
return fmt.Errorf("list unfiltered sessions: %w", err)
}
fmt.Println("unfiltered:", len(all))
missing := tmux.TmuxFilter("#{==:#{session_name},missing}")
none, err := server.SearchSessions(ctx, &missing)
if err != nil {
return fmt.Errorf("search missing session: %w", err)
}
fmt.Println("missing:", len(none))
return nil
}
if err := run(); err != nil {
panic(err)
}
// Output:
// matched: [work-one work-two]
// unfiltered: 3
// missing: 0
}

Run the saved file. Go checks the Output comment and fails on different output or an operation or cleanup error:

Terminal window
$ go test -count=1 -v example_test.go

tmux command#

The tmux commands behind SearchSessions.

list-sessions run-shell (the Server has no daemon identity)

tmux C API#

The C declarations in tmux that implement these commands.

Guides#

Pages that discuss SearchSessions.

In other ports#

Filter the sessions with a tmux expression

Equivalents of Filter the sessions with a tmux expression in other languages
Language Equivalent
Python libtmux.Server.search_sessions
Lua libtmux.Server:query
Haskell No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing.
TypeScript no equivalent on Server
Rust no equivalent on Server
Java no equivalent on Server
C# LibTmux.Server.SearchSessionsAsync
C++ no equivalent on Server
Swift no equivalent on Server
OCaml No direct public OCaml equivalent is recorded for this operation.
Perl No dedicated equivalent in the selected Perl surface. Explicit Server::cmd remains available for native tmux commands.

No equivalent is recorded in Ruby, PowerShell, Julia, Erlang, Elixir, Common Lisp, Clojure, Kotlin, Scala or F#.

Esc

Type to search.

Reference previewOpen reference