C++Home

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

libtmux for C++

C++-based tmux client

tmux client library for C++. Control tmux, connect to and create servers, navigate sessions, windows, and panes, send keys, watch for changes via control mode, capture pane contents

Product

Control tmux from C++ code 0.x Released; the API may change. Read more Core Library home →

#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <libtmux/libtmux.hpp>
int main() {
char directory[] = "/tmp/ltx-cxx-home-XXXXXX";
if (!mkdtemp(directory)) {
std::perror("create temporary directory");
return 1;
}
const auto socket = std::filesystem::path(directory) / "tmux.sock";
const auto server = libtmux::Server::startable_at_socket_path(
socket,
"/dev/null",
{},
{.timeout = std::chrono::seconds{5}}).value();
const auto work = [&] {
const auto session = server.new_session({
.name = "demo",
.shell_command = "/bin/sh",
}).value();
// Lay out the session: name its window, split it, add a second window.
const auto editor = session.active_window().value();
editor.rename("editor").value();
const auto left = editor.active_pane().value();
const auto right = left.split({
.horizontal = true,
.shell_command = "/bin/sh",
}).value();
const auto logs = session.new_window({
.name = "logs",
.shell_command = "/bin/sh",
}).value();
// Type a command into the new pane and wait for what it prints.
right.send_line("printf 'hello from %s\\n' tmux").value();
const auto ready = right.wait_for_text(
"hello from tmux",
{.timeout = std::chrono::seconds{5}}).value();
if (!ready.matched) throw std::runtime_error("output did not arrive");
// Move the focus an attached client sees: the pane, then the window.
right.select().value();
logs.select().value();
// Walk the objects: down from the server, then up from one pane.
const auto sessions = server.sessions().value();
for (const auto& each : sessions) {
std::cout << "session " << each.name() << '\n';
const auto windows = each.windows().value();
for (const auto& window : windows) {
std::cout << " window " << window.index() << ':'
<< window.name() << ':';
const auto panes = window.panes().value();
for (const auto& pane : panes) {
std::cout << ' ' << pane.id().value();
}
std::cout << '\n';
}
}
const auto window = right.window().value();
const auto owner = window.session().value();
std::cout << right.id().value() << " is in window " << window.name()
<< " of session " << owner.name() << '\n';
};
work();
}
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <libtmux/libtmux.hpp>
int main() {
char directory[] = "/tmp/ltx-cxx-home-XXXXXX";
if (!mkdtemp(directory)) {
std::perror("create temporary directory");
return 1;
}
const auto checked = [](auto result) {
if (!result) throw std::runtime_error(result.error().diagnostic);
return std::move(result).value();
};
const auto socket = std::filesystem::path(directory) / "tmux.sock";
const auto server = checked(libtmux::Server::startable_at_socket_path(
socket,
"/dev/null",
{},
{.timeout = std::chrono::seconds{5}}));
const auto work = [&] {
const auto session = checked(server.new_session({
.name = "demo",
.shell_command = "/bin/sh",
}));
// Lay out the session: name its window, split it, add a second window.
const auto editor = checked(session.active_window());
checked(editor.rename("editor"));
const auto left = checked(editor.active_pane());
const auto right = checked(left.split({
.horizontal = true,
.shell_command = "/bin/sh",
}));
const auto logs = checked(session.new_window({
.name = "logs",
.shell_command = "/bin/sh",
}));
// Type a command into the new pane and wait for what it prints.
checked(right.send_line("printf 'hello from %s\\n' tmux"));
const auto ready = checked(right.wait_for_text(
"hello from tmux",
{.timeout = std::chrono::seconds{5}}));
if (!ready.matched) throw std::runtime_error("output did not arrive");
// Move the focus an attached client sees: the pane, then the window.
checked(right.select());
checked(logs.select());
// Walk the objects: down from the server, then up from one pane.
const auto sessions = checked(server.sessions());
for (const auto& each : sessions) {
std::cout << "session " << each.name() << '\n';
const auto windows = checked(each.windows());
for (const auto& window : windows) {
std::cout << " window " << window.index() << ':'
<< window.name() << ':';
const auto panes = checked(window.panes());
for (const auto& pane : panes) {
std::cout << ' ' << pane.id().value();
}
std::cout << '\n';
}
}
const auto window = checked(right.window());
const auto owner = checked(window.session());
std::cout << right.id().value() << " is in window " << window.name()
<< " of session " << owner.name() << '\n';
};
int status = 0;
try {
work();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
status = 1;
}
return status;
}
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <libtmux/libtmux.hpp>
int main() {
char directory[] = "/tmp/ltx-cxx-home-XXXXXX";
if (!mkdtemp(directory)) {
std::perror("create temporary directory");
return 1;
}
const auto socket = std::filesystem::path(directory) / "tmux.sock";
auto created = libtmux::Server::startable_at_socket_path(
socket,
"/dev/null",
{},
{.timeout = std::chrono::seconds{5}});
if (!created) {
std::fprintf(stderr, "%s\n", created.error().diagnostic.c_str());
std::filesystem::remove_all(directory);
return 1;
}
const auto server = std::move(created).value();
const auto work = [&] {
const auto session = server.new_session({
.name = "demo",
.shell_command = "/bin/sh",
}).value();
// Lay out the session: name its window, split it, add a second window.
const auto editor = session.active_window().value();
editor.rename("editor").value();
const auto left = editor.active_pane().value();
const auto right = left.split({
.horizontal = true,
.shell_command = "/bin/sh",
}).value();
const auto logs = session.new_window({
.name = "logs",
.shell_command = "/bin/sh",
}).value();
// Type a command into the new pane and wait for what it prints.
right.send_line("printf 'hello from %s\\n' tmux").value();
const auto ready = right.wait_for_text(
"hello from tmux",
{.timeout = std::chrono::seconds{5}}).value();
if (!ready.matched) throw std::runtime_error("output did not arrive");
// Move the focus an attached client sees: the pane, then the window.
right.select().value();
logs.select().value();
// Walk the objects: down from the server, then up from one pane.
const auto sessions = server.sessions().value();
for (const auto& each : sessions) {
std::cout << "session " << each.name() << '\n';
const auto windows = each.windows().value();
for (const auto& window : windows) {
std::cout << " window " << window.index() << ':'
<< window.name() << ':';
const auto panes = window.panes().value();
for (const auto& pane : panes) {
std::cout << ' ' << pane.id().value();
}
std::cout << '\n';
}
}
const auto window = right.window().value();
const auto owner = window.session().value();
std::cout << right.id().value() << " is in window " << window.name()
<< " of session " << owner.name() << '\n';
};
int status = 0;
try {
work();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
status = 1;
}
if (std::filesystem::exists(socket)) {
server.kill().value();
}
std::filesystem::remove_all(directory);
return status;
}
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <utility>
#include <libtmux/libtmux.hpp>
int main() {
char directory[] = "/tmp/ltx-cxx-home-XXXXXX";
if (!mkdtemp(directory)) {
std::perror("create temporary directory");
return 1;
}
const auto checked = [](auto result) {
if (!result) throw std::runtime_error(result.error().diagnostic);
return std::move(result).value();
};
const auto socket = std::filesystem::path(directory) / "tmux.sock";
auto created = libtmux::Server::startable_at_socket_path(
socket,
"/dev/null",
{},
{.timeout = std::chrono::seconds{5}});
if (!created) {
std::fprintf(stderr, "%s\n", created.error().diagnostic.c_str());
std::filesystem::remove_all(directory);
return 1;
}
const auto server = std::move(created).value();
const auto work = [&] {
const auto session = checked(server.new_session({
.name = "demo",
.shell_command = "/bin/sh",
}));
// Lay out the session: name its window, split it, add a second window.
const auto editor = checked(session.active_window());
checked(editor.rename("editor"));
const auto left = checked(editor.active_pane());
const auto right = checked(left.split({
.horizontal = true,
.shell_command = "/bin/sh",
}));
const auto logs = checked(session.new_window({
.name = "logs",
.shell_command = "/bin/sh",
}));
// Type a command into the new pane and wait for what it prints.
checked(right.send_line("printf 'hello from %s\\n' tmux"));
const auto ready = checked(right.wait_for_text(
"hello from tmux",
{.timeout = std::chrono::seconds{5}}));
if (!ready.matched) throw std::runtime_error("output did not arrive");
// Move the focus an attached client sees: the pane, then the window.
checked(right.select());
checked(logs.select());
// Walk the objects: down from the server, then up from one pane.
const auto sessions = checked(server.sessions());
for (const auto& each : sessions) {
std::cout << "session " << each.name() << '\n';
const auto windows = checked(each.windows());
for (const auto& window : windows) {
std::cout << " window " << window.index() << ':'
<< window.name() << ':';
const auto panes = checked(window.panes());
for (const auto& pane : panes) {
std::cout << ' ' << pane.id().value();
}
std::cout << '\n';
}
}
const auto window = checked(right.window());
const auto owner = checked(window.session());
std::cout << right.id().value() << " is in window " << window.name()
<< " of session " << owner.name() << '\n';
};
int status = 0;
try {
work();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
status = 1;
}
if (std::filesystem::exists(socket)) {
server.kill().value();
}
std::filesystem::remove_all(directory);
return status;
}
Example details
No tmux server yet

tmux before the first call: no server is running.

No tmux server yet

tmux after Server.startable_at_socket_path: no server is running; the call sent no command.

%0 sh
[demo]

tmux after Server.new_session: added session demo.

%0 sh
[demo]

tmux after Session.active_window: unchanged.

%0 sh
[demo]

tmux after Window.rename: renamed window 0:sh to 0:editor.

%0 sh
[demo]

tmux after Window.active_pane: unchanged.

%0 sh
%1 sh
[demo]

tmux after Pane.split: added pane %1 to window 0:editor.

%0 sh
%1 sh
[demo]

tmux after Session.new_window: added window 1:logs.

%0 sh
%1 sh
[demo]

tmux after Pane.send_line: sent keys to pane %1.

%0 sh
%1 sh
$ printf 'hello from %s\n' tmux
hello from tmux
$
[demo]

tmux after Pane.wait_for_text: read pane %1.

%0 sh
%1 sh
$ printf 'hello from %s\n' tmux
hello from tmux
$
[demo]

tmux after Pane.select: selected pane %1 in window 0:editor.

%2 sh
[demo]

tmux after Window.select: selected window 1:logs.

%2 sh
[demo]

tmux after Server.sessions: unchanged.

%2 sh
[demo]

tmux after Session.windows: unchanged.

%2 sh
[demo]

tmux after Window.panes: unchanged.

%2 sh
[demo]

tmux after Window.panes: unchanged.

%2 sh
[demo]

tmux after Pane.window: unchanged.

%2 sh
[demo]

tmux after Window.session: unchanged.

No sessions

tmux after Server.kill: the server has exited.

  1. Before the first call

    No tmux server is running yet.

  2. Server.startable_at_socket_path returned expected<Server, CommandFailure>

    {
    "socketPath": "/tmp/ltx-cxx-home-AbC123/tmux.sock"
    }

    The call sent no tmux command.

  3. Server.new_session returned expected<Session, CommandFailure>

    {
    "id": "$0",
    "name": "demo"
    }
    1 tmux command, 1 that changes tmux
    1. new-session -d -P -s demo -F #{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_attached}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_windows}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_path}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_created}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_group}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_grouped}}␞ -- /bin/sh $0␞demo␞0␞1␞/tmp/marked␞1793888732␞␞0␞

    Before these, the library asked tmux about itself 2 times (its version, its daemon, how a control client reads escapes).

  4. Session.active_window returned expected<Window, CommandFailure>

    {
    "id": "@0",
    "index": 0,
    "name": "sh"
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; display-message -p -t $0 #{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_panes}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_width}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_height}}␞#{s/␞/␛S/:#… @0␞sh␞1␞$0␞0␞1␞80␞24␞{"V":2,"L":{"t":"p","w":80,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%0"}}␞0␞0␞0␞1␞

    Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).

  5. Window.rename returned expected<void, CommandFailure>

    null
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; rename-window -t $0:@0 -- editor no output
  6. Window.active_pane returned expected<Pane, CommandFailure>

    {
    "id": "%0"
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; display-message -p -t $0:@0 #{s/␞/␛S/:#{s/␛/␛E/:pane_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_current_command}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_title}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_pid}}␞#{s/␞/␛S/:#{s/␛… %0␞sh␞1␞@0␞$0␞0␞host␞3180109␞/dev/pts/14␞/tmp/marked␞80␞24␞0␞0␞1␞1␞1␞1␞0␞0␞0␞␞
  7. Pane.split returned expected<Pane, CommandFailure>

    {
    "id": "%1"
    }
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; split-window -t %0 -P -d -h -F #{s/␞/␛S/:#{s/␛/␛E/:pane_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_current_command}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_title}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_pid}}␞#{s/␞/␛S/:#{… %1␞sh␞0␞@0␞$0␞1␞host␞3180399␞/dev/pts/16␞/tmp/marked␞39␞24␞0␞0␞1␞1␞0␞1␞0␞41␞0␞␞
  8. Session.new_window returned expected<Window, CommandFailure>

    {
    "id": "@1",
    "index": 1,
    "name": "logs"
    }
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; new-window -t $0 -P -n logs -d -F #{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_panes}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_width}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_height}}␞#{… @1␞logs␞0␞$0␞1␞1␞80␞24␞{"V":2,"L":{"t":"p","w":80,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%2"}}␞0␞0␞0␞1␞
  9. Pane.send_line returned expected<void, CommandFailure>

    null
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; send-keys -t %1 -l -- 'printf '\''hello from %s\n'\'' tmux' ; send-keys -t %1 Enter no output
  10. Pane.wait_for_text returned expected<WaitResult, CommandFailure>

    {
    "matched": true
    }
    2 tmux commands
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; display-message -p -t %1 -- '#{cursor_y} #{pane_height}' 2 24
    2. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; capture-pane -p -J -t %1 $ printf 'hello from %s\n' tmux hello from tmux $
  11. Pane.select returned expected<void, CommandFailure>

    null
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; select-pane -t %1 no output
  12. Window.select returned expected<void, CommandFailure>

    null
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; select-window -t $0:@1 no output
  13. Server.sessions returned expected<std::vector<Session>, CommandFailure>

    {
    "count": 1
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; list-sessions -F #{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_attached}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_windows}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_path}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_created}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_group}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_grouped}}␞ $0␞demo␞0␞2␞/tmp/marked␞1793888732␞␞0␞
  14. Session.windows returned expected<std::vector<Window>, CommandFailure>

    {
    "count": 2
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; list-windows -t $0 -F #{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_panes}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_width}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_height}}␞#{s/␞/␛S/:#{s/… @0␞editor␞0␞$0␞0␞2␞80␞24␞{"V":2,"L":{"t":"h","w":80,"h":24,"x":0,"y":0,"c":[{"t":"p","w":40,"h":24,"x":0,"y":0,"l":0,"i":0,"I":"%0"},{"t":"p","w":39,"h":24,"x":41,"y":0,"a":true,"i":1,"I":"%1"}]}}␞0␞0␞0␞1␞ @1␞logs␞1␞$0␞1␞1␞80␞24␞{"V":2,"L":{"t":"p","w":80,"h":24,"x":0,"y":0,"a":true,"i":0,"I":"%2"}}␞0␞0␞0␞1␞
  15. Window.panes returned expected<std::vector<Pane>, CommandFailure>

    {
    "count": 2
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; list-panes -t $0:@0 -F #{s/␞/␛S/:#{s/␛/␛E/:pane_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_current_command}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_title}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_pid}}␞#{s/␞/␛S/:#{s/␛/␛E/:… %0␞sh␞0␞@0␞$0␞0␞host␞3180109␞/dev/pts/14␞/tmp/marked␞40␞24␞0␞0␞1␞1␞1␞0␞0␞0␞0␞␞ %1␞sh␞1␞@0␞$0␞1␞host␞3180399␞/dev/pts/16␞/tmp/marked␞39␞24␞0␞0␞1␞1␞0␞1␞0␞41␞0␞␞
  16. Window.panes returned expected<std::vector<Pane>, CommandFailure>

    {
    "count": 1
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; list-panes -t $0:@1 -F #{s/␞/␛S/:#{s/␛/␛E/:pane_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_current_command}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_title}}␞#{s/␞/␛S/:#{s/␛/␛E/:pane_pid}}␞#{s/␞/␛S/:#{s/␛/␛E/:… %2␞sh␞1␞@1␞$0␞0␞host␞3180426␞/dev/pts/17␞/tmp/marked␞80␞24␞0␞0␞1␞1␞1␞1␞0␞0␞0␞␞
  17. Pane.window returned expected<Window, CommandFailure>

    {
    "id": "@0",
    "index": 0,
    "name": "editor"
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; display-message -p -t %1 #{s/␞/␛S/:#{s/␛/␛E/:window_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_active}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_index}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_panes}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_width}}␞#{s/␞/␛S/:#{s/␛/␛E/:window_height}}␞#{s/␞/␛S/:#… @0␞editor␞0␞$0␞0␞2␞80␞24␞{"V":2,"L":{"t":"h","w":80,"h":24,"x":0,"y":0,"c":[{"t":"p","w":40,"h":24,"x":0,"y":0,"l":0,"i":0,"I":"%0"},{"t":"p","w":39,"h":24,"x":41,"y":0,"a":true,"i":1,"I":"%1"}]}}␞0␞0␞0␞1␞
  18. Window.session returned expected<Session, CommandFailure>

    {
    "id": "$0",
    "name": "demo"
    }
    1 tmux command
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; display-message -p -t $0:@0 #{s/␞/␛S/:#{s/␛/␛E/:session_id}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_name}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_attached}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_windows}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_path}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_created}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_group}}␞#{s/␞/␛S/:#{s/␛/␛E/:session_gr… $0␞demo␞0␞2␞/tmp/marked␞1793888732␞␞0␞
  19. Server.kill returned expected<void, CommandFailure>

    null
    1 tmux command, 1 that changes tmux
    1. if-shell -F #{==:#{version},3.8a} '' libtmux-version-79582a27c4ebffb5d030f88c373bf276 ; kill-server no output
1/17
Run this example

CMake 3.25+, Clang 18 with matching libc++ development files, C++23, tmux on Linux/macOS

Save the program above as main.cpp in a new directory, along with the files below.

Cleanup is omitted; the private tmux server and its temporary files may remain. Select Cleanup to remove them when the program finishes. The program stops its private tmux server and removes any temporary files it created when it finishes. Error handling adds diagnostics; every version checks required results and deadlines.

CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
project(libtmux_api_example LANGUAGES CXX)
find_package(libtmux REQUIRED)
add_executable(api_example main.cpp)
target_link_libraries(api_example PRIVATE libtmux::libtmux)

Setup and run

# Save the files above at their displayed relative paths.
git clone https://github.com/libtmux/libtmux-cxx.git libtmux-source
git -C libtmux-source checkout --detach 888167d2d908e7f1c0e6969f96c5582edd4ac8f2
cmake -S libtmux-source -B library \
-DCMAKE_TOOLCHAIN_FILE="$PWD/libtmux-source/cmake/toolchains/clang-libcxx.cmake" \
-DCMAKE_BUILD_TYPE=Release -DLIBTMUX_BUILD_TESTS=OFF -DLIBTMUX_BUILD_EXAMPLES=OFF \
-DLIBTMUX_BUILD_TESTING_LIBRARY=OFF -DLIBTMUX_CXX_STANDARD=23 \
-DCMAKE_CXX_COMPILER=clang++
cmake --build library -j 2
cmake --install library --prefix "$PWD/prefix"
cmake -S . -B build -DCMAKE_PREFIX_PATH="$PWD/prefix" -DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_CXX_FLAGS=-stdlib=libc++ -DCMAKE_CXX_STANDARD=23
cmake --build build -j 2
build/api_example

Expected output

session demo
window 0:editor: %0 %1
window 1:logs: %2
%1 is in window editor of session demo

Install libtmux-cxx

From Git
CMakeLists.txt
vcpkg-configuration.json
Terminal
CMakeLists.txt
libtmux-cxx Not on a registry yet
include(FetchContent)
FetchContent_Declare(
  libtmux
  GIT_REPOSITORY https://github.com/libtmux/libtmux-cxx.git
  GIT_TAG        v0.1.0-alpha.10
)
FetchContent_MakeAvailable(libtmux)
target_link_libraries(your_target PRIVATE libtmux::libtmux)
{
  "registries": [
    {
      "kind": "git",
      "repository": "https://github.com/libtmux/libtmux-cxx",
      "baseline": "",
      "packages": ["libtmux"]
    }
  ]
}
$ git submodule add \
  https://github.com/libtmux/libtmux-cxx.git \
  third_party/libtmux
include(FetchContent)
FetchContent_Declare(libtmux-cxx
  GIT_REPOSITORY https://github.com/libtmux/libtmux-cxx
  GIT_TAG v0.1.0)
FetchContent_MakeAvailable(libtmux-cxx)

Name a tag or a commit, never a moving branch. Built this way the library brings nothing else: its tests, examples and MCP server all default off when it is not the top-level project.

This repository is a vcpkg git registry, not a curated-registry port. Put this in vcpkg-configuration.json, add "libtmux" to your manifest, and run vcpkg x-update-baseline to fill the baseline.

Then add_subdirectory(third_party/libtmux) and link libtmux::libtmux.

Pins the tagged source; the registry release is the default.

MCP

Give an assistant tmux tools concept Working example; may change without notice. Read more MCP home →

tmux through MCP

Replay Hover a call to preview the tmux state after it. Use its pin button to hold it. With the replay focused, the arrow keys, Home and End step through the calls.

tmux before the first call.

tmux window of 1 pane
%0 sh
[checks] 0:sh*

tmux after create_session.

tmux window of 1 pane
%0 sh
[checks] 0:sh*

tmux after list_panes: unchanged, the call only reads.

tmux window of 2 panes
%0 sh
%1 sh
[checks] 0:sh*

tmux after split_window.

tmux window of 2 panes
%0 sh
%1 sh
[checks] 0:sh*

tmux after list_windows: unchanged, the call only reads.

tmux window of 2 panes
%0 sh
$ pytest -q
..                               [100%]
2 passed in 0.01s
%1 sh
[checks] 0:sh*

tmux after run_shell_command.

tmux window of 2 panes
%0 sh
$ pytest -q
..                               [100%]
2 passed in 0.01s
%1 sh
$ cat checks.log
2026-01-01 checks started
2026-01-01 checks finished
[checks] 0:sh*

tmux after run_shell_command.

No sessions

tmux after kill_session.

You

Create a tmux session called checks, split its window into two panes, run pytest -q in one and show checks.log in the other, show me the results, then remove the session.

AgentCreate the session.

create_session Execute {"name":"checks"}
Result
{
"name": "checks",
"session_id": "$0"
}

AgentList the panes, each with its session id.

list_panes Inspect {}
Result
{
"panes": [
{
"active": true,
"command": "sh",
"dead": false,
"height": 24,
"id": "%0",
"index": 0,
"path": "/home/user/project",
"pid": 3564863,
"session_id": "$0",
"title": "host",
"width": 80,
"window_id": "@0"
}
]
}

AgentSplit the pane the list returned (%0) in two.

split_window Execute {"paneId":"%0","direction":"horizontal"}
Result
{
"pane_id": "%1"
}

AgentRead the window's layout.

list_windows Inspect {"session":"checks"}
Result
{
"windows": [
{
"active": true,
"height": 24,
"id": "@0",
"index": 0,
"layout": "8205,80x24,0,0{40x24,0,0,0,39x24,41,0,1}",
"name": "sh",
"pane_count": 2,
"session_id": "$0",
"width": 80
}
]
}

AgentRun the tests in the pane the list returned (%0).

run_shell_command Execute {"paneId":"%0","command":"pytest -q"}
Result
{
"exit_code": 0,
"pane_id": "%0",
"text": ".. [100%]\n2 passed in 0.01s\n"
}

AgentShow the log in the new pane the split returned (%1).

run_shell_command Execute {"paneId":"%1","command":"cat checks.log"}
Result
{
"exit_code": 0,
"pane_id": "%1",
"text": "2026-01-01 checks started\n2026-01-01 checks finished\n"
}

AgentRemove the session once the result is read.

kill_session Teardown {"session":"checks"}
Result
{
"deleted": true,
"session_id": "$0"
}
Agent

pytest printed 2 passed and checks.log ends with checks finished. I removed the checks session.

1/7

Install libtmux-mcp-server

Choose your client and configuration scope, then run the command or paste the configuration.

Terminal
Terminal
Terminal
claude_desktop_config.json
Terminal
.codex/config.toml
Terminal
Terminal
.cursor/mcp.json
~/.cursor/mcp.json
Terminal
Terminal
~/.gemini/config/mcp_config.json
Terminal
libtmux-mcp-server Not on a registry yet
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ claude mcp add tmux -- /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ claude mcp add tmux --scope user -- /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ claude mcp add tmux --scope project -- /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/libtmux-mcp-server"
        }
    }
}
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ codex mcp add tmux -- /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
[mcp_servers.tmux]
command = "/absolute/path/to/libtmux-mcp-server"
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ gemini mcp add --scope user tmux /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ gemini mcp add --scope project tmux /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/libtmux-mcp-server"
        }
    }
}
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/libtmux-mcp-server"
        }
    }
}
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ grok mcp add --scope user tmux -- /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ grok mcp add --scope project tmux -- /absolute/path/to/libtmux-mcp-server
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
{
    "mcpServers": {
        "tmux": {
            "command": "/absolute/path/to/libtmux-mcp-server"
        }
    }
}
First
$ cmake \
    -S . \
    -B build/mcp \
    -DLIBTMUX_BUILD_MCP_SERVER=ON \
    -DLIBTMUX_FETCH_DEPS=ON && \
cmake --build build/mcp && \
cmake --install build/mcp --prefix ~/.local
$ opencode mcp add tmux -- /absolute/path/to/libtmux-mcp-server

Writes ~/.claude.json (this project).

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes ~/.claude.json (all projects).

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes .mcp.json (in repo, version-controlled).

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes ~/.codex/config.toml.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Codex's CLI doesn't support project scope yet — paste this into .codex/config.toml at the repo root.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes ~/.gemini/settings.json.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes .gemini/settings.json (in repo).

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes ~/.grok/config.toml.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes ./.grok/config.toml (in repo).

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Writes ~/.config/opencode/opencode.jsonc.

The MCP server is a build option rather than a second package, off by default because it is the only component that needs the JSON dependency. The install above puts libtmux-mcp-server in ~/.local/bin; use its full path.

Workspace Manager

Load a workspace file concept Working example; may change without notice. Read more Workspace Manager home →

Preset
Three panesShort-hand panesTwo panesFour panesMain-horizontal layoutEnvironmentStart directorySynchronized panesBlank panesFocusMain pane height as a percentageMinimal workspacetmux optionsShell per paneSkip Enter for a commandSkip Enter for a panePause around a commandPause before a paneWait for a virtualenvKeep commands out of historyWindow index

Choose a preset 21

Three panes

A window of three panes.

session_name: 3-panes
windows:
- window_name: dev window
layout: main-vertical
shell_command_before:
- cd ~/
panes:
- shell_command:
- cd /var/log
- ls -al | grep \.log
- echo hello
- echo hello
{
"session_name": "3-panes",
"windows": [
{
"window_name": "dev window",
"layout": "main-vertical",
"shell_command_before": [
"cd ~/"
],
"panes": [
{
"shell_command": [
"cd /var/log",
"ls -al | grep \\.log"
]
},
"echo hello",
"echo hello"
]
}
]
}
Window 0, dev window: 3 panes, main-vertical❯ cd /var/log❯ ls -al | grep \.log❯ echo hello❯ echo hello
layout main-vertical

Workspace file: examples/workspace/tests/fixtures/tmuxp/3-pane.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Short-hand panes

A pane can be one command written as a string, and a command list can be one string.

session_name: shorthands
windows:
- window_name: long form
panes:
- shell_command:
- echo 'did you know'
- echo 'you can inline'
- shell_command: echo 'single commands'
- echo 'for panes'
{
"session_name": "shorthands",
"windows": [
{
"window_name": "long form",
"panes": [
{
"shell_command": [
"echo 'did you know'",
"echo 'you can inline'"
]
},
{
"shell_command": "echo 'single commands'"
},
"echo 'for panes'"
]
}
]
}
Window 0, long form: 3 panes, even-vertical❯ echo 'did you know'❯ echo 'you can inline'❯ echo 'single commands'❯ echo 'for panes'
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/shorthands.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Two panes

A window of two panes.

session_name: work
start_directory: /tmp
windows:
- window_name: editor
layout: main-vertical
panes:
- shell_command:
- echo first
- echo second
- echo lone
- window_name: logs
root: /
panes:
- shell_command: echo tail
-
{
"session_name": "work",
"start_directory": "/tmp",
"windows": [
{
"window_name": "editor",
"layout": "main-vertical",
"panes": [
{
"shell_command": [
"echo first",
"echo second"
]
},
"echo lone"
]
},
{
"window_name": "logs",
"root": "/",
"panes": [
{
"shell_command": "echo tail"
},
null
]
}
]
}
Window 0, editor: 2 panes, main-vertical❯ echo first❯ echo second❯ echo lone
layout main-vertical
Window 1, logs: 2 panes, even-vertical❯ echo tail
no layout set

Workspace file: examples/workspace/tests/tmuxp_test.cpp. Tested by ADocumentBecomesAWorkspace (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Four panes

A window of four panes.

session_name: 4-pane-split
windows:
- window_name: dev window
layout: tiled
shell_command_before:
- cd ~/
panes:
- shell_command:
- cd /var/log
- ls -al | grep \.log
- echo hello
- echo hello
- echo hello
{
"session_name": "4-pane-split",
"windows": [
{
"window_name": "dev window",
"layout": "tiled",
"shell_command_before": [
"cd ~/"
],
"panes": [
{
"shell_command": [
"cd /var/log",
"ls -al | grep \\.log"
]
},
"echo hello",
"echo hello",
"echo hello"
]
}
]
}
Window 0, dev window: 4 panes, tiled❯ cd /var/log❯ ls -al | grep \.log❯ echo hello❯ echo hello❯ echo hello
layout tiled

Workspace file: examples/workspace/tests/fixtures/tmuxp/4-pane.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Main-horizontal layout

The main-horizontal layout gives the first pane the top of the window and splits the rest below it.

session_name: libtmux
start_directory: /tmp
shell_command_before:
- echo setup
windows:
- window_name: work
focus: True
layout: main-horizontal
options:
main-pane-height: 60%
panes:
- focus: true
- echo second
- window_name: docs
shell_command_before: echo docs-setup
panes:
- echo only
{
"session_name": "libtmux",
"start_directory": "/tmp",
"shell_command_before": [
"echo setup"
],
"windows": [
{
"window_name": "work",
"focus": true,
"layout": "main-horizontal",
"options": {
"main-pane-height": "60%"
},
"panes": [
{
"focus": true
},
"echo second"
]
},
{
"window_name": "docs",
"shell_command_before": "echo docs-setup",
"panes": [
"echo only"
]
}
]
}
Window 0, work: 2 panes, main-horizontal❯ echo second
layout main-horizontal
Window 1, docs: 1 pane, even-vertical❯ echo only
no layout set

Workspace file: examples/workspace/tests/tmuxp_test.cpp. Tested by EveryKeyTheDocumentCarriesReachesTheWorkspace (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Environment

environment sets variables for a session, a window or a pane.

session_name: Environment variables test
environment:
EDITOR: /usr/bin/vim
DJANGO_SETTINGS_MODULE: my_app.settings.local
SERVER_PORT: "8009"
windows:
- window_name: Django project
panes:
- ./manage.py runserver 0.0.0.0:${SERVER_PORT}
- window_name: Another Django project
environment:
DJANGO_SETTINGS_MODULE: my_app.settings.local
SERVER_PORT: "8010"
panes:
- ./manage.py runserver 0.0.0.0:${SERVER_PORT}
- environment:
DJANGO_SETTINGS_MODULE: my_app.settings.local-testing
SERVER_PORT: "8011"
shell_command: ./manage.py runserver 0.0.0.0:${SERVER_PORT}
{
"session_name": "Environment variables test",
"environment": {
"EDITOR": "/usr/bin/vim",
"DJANGO_SETTINGS_MODULE": "my_app.settings.local",
"SERVER_PORT": "8009"
},
"windows": [
{
"window_name": "Django project",
"panes": [
"./manage.py runserver 0.0.0.0:${SERVER_PORT}"
]
},
{
"window_name": "Another Django project",
"environment": {
"DJANGO_SETTINGS_MODULE": "my_app.settings.local",
"SERVER_PORT": "8010"
},
"panes": [
"./manage.py runserver 0.0.0.0:${SERVER_PORT}",
{
"environment": {
"DJANGO_SETTINGS_MODULE": "my_app.settings.local-testing",
"SERVER_PORT": "8011"
},
"shell_command": "./manage.py runserver 0.0.0.0:${SERVER_PORT}"
}
]
}
]
}
Window 0, Django project: 1 pane, even-vertical❯ ./manage.py runserver 0.0.0.0:${SERVER_PORT}
no layout set
Window 1, Another Django project: 2 panes, even-vertical❯ ./manage.py runserver 0.0.0.0:${SERVER_PORT}❯ ./manage.py runserver 0.0.0.0:${SERVER_PORT}
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/session-environment.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Start directory

start_directory chooses where a session, a window or a pane starts.

session_name: start directory
start_directory: /var/
windows:
- window_name: should be /var/
panes:
- shell_command:
- echo "\033c
- it trickles down from session-level"
- echo hello
- window_name: should be /var/log
start_directory: log
panes:
- shell_command:
- echo '\033c
- window start_directory concatenates to session start_directory
- if it is not absolute'
- echo hello
- window_name: should be ~
start_directory: "~"
panes:
- shell_command:
- 'echo \\033c ~ has precedence. note: remember to quote ~ in YAML'
- echo hello
- window_name: should be /bin
start_directory: /bin
panes:
- echo '\033c absolute paths also have precedence.'
- echo hello
- window_name: should be workspace file's dir
start_directory: ./
panes:
- shell_command:
- echo '\033c
- ./ is relative to workspace file location
- ../ will be parent of workspace file
- ./test will be \"test\" dir inside dir of workspace file'
- shell_command:
- echo '\033c
- This way you can load up workspaces from projects and maintain
- relative paths.'
{
"session_name": "start directory",
"start_directory": "/var/",
"windows": [
{
"window_name": "should be /var/",
"panes": [
{
"shell_command": [
"echo \"\\033c",
"it trickles down from session-level\""
]
},
"echo hello"
]
},
{
"window_name": "should be /var/log",
"start_directory": "log",
"panes": [
{
"shell_command": [
"echo '\\033c",
"window start_directory concatenates to session start_directory",
"if it is not absolute'"
]
},
"echo hello"
]
},
{
"window_name": "should be ~",
"start_directory": "~",
"panes": [
{
"shell_command": [
"echo \\\\033c ~ has precedence. note: remember to quote ~ in YAML"
]
},
"echo hello"
]
},
{
"window_name": "should be /bin",
"start_directory": "/bin",
"panes": [
"echo '\\033c absolute paths also have precedence.'",
"echo hello"
]
},
{
"window_name": "should be workspace file's dir",
"start_directory": "./",
"panes": [
{
"shell_command": [
"echo '\\033c",
"./ is relative to workspace file location",
"../ will be parent of workspace file",
"./test will be \\\"test\\\" dir inside dir of workspace file'"
]
},
{
"shell_command": [
"echo '\\033c",
"This way you can load up workspaces from projects and maintain",
"relative paths.'"
]
}
]
}
]
}
Window 0, should be /var/: 2 panes, even-vertical❯ echo "\033c❯ it trickles down from session-level"❯ echo hello
no layout set
Window 1, should be /var/log: 2 panes, even-vertical❯ echo '\033c❯ window start_directory concatenates to session start_directory❯ if it is not absolute'❯ echo hello
no layout set
Window 2, should be ~: 2 panes, even-vertical❯ echo \\033c ~ has precedence. note: remember to quote ~ in YAML❯ echo hello
no layout set
Window 3, should be /bin: 2 panes, even-vertical❯ echo '\033c absolute paths also have precedence.'❯ echo hello
no layout set
Window 4, should be workspace file's dir: 2 panes, even-vertical❯ echo '\033c❯ ./ is relative to workspace file location❯ ../ will be parent of workspace file❯ ./test will be \"test\" dir inside dir of workspace file'❯ echo '\033c❯ This way you can load up workspaces from projects and maintain❯ relative paths.'
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/start-directory.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Synchronized panes

options_after sets synchronize-panes once the panes exist, so typing in one pane reaches the others.

session_name: 2-pane-synchronized
windows:
- window_name: Two synchronized panes
panes:
- ssh server1
- ssh server2
options_after:
synchronize-panes: on
{
"session_name": "2-pane-synchronized",
"windows": [
{
"window_name": "Two synchronized panes",
"panes": [
"ssh server1",
"ssh server2"
],
"options_after": {
"synchronize-panes": "on"
}
}
]
}
Window 0, Two synchronized panes: 2 panes, even-vertical❯ ssh server1❯ ssh server2
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/2-pane-synchronized.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Blank panes

An empty entry, null, pane or blank opens a pane with no command.

session_name: Blank pane test
windows:
# Emptiness will simply open a blank pane, if no shell_command_before.
# All these are equivalent
- window_name: Blank pane test
panes:
-
- pane
- blank
- window_name: More blank panes
panes:
- null
- shell_command:
- shell_command:
-
# an empty string will be treated as a carriage return
- window_name: Empty string (return)
panes:
- ""
- shell_command: ""
- shell_command:
- ""
# a pane can have other options but still be blank
- window_name: Blank with options
panes:
- focus: true
- start_directory: /tmp
{
"session_name": "Blank pane test",
"windows": [
{
"window_name": "Blank pane test",
"panes": [
null,
"pane",
"blank"
]
},
{
"window_name": "More blank panes",
"panes": [
null,
{
"shell_command": null
},
{
"shell_command": [
null
]
}
]
},
{
"window_name": "Empty string (return)",
"panes": [
"",
{
"shell_command": ""
},
{
"shell_command": [
""
]
}
]
},
{
"window_name": "Blank with options",
"panes": [
{
"focus": true
},
{
"start_directory": "/tmp"
}
]
}
]
}
Window 0, Blank pane test: 3 panes, even-vertical
no layout set
Window 1, More blank panes: 3 panes, even-vertical
no layout set
Window 2, Empty string (return): 3 panes, even-vertical
no layout set
Window 3, Blank with options: 2 panes, even-vertical
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/blank-panes.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Focus

focus: true selects a window or a pane when the session is built.

session_name: focus
windows:
- window_name: attached window
focus: true
panes:
- shell_command:
- echo hello
- echo 'this pane should be selected on load'
focus: true
- shell_command:
- cd /var/log
- echo hello
- window_name: second window
shell_command_before: cd /var/log
panes:
- pane
- shell_command:
- echo 'this pane should be focused, when window switched to first time'
focus: true
- pane
{
"session_name": "focus",
"windows": [
{
"window_name": "attached window",
"focus": true,
"panes": [
{
"shell_command": [
"echo hello",
"echo 'this pane should be selected on load'"
],
"focus": true
},
{
"shell_command": [
"cd /var/log",
"echo hello"
]
}
]
},
{
"window_name": "second window",
"shell_command_before": "cd /var/log",
"panes": [
"pane",
{
"shell_command": [
"echo 'this pane should be focused, when window switched to first time'"
],
"focus": true
},
"pane"
]
}
]
}
Window 0, attached window: 2 panes, even-vertical❯ echo hello❯ echo 'this pane should be selected on load'❯ cd /var/log❯ echo hello
no layout set
Window 1, second window: 3 panes, even-vertical❯ echo 'this pane should be focused, when window switched to first time'
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/focus-window-and-panes.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Main pane height as a percentage

main-pane-height takes a percentage of the window as well as a row count.

session_name: main-pane-height
start_directory: "~"
windows:
- layout: main-horizontal
options:
main-pane-height: 67%
panes:
- shell_command:
- top
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
window_name: my window name
{
"session_name": "main-pane-height",
"start_directory": "~",
"windows": [
{
"layout": "main-horizontal",
"options": {
"main-pane-height": "67%"
},
"panes": [
{
"shell_command": [
"top"
],
"start_directory": "~"
},
{
"shell_command": [
"echo \"hey\""
]
},
{
"shell_command": [
"echo \"moo\""
]
}
],
"window_name": "my window name"
}
]
}
Window 0, my window name: 3 panes, main-horizontal❯ top❯ echo "hey"❯ echo "moo"
layout main-horizontal

Workspace file: examples/workspace/tests/fixtures/tmuxp/main-pane-height-percentage.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Minimal workspace

A session name and one window with one blank pane.

session_name: My tmux session
windows:
- panes:
-
{
"session_name": "My tmux session",
"windows": [
{
"panes": [
null
]
}
]
}
Window 0: 1 pane, even-vertical
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/minimal.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

tmux options

global_options, options and a window's options set tmux options.

session_name: test window options
start_directory: "~"
global_options:
default-shell: /bin/sh
default-command: /bin/sh
options:
main-pane-height: ${MAIN_PANE_HEIGHT} # works with env variables
windows:
- layout: main-horizontal
options:
automatic-rename: on
panes:
- shell_command:
- man echo
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
{
"session_name": "test window options",
"start_directory": "~",
"global_options": {
"default-shell": "/bin/sh",
"default-command": "/bin/sh"
},
"options": {
"main-pane-height": "${MAIN_PANE_HEIGHT}"
},
"windows": [
{
"layout": "main-horizontal",
"options": {
"automatic-rename": "on"
},
"panes": [
{
"shell_command": [
"man echo"
],
"start_directory": "~"
},
{
"shell_command": [
"echo \"hey\""
]
},
{
"shell_command": [
"echo \"moo\""
]
}
]
}
]
}
Window 0: 3 panes, main-horizontal❯ man echo❯ echo "hey"❯ echo "moo"
layout main-horizontal

Workspace file: examples/workspace/tests/fixtures/tmuxp/options.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Shell per pane

A pane can start its own shell or program, and window_shell sets the default for a window.

session_name: Pane shell example
windows:
- window_name: first
window_shell: /usr/bin/python2
layout: even-vertical
suppress_history: false
options:
remain-on-exit: true
panes:
- shell: /usr/bin/python3
shell_command:
- print('This is python 3')
- shell: /usr/bin/vim -u none
shell_command:
- iAll panes have the `remain-on-exit` setting on.
- When you exit out of the shell or application, the panes will remain.
- Use tmux command `:kill-pane` to remove the pane.
- Use tmux command `:respawn-pane` to restart the shell in the pane.
- Use <Escape> and then `:q!` to get out of this vim window. :-)
- shell_command:
- print('Hello World 2')
- shell: /usr/bin/top
{
"session_name": "Pane shell example",
"windows": [
{
"window_name": "first",
"window_shell": "/usr/bin/python2",
"layout": "even-vertical",
"suppress_history": false,
"options": {
"remain-on-exit": true
},
"panes": [
{
"shell": "/usr/bin/python3",
"shell_command": [
"print('This is python 3')"
]
},
{
"shell": "/usr/bin/vim -u none",
"shell_command": [
"iAll panes have the `remain-on-exit` setting on.",
"When you exit out of the shell or application, the panes will remain.",
"Use tmux command `:kill-pane` to remove the pane.",
"Use tmux command `:respawn-pane` to restart the shell in the pane.",
"Use <Escape> and then `:q!` to get out of this vim window. :-)"
]
},
{
"shell_command": [
"print('Hello World 2')"
]
},
{
"shell": "/usr/bin/top"
}
]
}
]
}
Window 0, first: 4 panes, even-vertical❯ print('This is python 3')❯ iAll panes have the `remain-on-exit` setting on.❯ When you exit out of the shell or application, the panes will remain.❯ Use tmux command `:kill-pane` to remove the pane.❯ Use tmux command `:respawn-pane` to restart the shell in the pane.❯ Use <Escape> and then `:q!` to get out of this vim window. :-)❯ print('Hello World 2')
layout even-vertical

Workspace file: examples/workspace/tests/fixtures/tmuxp/pane-shell.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Skip Enter for a command

enter: false types a command without running it.

session_name: Skip command execution (command-level)
windows:
- panes:
- shell_command:
# You can see this
- echo "___$((11 + 1))___"
# This is skipped
- cmd: echo "___$((1 + 3))___"
enter: false
{
"session_name": "Skip command execution (command-level)",
"windows": [
{
"panes": [
{
"shell_command": [
"echo \"___$((11 + 1))___\"",
{
"cmd": "echo \"___$((1 + 3))___\"",
"enter": false
}
]
}
]
}
]
}
Window 0: 1 pane, even-vertical❯ echo "___$((11 + 1))___"❯ echo "___$((1 + 3))___"
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/skip-send.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Skip Enter for a pane

enter: false on a pane types its commands without running them.

session_name: Skip command execution (pane-level)
windows:
- panes:
- shell_command: echo "___$((1 + 3))___"
enter: false
- shell_command:
- echo "___$((1 + 3))___"\;
- echo "___$((1 + 3))___"
enter: false
{
"session_name": "Skip command execution (pane-level)",
"windows": [
{
"panes": [
{
"shell_command": "echo \"___$((1 + 3))___\"",
"enter": false
},
{
"shell_command": [
"echo \"___$((1 + 3))___\"\\;",
"echo \"___$((1 + 3))___\""
],
"enter": false
}
]
}
]
}
Window 0: 2 panes, even-vertical❯ echo "___$((1 + 3))___"❯ echo "___$((1 + 3))___"\;❯ echo "___$((1 + 3))___"
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/skip-send-pane-level.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Pause around a command

sleep_before and sleep_after delay a command or the one after it.

session_name: Pause / skip command execution (command-level)
windows:
- panes:
- shell_command:
# Executes immediately
- echo "___$((11 + 1))___"
# Delays before sending 2 seconds
- cmd: echo "___$((1 + 3))___"
sleep_before: 2
# Executes immediately
- cmd: echo "___$((1 + 3))___"
# Pauses 2 seconds after
- cmd: echo "Stuff rendering here!"
sleep_after: 2
# Executes after earlier commands (after 2 sec)
- cmd: echo "2 seconds later"
{
"session_name": "Pause / skip command execution (command-level)",
"windows": [
{
"panes": [
{
"shell_command": [
"echo \"___$((11 + 1))___\"",
{
"cmd": "echo \"___$((1 + 3))___\"",
"sleep_before": 2
},
{
"cmd": "echo \"___$((1 + 3))___\""
},
{
"cmd": "echo \"Stuff rendering here!\"",
"sleep_after": 2
},
{
"cmd": "echo \"2 seconds later\""
}
]
}
]
}
]
}
Window 0: 1 pane, even-vertical❯ echo "___$((11 + 1))___"❯ echo "___$((1 + 3))___"❯ echo "___$((1 + 3))___"❯ echo "Stuff rendering here!"❯ echo "2 seconds later"
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/sleep.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Pause before a pane

sleep_before on a pane waits before it sends any of its commands.

session_name: Pause / skip command execution (pane-level)
windows:
- panes:
- # Wait 2 seconds before sending all commands in this pane
sleep_before: 2
shell_command:
- echo "___$((11 + 1))___"
- cmd: echo "___$((1 + 3))___"
- cmd: echo "___$((1 + 3))___"
- cmd: echo "Stuff rendering here!"
- cmd: echo "2 seconds later"
{
"session_name": "Pause / skip command execution (pane-level)",
"windows": [
{
"panes": [
{
"sleep_before": 2,
"shell_command": [
"echo \"___$((11 + 1))___\"",
{
"cmd": "echo \"___$((1 + 3))___\""
},
{
"cmd": "echo \"___$((1 + 3))___\""
},
{
"cmd": "echo \"Stuff rendering here!\""
},
{
"cmd": "echo \"2 seconds later\""
}
]
}
]
}
]
}
Window 0: 1 pane, even-vertical❯ echo "___$((11 + 1))___"❯ echo "___$((1 + 3))___"❯ echo "___$((1 + 3))___"❯ echo "Stuff rendering here!"❯ echo "2 seconds later"
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/sleep-pane-level.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Wait for a virtualenv

A setup command with sleep_before and sleep_after gives a virtualenv time to activate before the commands run.

session_name: virtualenv
shell_command_before:
# - cmd: source $(poetry env info --path)/bin/activate
# - cmd: source `pipenv --venv`/bin/activate
- cmd: source .venv/bin/activate
sleep_before: 1
sleep_after: 1
windows:
- panes:
- shell_command:
- ./manage.py runserver
{
"session_name": "virtualenv",
"shell_command_before": [
{
"cmd": "source .venv/bin/activate",
"sleep_before": 1,
"sleep_after": 1
}
],
"windows": [
{
"panes": [
{
"shell_command": [
"./manage.py runserver"
]
}
]
}
]
}
Window 0: 1 pane, even-vertical❯ ./manage.py runserver
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/sleep-virtualenv.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Keep commands out of history

suppress_history chooses whether commands reach the shell history, per session, window, pane or command.

session_name: suppress
suppress_history: false
windows:
- window_name: appended
focus: true
suppress_history: false
panes:
- echo "window in the history!"
- window_name: suppressed
suppress_history: true
panes:
- echo "window not in the history!"
- window_name: default
panes:
- echo "session in the history!"
- window_name: mixed
suppress_history: false
panes:
- shell_command:
- echo "command in the history!"
suppress_history: false
- shell_command:
- echo "command not in the history!"
suppress_history: true
- shell_command:
- echo "window in the history!"
{
"session_name": "suppress",
"suppress_history": false,
"windows": [
{
"window_name": "appended",
"focus": true,
"suppress_history": false,
"panes": [
"echo \"window in the history!\""
]
},
{
"window_name": "suppressed",
"suppress_history": true,
"panes": [
"echo \"window not in the history!\""
]
},
{
"window_name": "default",
"panes": [
"echo \"session in the history!\""
]
},
{
"window_name": "mixed",
"suppress_history": false,
"panes": [
{
"shell_command": [
"echo \"command in the history!\""
],
"suppress_history": false
},
{
"shell_command": [
"echo \"command not in the history!\""
],
"suppress_history": true
},
{
"shell_command": [
"echo \"window in the history!\""
]
}
]
}
]
}
Window 0, appended: 1 pane, even-vertical❯ echo "window in the history!"
no layout set
Window 1, suppressed: 1 pane, even-vertical❯ echo "window not in the history!"
no layout set
Window 2, default: 1 pane, even-vertical❯ echo "session in the history!"
no layout set
Window 3, mixed: 3 panes, even-vertical❯ echo "command in the history!"❯ echo "command not in the history!"❯ echo "window in the history!"
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/suppress-history.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Window index

window_index places a window at an index; a window without one takes the lowest free index.

session_name: Window index example
windows:
- window_name: zero
panes:
- echo "this window's index will be zero"
- window_name: five
panes:
- echo "this window's index will be five"
window_index: 5
- window_name: one
panes:
- echo "this window's index will be one"
{
"session_name": "Window index example",
"windows": [
{
"window_name": "zero",
"panes": [
"echo \"this window's index will be zero\""
]
},
{
"window_name": "five",
"panes": [
"echo \"this window's index will be five\""
],
"window_index": 5
},
{
"window_name": "one",
"panes": [
"echo \"this window's index will be one\""
]
}
]
}
Window 0, zero: 1 pane, even-vertical❯ echo "this window's index will be zero"
no layout set
Window 1, one: 1 pane, even-vertical❯ echo "this window's index will be one"
no layout set
Window 5, five: 1 pane, even-vertical❯ echo "this window's index will be five"
no layout set

Workspace file: examples/workspace/tests/fixtures/tmuxp/window-index.yaml. Tested by EveryTmuxpExampleParses (examples/workspace/tests/tmuxp_test.cpp; loaded by the library in a test; the command is not run).

Install tmux-workspace

The workspace package is separate from the core library.

CMake
Terminal
tmux-workspace Not on a registry yet
$ cmake --preset cxx-dev -DLIBTMUX_BUILD_WORKSPACE_CLI=ON && \
    cmake --build --preset cxx-dev --target tmux-workspace --parallel 2
Then
$ tmux-workspace load ./workspace.yaml

Run from a libtmux-cxx checkout with Clang 18.1.3, libc++ 18.1, CMake 3.25 or newer, and Ninja. The command is build/cxx-dev/apps/workspace/tmux-workspace.

Packages and integrations

API references

Esc
Search in

Type to search.

Reference previewOpen reference