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

NewSession creates a detached session, then returns a newly materialized Session. It never changes client focus. KillExisting is not rolled back; transport and context failures may be delivery-ambiguous.

Source and package
Qualified name
tmux.Server.NewSession
Package
tmux
Declared in
Server
Distribution
github.com/libtmux/libtmux-go/tmux
Source
tmux/session_lifecycle.go
Other languages
Perlmethod Erlang PowerShell Julia Elixir Haskell Common Lisp PythonRubyLuaTypeScriptRustJavaC#C++SwiftKotlinScalaOCaml F# Clojure

Description#

The returned record has unmaterialized Session.Windows and Session.Panes relations. Use the resolvers or Server.Snapshot when relations are needed.

If tmux reports an ID before a transport or refresh failure, the returned partial Session contains its Server and SessionID. Other failures return zero.

Examples#

Create a detached session named work with an editor window running cat. A second creation with the same name returns ErrSessionExists. Creation returns a session record without captured window relations; use a live lookup or a snapshot when those relations are needed. 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"
"time"
"github.com/libtmux/libtmux-go/tmux"
)
// Create a detached session named work with an editor window running cat.
// A second creation with the same name returns ErrSessionExists. Creation
// returns a session record without captured window relations; use a live
// lookup or a snapshot when those relations are needed.
func ExampleServer_NewSession_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()
session, err := server.NewSession(ctx, tmux.NewSessionRequest{
Name: "work", WindowName: "editor", Command: "cat", Width: 100, Height: 30,
})
if err != nil {
return fmt.Errorf("create session: %w", err)
}
name, ok := session.Name()
if !ok {
return errors.New("created session has no name")
}
fmt.Println("session:", name)
_, relations := session.Windows()
fmt.Println("window relations captured:", relations)
_, err = server.NewSession(ctx, tmux.NewSessionRequest{Name: "work", Command: "cat"})
if !errors.Is(err, tmux.ErrSessionExists) {
return errors.Join(errors.New("expected duplicate session error"), err)
}
fmt.Println("duplicate rejected: true")
return nil
}
if err := run(); err != nil {
panic(err)
}
// Output:
// session: work
// window relations captured: false
// duplicate rejected: true
}

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

kill-session (KillExisting is set, session exists) new-session list-sessions (a name is given)

tmux C API#

The C declarations in tmux that implement these commands.

Each path starts at a function a command runs, which is degree 1. The degree counts steps from there along the path, and each step is a call or a reference: degree 2 is reached from that function, degree 3 from the next, and so on.

Create a session and its first pane

  1. 1 cmd_new_session_exec
  2. calls → 2 spawn_window
  3. calls → 3 spawn_pane
  4. calls → 4 window_add_pane
  5. calls → 5 window_pane_create

Guides#

Pages that discuss NewSession.

In other ports#

Create a session

Equivalents of Create a session in other languages
Language Equivalent
Python libtmux.Server.new_session
Ruby LibTmux::Server#new_session
Lua libtmux.Server:new_session
Erlang tmux_session:create/2
Haskell No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing.
TypeScript server.Server.newSession
Rust server.Server.new_session
Java io.github.libtmux.Server.newSession
Clojure libtmux.core/new-session!
Kotlin io.github.libtmux.kotlin.Server.newSession
Scala io.github.libtmux.scaladsl.Server.newSession
C# LibTmux.Server.CreateSessionAsync
C++ libtmux::Server::new_session
Swift Server.newSession(named:startDirectory:windowName:width:height:environment:shell:)
OCaml Libtmux_unix.Client.create_session
Perl LibTmux::Server::new_session Explicit Server operation. Captured entities remain local observations; use a new snapshot to observe changes.

No equivalent is recorded in PowerShell, Julia, Elixir, Common Lisp or F#.

Esc

Type to search.

Reference previewOpen reference