Attach and send keys
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.
The examples include setup, error handling, and cleanup. Source and verification identifies their files and checks.
// 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 shows that pattern, and Filtering and querying, in practice covers absent and ambiguous matches.
Where this comes from¶
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
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.