Pane.Capture
Capture captures printed content from the receiver's exact linked pane. It returns a caller-owned slice. Completed stderr becomes a CommandError; any stdout tmux still printed is returned alongside it. A nonzero exit with empty stderr is not an error, as for every command (see the package documentation), and its stdout is returned.
Source and package
- func (p Pane) Capture( ctx context
.Context , request CapturePane , ) ([]string, error)Request -
Description#
This is a point-in-time capture rather than a stream; the zero request reads the visible screen. It may include a shell's echo of
Pane.SendKeysinput. Compare whole lines when polling; useControlClient.NextNotificationorServer.WaitForwhen the application exposes a stream or explicit signal.Invalid positions fail before execution. Transport and context failures return any partial stdout with the error.
Examples#
Capture the pane's history and visible screen as lines after sending text to cat. CaptureBoundary selects the beginning of history and the end of the screen. Poll whole lines because terminal echo may appear before the application handles input; this example checks visible text, not command completion. 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""slices""time""github.com/libtmux/libtmux-go/tmux")// Capture the pane's history and visible screen as lines after sending text// to cat. CaptureBoundary selects the beginning of history and the end of// the screen. Poll whole lines because terminal echo may appear before the// application handles input; this example checks visible text, not command// completion.func ExamplePane_Capture_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)}pane, err := session.ResolveActivePane(ctx)if err != nil {return fmt.Errorf("resolve pane: %w", err)}text := "api input"if err := pane.SendKeys(ctx, tmux.SendKeysRequest{Command: &text, Literal: true}); err != nil {return fmt.Errorf("send literal input: %w", err)}// Capture is a point-in-time read, so wait for a complete matching line.ticker := time.NewTicker(10 * time.Millisecond)defer ticker.Stop()for {lines, err := pane.Capture(ctx, tmux.CapturePaneRequest{Start: tmux.CaptureBoundary, End: tmux.CaptureBoundary,})if err != nil {return fmt.Errorf("capture pane: %w", err)}if slices.Contains(lines, "api input") {fmt.Println("captured: api input")break}select {case <-ctx.Done():return fmt.Errorf("wait for pane text: %w", ctx.Err())case <-ticker.C:}}return nil}if err := run(); err != nil {panic(err)}// Output:// captured: api input}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 Capture.
tmux C API#
The C declarations in tmux that implement these commands.
Guides#
Pages that discuss Capture.
- Examples
- Topics
In other ports#
Read a pane’s visible contents
| 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 | pane |
| Rust | pane |
| Java | io |
| Clojure | libtmux |
| Kotlin | io |
| Scala | io |
| C# | Lib |
| F# | 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 or Common Lisp.