# pane.Pane.capture

- **Module:** pane
- **Package:** libtmux
- **Language:** TypeScript
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-ts/blob/46cccfef2a546de55ce8b308249b8c76339089a0/packages/libtmux/src/pane.ts#L344
- **Page:** https://libtmux.org/en/ts/latest/reference/pane-pane-capture/

```
capture(
  options?: CaptureOptions,
): Promise<readonly string[]>
```

Capture this pane's contents as lines.

Save this complete program as `capture.ts`. With Bun 1.4.2 or newer,
tmux 3.2a or newer, and `libtmux` installed, run `bun run capture.ts`.
It creates and stops a private tmux server. A failed operation or cleanup
exits with an error; a cleanup error names the retained socket directory.

## Examples

```ts
import { mkdtemp, readdir, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { Server } from "libtmux";

const directory = await mkdtemp(join(tmpdir(), "ltx-capture-"));
const server = new Server({
  socketPath: join(directory, "tmux.sock"),
  configFile: "/dev/null",
  timeoutMs: 5_000,
});
const failures: unknown[] = [];
try {
  const signal = AbortSignal.timeout(5_000);
  const session = await server.newSession({
    name: "capture",
    shellCommand: "sh",
    signal,
    environment: { ENV: "/dev/null" },
  });
  const pane = session.activePane;
  if (!pane) throw new Error("The session has no active pane");
  await pane.sendKeys("printf '\\nlibtmux capture ready\\n'", { signal });
  for (;;) {
    const lines = await pane.capture({ signal });
    const line = lines.find((line) => line === "libtmux capture ready");
    if (line !== undefined) {
      console.log(line);
      break;
    }
    await delay(20, undefined, { signal });
  }
} catch (error) {
  failures.push(error);
} finally {
  try {
    if ((await readdir(directory)).includes("tmux.sock")) await server.kill();
    await rm(directory, { recursive: true });
  } catch (error) {
    failures.push(new Error(`Cleanup failed; inspect ${directory}`, { cause: error }));
  }
}
if (failures.length > 0) throw new AggregateError(failures, "Capture failed");
```

## Parameters

- `options?` (`CaptureOptions`)

## tmux command

The tmux commands behind `capture`.

- [`capture-pane`](/en/tmux/latest/manual/capture-pane/)

## tmux C API

The C declarations in tmux that implement these commands.

- [`cmd_capture_pane_entry`](/en/tmux/latest/reference/c-variable-cmd_capture_pane_entry/) (attribute)
- [`cmd_capture_pane_exec`](/en/tmux/latest/reference/c-function-cmd-capture-pane-c-cmd_capture_pane_exec/) (function)

## Guides

Pages that discuss `capture`.

- [Capturing output](/en/ts/latest/guides/capturing-output/)
- [Getting started](/en/ts/latest/guides/getting-started/)
- [Sending keys](/en/ts/latest/guides/sending-keys/)

## In other ports

Read a pane’s visible contents

| Language | Equivalent |
| --- | --- |
| Python | [`libtmux.Pane.capture_pane`](/en/py/latest/reference/libtmux-pane-capture_pane/) (method) |
| Ruby | [`LibTmux::Pane#capture`](/en/ruby/latest/reference/libtmux-pane-capture/) (method) |
| Lua | [`libtmux.Pane:capture`](/en/lua/latest/reference/libtmux-pane-capture/) (method) |
| Rust | [`pane.Pane.capture`](/en/rs/latest/reference/pane-pane-capture/) (method) |
| Go | [`tmux.Pane.Capture`](/en/go/latest/reference/tmux-pane-capture/) (method) |
| Java | [`io.github.libtmux.Pane.capture`](/en/java/latest/reference/io-github-libtmux-pane-pane-capture/) (method) |
| Kotlin | [`io.github.libtmux.kotlin.Pane.capture`](/en/kotlin/latest/reference/io-github-libtmux-kotlin-pane-capture/) (method) |
| Scala | [`io.github.libtmux.scaladsl.Pane.capture`](/en/scala/latest/reference/io-github-libtmux-scaladsl-pane-capture/) (method) |
| C# | [`LibTmux.Pane.CaptureAsync`](/en/csharp/latest/reference/libtmux-pane-captureasync/) (method) |
| F# | [`LibTmux.FSharp.Pane.capture`](/en/fsharp/latest/reference/libtmux-fsharp-pane-capture/) (function) |
| C++ | [`libtmux::Pane::capture`](/en/cxx/latest/reference/libtmux-pane-capture/) (method) |
| Swift | [`Server.capture(_:includingHistory:)`](/en/swift/latest/reference/server-capture(_-includinghistory-)/) (method) |

No equivalent is recorded in PowerShell, Julia or Elixir.
