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.Sessions

Sessions materializes the server's sessions. Failures, including ErrNoServer, remain errors rather than empty results.

Source and package
Qualified name
tmux.Server.Sessions
Package
tmux
Declared in
Server
Distribution
github.com/libtmux/libtmux-go/tmux
Source
tmux/hierarchy.go
Other languages
Perlmethod Erlangfunction PowerShellcmdlet Juliafunction Elixirfunction Haskell Common Lispfunction PythonRubyLuaTypeScriptRustJavaC#C++SwiftKotlinScalaOCamlfunction F#function Clojurefunction
func (s Server) Sessions( ctx context.Context, ) ([]Session, error)

Examples#

List all sessions on a private server. Sessions reads a fresh snapshot; an absent daemon is an error, not an empty list. Names are sorted here only to make the example output independent of tmux ordering. 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"
)
// List all sessions on a private server. Sessions reads a fresh snapshot;
// an absent daemon is an error, not an empty list. Names are sorted here
// only to make the example output independent of tmux ordering.
func ExampleServer_Sessions_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", "docs"} {
if _, err = server.NewSession(ctx, tmux.NewSessionRequest{
Name: name, WindowName: name + "-editor", Command: "cat", Width: 100, Height: 30,
}); err != nil {
return fmt.Errorf("create %s session: %w", name, err)
}
}
sessions, err := server.Sessions(ctx)
if err != nil {
return fmt.Errorf("list sessions: %w", err)
}
names := make([]string, 0, len(sessions))
for _, session := range sessions {
name, ok := session.Name()
if !ok {
return fmt.Errorf("session %s has no name", session.ID())
}
names = append(names, name)
}
slices.Sort(names)
fmt.Println(names)
return nil
}
if err := run(); err != nil {
panic(err)
}
// Output:
// [docs work]
}

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
Parameters

Guides#

Pages that discuss Sessions.

Topics

In other ports#

List the server’s sessions

Equivalents of List the server’s sessions in other languages
Language Equivalent
Python libtmux.Server.sessions
Ruby LibTmux::Server#list_sessions
Lua libtmux.Snapshot.sessions
PowerShell LibTmux\Get-TmuxSession Acquires sessions through -Server, with optional exact -Id and -Name selectors.
Julia LibTmux.sessions sessions reads membership from a captured Snapshot without contacting tmux.
Erlang tmux_snapshot:sessions/1 Returns captured rows without tmux I/O.
Elixir LibTmux.Snapshot.sessions/1 Returns a Selection of captured sessions without contacting tmux.
Haskell No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing.
Common Lisp libtmux:sessions sessions returns a fresh vector from a captured snapshot without contacting tmux.
TypeScript server.Server.sessions
Rust server.Server.sessions
Java io.github.libtmux.Server.sessions
Clojure libtmux.core/sessions! Acquires a new captured graph and returns its session maps; acquisition failure throws.
Kotlin io.github.libtmux.kotlin.Server.sessions
Scala io.github.libtmux.scaladsl.Server.sessions
C# LibTmux.Server.Sessions
F# LibTmux.FSharp.Server.sessions Builds a deferred session query on LibTmux.Server. Query.list acquires the results; it does not traverse the handle’s captured membership.
C++ libtmux::Server::sessions
Swift Server.sessions()
OCaml Libtmux.Snapshot.sessions Reads an immutable array of already captured sessions without I/O.
Perl LibTmux::Snapshot::sessions Reads captured membership locally, preserving order.
Esc

Type to search.

Reference previewOpen reference