# Attach and send keys

Source: https://libtmux.org/en/ruby/latest/examples/attach-and-send-keys/

> Get a session handle, send a command to a pane, and capture output.

Get a session handle, send a command to a pane, and capture output. These
examples use libtmux from your program; to attach your terminal interactively,
see [Attaching to tmux](/guides/attaching-to-tmux/).

Select your port below. The examples retain their source's setup, error
handling, and cleanup, so the operations shown vary by port. [Where this comes
from](#where-this-comes-from) identifies each source and its test coverage.

## Finding an existing session instead

For a script that runs repeatedly, look up a session before creating it.
[Attaching to
tmux](/guides/attaching-to-tmux/#finding-a-session-instead-of-always-creating-one)
shows that pattern, and [Filtering and querying, in
practice](/guides/querying-and-filtering/) covers absent and ambiguous matches.

## Where this comes from

### Python

**Source:** `src/libtmux/server.py`, `session.py`, `pane.py` docstrings

**In this page:** hand-quoted, composed from three separate docstrings

**Checked by:** `pytest` runs every `>>>` doctest (`testpaths` includes
`src/libtmux`) against a real, isolated tmux session on every test run

### TypeScript

**Source:** `examples/quickstart/quickstart.ts`

**In this page:** read whole from the file

**Checked by:** run against real tmux by `bun test examples`; its first half is
also mirrored into README.md under a `<!-- runs: ... -->` marker, checked
line-for-line by `scripts/check-doc-runnable.ts`

### Rust

**Source:** `crates/libtmux/examples/scratch.rs`

**In this page:** read whole from the file

**Checked by:** run to completion against a throwaway tmux by
`scripts/run-examples.sh` (`just examples`), which CI runs and which also
asserts the example leaves no session behind

### Go

**Source:** `examples/quickstart/main.go`

**In this page:** read whole from the file

**Checked by:** the whole file runs against a real tmux server as
`TestQuickstart`; the `docs:quickstart` region inside it is additionally
mirrored into README.md by `go generate ./tmux`, and CI fails if the two drift

### Java

**Source:**
`examples/src/main/java/io/github/libtmux/examples/BuildAWorkspace.java`

**In this page:** read whole from the file

**Checked by:** run against real tmux by the `examples` module's own
`ExamplesRunTest`

### .NET

**Source:** `examples/LibTmux.Examples/Snippets/OneShot.cs`

**In this page:** read whole from the file

**Checked by:** its `ConnectAndBuild` region is mirrored into README.md and
checked by `sync_snippets.py --check`; the mirrored `csharp run` block is
additionally compiled and run by `ReadmeExampleTests`

### C++

**Source:** `examples/05-readme.cpp`, the `connect` and `build` regions

**In this page:** Copied excerpts from the `connect` and `build` regions.

**Checked by:** quoted verbatim into README.md, checked for drift by
`tools/docs/check_readme.py`, and the whole file is built and run by CTest

### Swift

**Source:** `Examples/Sources/ExampleCode/Changing.swift`

**In this page:** read whole from the file

**Checked by:** matched against the README's "Change what is there" section by
`Scripts/check_examples.py`; compiled and run through the package's public
products by `swift test --package-path Examples`

### Source inclusion

A `file="..."` fence reads the named source during the site build. Hand-quoted
excerpts are copies; their source files and regions are listed above.
