Core LibraryConcepts

Choose documentation 1

latest

Current version

latest
English

Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

Edit this page on GitHub

Server, session, window, pane

Capture a hierarchy, then read its sessions, windows and panes locally. Handles retain the state they captured; a rename does not rewrite an older handle. Use a new capture to observe later changes.

Server selects the socket and owns client resources. Session, Window and Pane hold captured state and send mutations through that server. The session list captures the hierarchy; walking its children reads that capture.

A session holds window placements; a window holds panes. A linked window can appear in several sessions, so traversal counts placements rather than necessarily distinct physical windows. Names can change; retain IDs when identifying a target.

Setup and run

Use an empty directory on Linux with Git, tmux 3.2a or newer, and JDK 25. Save the project files and launcher below, then save any complete program on this page. Each program has its own imports and entry point.

The pinned source checkout supplies the Gradle wrapper and the library dependency.

settings.gradle.kts
rootProject.name = "connect"
includeBuild("libtmux-source")
build.gradle.kts
val example = providers.gradleProperty("example").getOrElse("Local")
plugins {
application
kotlin("jvm") version "2.4.10"
}
repositories { mavenCentral() }
dependencies {
implementation("io.github.libtmux:libtmux-kotlin:0.0.1-alpha.17-SNAPSHOT")
}
kotlin {
jvmToolchain(25)
sourceSets.main { kotlin.srcDir("."); kotlin.include("${example}.kt") }
}
application { mainClass.set("${example}Kt") }
gradle.properties
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8
kotlin.daemon.jvmargs=-Xmx2g
org.gradle.workers.max=2

The launcher creates two sessions on a private socket: work-one with an editor window, and work-two with a logs window. Each pane runs cat so it stays alive. It stops only this server when the program finishes or fails. If shutdown fails, it reports the retained directory and exits with an error.

run.sh
#!/bin/sh
set -eu
binary=$(command -v tmux)
mkdir -p /tmp/libtmux-java-dev
directory=$(mktemp -d /tmp/libtmux-java-dev/query.XXXXXX)
socket="$directory/tmux.sock"
cleanup() {
status=$?
trap - 0 HUP INT TERM
if [ -S "$socket" ] && ! "$binary" -S "$socket" kill-server; then
printf 'Cannot stop tmux; kept %s\n' "$directory" >&2
exit 1
fi
rm -rf "$directory" || exit 1
exit "$status"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM
unset TMUX TMUX_PANE
export LIBTMUX_SOCKET_PATH="$socket" TMUX_BIN="$binary"
"$binary" -S "$socket" -f /dev/null new-session -d -s work-one -n editor /bin/cat
"$binary" -S "$socket" new-session -d -s work-two -n logs /bin/cat
"$@"
"$binary" -S "$socket" has-session -t '=work-one'

Fetch the library revision used by these examples:

Terminal window
$ git clone https://github.com/libtmux/libtmux-java libtmux-source &&
git -C libtmux-source checkout be1d62fbaa1aa634c687e1c50bceeb09ef0b8a15

Each run starts from the same two-session fixture. Programs do not depend on another example having run first. Their assertions fail if the observed result differs.

Walk sessions, windows and panes

Flatten the captured children and verify the fixture contains two of each. The program also prints each session and its window.

Hierarchy.kt
import io.github.libtmux.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.kotlin.*
import io.github.libtmux.kotlin.query.*
import java.nio.file.Path
import java.time.Duration
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val socket = requireNotNull(System.getenv("LIBTMUX_SOCKET_PATH")) {
"Set LIBTMUX_SOCKET_PATH to an existing socket"
}
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.socketPath(Path.of(socket)))
.defaultTimeout(Duration.ofSeconds(5))
.build()
withServer(config) { server ->
val sessions = server.sessions()
val windows = sessions.flatMap { it.windows }
val panes = windows.flatMap { it.panes }
check(sessions.size == 2 && windows.size == 2 && panes.size == 2)
for (session in sessions) {
println("${session.name}: ${session.windows.single().name}")
}
println("2 sessions, 2 windows, 2 panes")
}
}
Terminal window
$ sh run.sh ./libtmux-source/gradlew --project-dir . run \
-Pexample=Hierarchy --console=plain --max-workers=2

Expected program output:

work-one: editor
work-two: logs
2 sessions, 2 windows, 2 panes

Observe a rename with a fresh read

Rename the editor window. The old handle still says editor; the returned handle and a new server read say renamed. This distinction matters when a UI, another client or your own code changes tmux after a capture.

Refresh.kt
import io.github.libtmux.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.kotlin.*
import io.github.libtmux.kotlin.query.*
import java.nio.file.Path
import java.time.Duration
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val socket = requireNotNull(System.getenv("LIBTMUX_SOCKET_PATH")) {
"Set LIBTMUX_SOCKET_PATH to an existing socket"
}
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.socketPath(Path.of(socket)))
.defaultTimeout(Duration.ofSeconds(5))
.build()
withServer(config) { server ->
val session = server.sessions().single { it.name == "work-one" }
val before = session.windows.single()
val after = before.rename("renamed")
check(before.name == "editor")
check(after.name == "renamed")
val readAgain = server.sessions().single { it.name == "work-one" }.windows.single()
check(readAgain.name == "renamed")
println("${before.name} -> ${readAgain.name}")
}
}
Terminal window
$ sh run.sh ./libtmux-source/gradlew --project-dir . run \
-Pexample=Refresh --console=plain --max-workers=2

Expected program output:

editor -> renamed

Select a target before changing it

A successful lookup does not reserve a tmux object. Another client can remove it before your next command. Handle a command failure at the mutation, and refresh before deciding what to do next. See filtering and queries for missing and ambiguous selections, and layouts for creation.

Esc

Type to search.