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
- func (s Server) NewSession( ctx context
.Context , request NewSession , ) (Session, error)Request -
Description#
The returned record has unmaterialized
Session.WindowsandSession.Panesrelations. Use the resolvers orServer.Snapshotwhen relations are needed.If tmux reports an ID before a transport or refresh failure, the returned partial Session contains its
ServerandSessionID. 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
tmuxandcatonPATH. 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@latestSave 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 sourcepackage tmux_testimport ("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- Parameters
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.
cmd_kill_session_entry cmd_kill_session_exec cmd_new_session_entry cmd_new_session_exec cmd_list_sessions_entry cmd_list_sessions_exec 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
cmd_new_session_exec - calls → 2
spawn_window - calls → 3
spawn_pane - calls → 4
window_add_pane - calls → 5
window_pane_create
Guides#
Pages that discuss NewSession.
- Concepts
In other ports#
Create a session
| Language | Equivalent |
|---|---|
| Python | libtmux |
| Ruby | Lib |
| Lua | libtmux |
| Erlang | tmux |
| Haskell | No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing. |
| TypeScript | server |
| Rust | server |
| Java | io |
| Clojure | libtmux |
| Kotlin | io |
| Scala | io |
| C# | Lib |
| C++ | libtmux:: |
| Swift | Server |
| OCaml | Libtmux |
| Perl | Lib 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#.