Layouts and repeated setup
Build a tmux layout from Kotlin by creating a window, splitting a pane and selecting a layout. Capture again to inspect the result. The examples use the library APIs directly and give every pane a command that stays alive.
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-kotlin 0.1.2 from Maven Central (source).
rootProject.name = "connect"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.1.2")}kotlin { jvmToolchain(25) sourceSets.main { kotlin.srcDir("."); kotlin.include("${example}.kt") }}application { mainClass.set("${example}Kt") }org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8kotlin.daemon.jvmargs=-Xmx2gorg.gradle.workers.max=2Each 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.
Create a two-pane tools window
Create tools in work-one, split its pane to the right, then choose even-horizontal. The final read verifies two panes. The previously captured session does not automatically gain the new window.
import io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.kotlin.*import io.github.libtmux.kotlin.query.*import java.nio.file.Filesimport java.nio.file.Pathimport java.time.Durationimport kotlinx.coroutines.NonCancellableimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.withContext
suspend fun demonstrate(server: Server) { val session = server.sessions().single { it.name == "work-one" } val window = session.newWindow( io.github.libtmux.WindowSpec.builder() .named("tools").running("/bin/cat").build() ) val original = window.panes.single() original.split( io.github.libtmux.SplitSpec.builder() .toRight().running("/bin/cat").build() ) window.selectLayout(io.github.libtmux.Layout.EVEN_HORIZONTAL) val refreshed = server.sessions().single { it.name == "work-one" } val tools = refreshed.windows.single { it.name == "tools" } check(tools.panes.size == 2) println("tools: 2 panes")}
fun main() = runBlocking { 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 { withServer(config) { server -> try { server.newSession { name = "work-one" window { name = "editor" running("/bin/cat") } } server.newSession { name = "work-two" window { name = "logs" running("/bin/cat") } } demonstrate(server) } finally { withContext(NonCancellable) { if (Files.exists(socket)) server.killServer() stopped = true } } } } finally { if (stopped || !Files.exists(socket)) { Files.deleteIfExists(socket) Files.delete(directory) } else { System.err.println("Cannot stop tmux; kept $directory") } }}$ gradle --console=plain --no-daemon run -Pexample=LayoutExpected program output:
tools: 2 panesReuse a named window
Look for tools before creating it. Two sequential calls return the same window ID, and the session has only editor and tools. This is useful for a setup command you run repeatedly.
import io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.kotlin.*import io.github.libtmux.kotlin.query.*import java.nio.file.Filesimport java.nio.file.Pathimport java.time.Durationimport kotlinx.coroutines.NonCancellableimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.withContext
suspend fun demonstrate(server: Server) { suspend fun ensureTools(): Window { val session = server.sessions().single { it.name == "work-one" } val existing = session.windows.singleOrNull { it.name == "tools" } if (existing != null) return existing return session.newWindow( io.github.libtmux.WindowSpec.builder() .named("tools").running("/bin/cat").build() ) } val first = ensureTools() val second = ensureTools() check(first.id == second.id) val session = server.sessions().single { it.name == "work-one" } check(session.windows.size == 2) println("one tools window after two calls")}
fun main() = runBlocking { 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 { withServer(config) { server -> try { server.newSession { name = "work-one" window { name = "editor" running("/bin/cat") } } server.newSession { name = "work-two" window { name = "logs" running("/bin/cat") } } demonstrate(server) } finally { withContext(NonCancellable) { if (Files.exists(socket)) server.killServer() stopped = true } } } } finally { if (stopped || !Files.exists(socket)) { Files.deleteIfExists(socket) Files.delete(directory) } else { System.err.println("Cannot stop tmux; kept $directory") } }}$ gradle --console=plain --no-daemon run -Pexample=ReuseLayoutExpected program output:
one tools window after two callsDecide what repeated setup means
The reuse example preserves the existing window and its running processes. It does not reset its pane count, layout or commands. Choose that policy deliberately when building a reusable workspace command.
This check-then-create sequence assumes one writer. Concurrent callers can both observe an absent window and create duplicates. Serialize setup in your application when several callers share a session. A later failure also leaves earlier successful mutations in place; tmux commands are not a transaction.
Use filtering and queries for stricter target selection and captured handles to understand refresh behavior.