# Attach and send keys

Source: https://libtmux.org/en/cxx/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](https://libtmux.org/en/cxx/latest/guides/attaching-to-tmux/).

The examples include setup, error handling, and cleanup. [Source and
verification](https://libtmux.org/en/cxx/latest/examples/attach-and-send-keys/#where-this-comes-from) identifies their files and checks.

```cpp
// No tmux failure is thrown. Every call answers with a value that is either
// the result or the reason there isn't one.
const auto sessions = server.sessions();
if (!sessions.has_value()) {
  std::fprintf(stderr, "%s\n", sessions.error().diagnostic.c_str());
  return 1;
}

for (const libtmux::Session& session : *sessions) {
  std::printf("%s has %lld window(s)\n", std::string{session.name()}.c_str(),
              session.window_count());
}

const libtmux::Session& session = sessions->at(0);

// Build an arrangement without composing a single tmux argument.
const auto editor = session.new_window({.name = "editor"});
if (!editor.has_value()) {
  std::fprintf(stderr, "%s\n", editor.error().diagnostic.c_str());
  return 1;
}

const auto logs = editor->split({.horizontal = true, .percentage = 30});
if (!logs.has_value()) {
  std::fprintf(stderr, "%s\n", logs.error().diagnostic.c_str());
  return 1;
}

(void)logs->send_text("journalctl -f");
(void)logs->send_key("Enter");
```

## Finding an existing session instead

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

## Where this comes from

**Source:** [`examples/05-readme.cpp`](<https://github.com/libtmux/libtmux-cxx/blob/9645e54239e040aa6f2bd6bf42c2fbe1aaf38250/examples/05-readme.cpp>), the [`connect`](<https://libtmux.org/en/cxx/latest/reference/libtmux-connection-connect/>) and [`build`](<https://libtmux.org/en/cxx/latest/workspace/reference/libtmux-workspace-build/>) regions

**In this page:** Copied excerpts from the [`connect`](<https://libtmux.org/en/cxx/latest/reference/libtmux-connection-connect/>) and [`build`](<https://libtmux.org/en/cxx/latest/workspace/reference/libtmux-workspace-build/>) regions.

**Checked by:** quoted verbatim into README.md, checked for drift by
[`tools/docs/check_readme.py`](<https://github.com/libtmux/libtmux-cxx/blob/9645e54239e040aa6f2bd6bf42c2fbe1aaf38250/tools/docs/check_readme.py>), and the whole file is built and run by CTest

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