Core Library0.xConcepts

Choose documentation 1

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
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 tmux 3.3a or newer, JDK 25, and Gradle 9. Save the project files below, then save any complete program on this page. Each program has its own imports and entry point.

Gradle resolves libtmux-scala_3 0.1.2 from Maven Central (source).

settings.gradle.kts
rootProject.name = "connect"
build.gradle.kts
val example = providers.gradleProperty("example").getOrElse("Local")
plugins {
application
scala
}
repositories { mavenCentral() }
dependencies {
implementation("org.scala-lang:scala3-library_3:3.9.0")
implementation("io.github.libtmux:libtmux-scala_3:0.1.2")
}
java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }
sourceSets.main { scala.srcDir("."); scala.include("${example}.scala") }
application { mainClass.set(example) }
gradle.properties
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8
kotlin.daemon.jvmargs=-Xmx2g
org.gradle.workers.max=2

Each program starts its own private tmux server with two sessions: work-one with an editor window, and work-two with a logs window. Each pane runs cat so it stays alive. The program stops only that server when it finishes or fails, then removes the server’s directory. If shutdown fails, it reports the retained directory and exits with an error.

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.scala
import io.github.libtmux.{ServerConfig, ServerEndpoint, SessionSpec}
import io.github.libtmux.scaladsl.*
import io.github.libtmux.scaladsl.query.*
import java.nio.file.{Files, Path}
import java.time.Duration
import scala.util.Using
import scala.jdk.CollectionConverters.*
object Hierarchy {
def demonstrate(server: Server): Unit = {
val sessions = server.sessions()
val windows = sessions.flatMap(_.windows)
val panes = windows.flatMap(_.panes)
assert(sessions.size == 2 && windows.size == 2 && panes.size == 2)
for (session <- sessions) {
println(s"${session.name}: ${session.windows.head.name}")
}
println("2 sessions, 2 windows, 2 panes")
}
def main(args: Array[String]): Unit = {
val directory = Files.createTempDirectory("libtmux-example-")
val socket = directory.resolve("tmux.sock")
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.socketPath(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build()
var stopped = false
try {
Using.resource(Server.open(config)) { server =>
try {
server.newSession(SessionSpec.builder()
.named("work-one").firstWindowNamed("editor")
.running("/bin/cat").build())
server.newSession(SessionSpec.builder()
.named("work-two").firstWindowNamed("logs")
.running("/bin/cat").build())
demonstrate(server)
} finally {
if (Files.exists(socket)) server.killServer()
stopped = true
}
}
} finally {
if (stopped || !Files.exists(socket)) {
Files.deleteIfExists(socket)
Files.delete(directory)
} else {
System.err.println(s"Cannot stop tmux; kept $directory")
}
}
}
}
Terminal window
$ gradle --console=plain --no-daemon run -Pexample=Hierarchy

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.scala
import io.github.libtmux.{ServerConfig, ServerEndpoint, SessionSpec}
import io.github.libtmux.scaladsl.*
import io.github.libtmux.scaladsl.query.*
import java.nio.file.{Files, Path}
import java.time.Duration
import scala.util.Using
import scala.jdk.CollectionConverters.*
object Refresh {
def demonstrate(server: Server): Unit = {
val session = server.sessions().find(_.name == "work-one").get
val before = session.windows.head
val after = before.rename("renamed")
assert(before.name == "editor")
assert(after.name == "renamed")
val readAgain = server.sessions().find(_.name == "work-one").get
.windows.head
assert(readAgain.name == "renamed")
println(s"${before.name} -> ${readAgain.name}")
}
def main(args: Array[String]): Unit = {
val directory = Files.createTempDirectory("libtmux-example-")
val socket = directory.resolve("tmux.sock")
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.socketPath(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build()
var stopped = false
try {
Using.resource(Server.open(config)) { server =>
try {
server.newSession(SessionSpec.builder()
.named("work-one").firstWindowNamed("editor")
.running("/bin/cat").build())
server.newSession(SessionSpec.builder()
.named("work-two").firstWindowNamed("logs")
.running("/bin/cat").build())
demonstrate(server)
} finally {
if (Files.exists(socket)) server.killServer()
stopped = true
}
}
} finally {
if (stopped || !Files.exists(socket)) {
Files.deleteIfExists(socket)
Files.delete(directory)
} else {
System.err.println(s"Cannot stop tmux; kept $directory")
}
}
}
}
Terminal window
$ gradle --console=plain --no-daemon run -Pexample=Refresh

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
Search in

Type to search.

Reference previewOpen reference