libtmux for Kotlin
Kotlin-based tmux client
tmux client library for Kotlin. Control tmux, connect to and create servers, navigate sessions, windows, and panes, send keys, watch for changes via control mode, capture pane contents
Built on the Java library for JVM. The packages share their source repository and releases.
import io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.SplitSpecimport io.github.libtmux.TextOutcomeimport io.github.libtmux.WindowSpecimport io.github.libtmux.kotlin.*import java.nio.file.Pathimport java.time.Durationimport java.util.UUIDimport kotlinx.coroutines.runBlockingimport kotlin.time.Duration.Companion.seconds
fun main() = runBlocking { val socket = "libtmux-home-${UUID.randomUUID()}" val config = ServerConfig.builder() .endpoint(ServerEndpoint.namedSocket(socket)) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(5)) .build()
withServer(config) { server -> val session = server.newSession("demo")
// Lay out the session: name its window, split it, // add a second window. val first = session.activeWindow ?: error("no active window") val editor = first.rename("editor") val left = editor.activePane ?: error("no active pane") val side = SplitSpec.builder().toRight().detached().build() val right = left.split(side) val tab = WindowSpec.builder().named("logs").detached().build() val logs = session.newWindow(tab)
// Type a command into the new pane and wait for what it prints. right.sendLine("printf 'hello from %s\\n' tmux") val outcome = right.awaitText( "hello from tmux", timeout = 5.seconds ) check(outcome != TextOutcome.TIMED_OUT)
// Move the focus an attached client sees: // the pane, then the window. right.select() logs.select()
// Walk the objects: down from the server, then up from one pane. val sessions = server.sessions() for (each in sessions) { println("session ${each.name}") for (window in each.windows) { val ids = window.panes.joinToString(" ") { "${it.id}" } println(" window ${window.index}:${window.name}: $ids") } }
val pane = server.pane(right.id) ?: error("no pane ${right.id}") val home = pane.window val owner = home.session println( "${pane.id} is in window ${home.name} of session ${owner.name}" ) }}import io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.SplitSpecimport io.github.libtmux.TextOutcomeimport io.github.libtmux.WindowSpecimport io.github.libtmux.kotlin.*import java.nio.file.Pathimport java.time.Durationimport java.util.UUIDimport kotlinx.coroutines.runBlockingimport kotlin.time.Duration.Companion.seconds
fun main() = runBlocking { val socket = "libtmux-home-${UUID.randomUUID()}" val config = ServerConfig.builder() .endpoint(ServerEndpoint.namedSocket(socket)) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(5)) .build()
withServer(config) { server -> try { val session = server.newSession("demo")
// Lay out the session: name its window, split it, // add a second window. val first = session.activeWindow ?: error("no active window") val editor = first.rename("editor") val left = editor.activePane ?: error("no active pane") val side = SplitSpec.builder().toRight().detached().build() val right = left.split(side) val tab = WindowSpec.builder().named("logs").detached().build() val logs = session.newWindow(tab)
// Type a command into the new pane and wait for what it prints. right.sendLine("printf 'hello from %s\\n' tmux") val outcome = right.awaitText( "hello from tmux", timeout = 5.seconds ) check(outcome != TextOutcome.TIMED_OUT)
// Move the focus an attached client sees: // the pane, then the window. right.select() logs.select()
// Walk the objects: down from the server, then up from one pane. val sessions = server.sessions() for (each in sessions) { println("session ${each.name}") for (window in each.windows) { val ids = window.panes.joinToString(" ") { "${it.id}" } println(" window ${window.index}:${window.name}: $ids") } }
val pane = server.pane(right.id) ?: error("no pane ${right.id}") val home = pane.window val owner = home.session println( "${pane.id} is in window ${home.name} of session ${owner.name}" ) } catch (error: Exception) { throw IllegalStateException( "tmux quickstart failed", error ) } }}import io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.SplitSpecimport io.github.libtmux.TextOutcomeimport io.github.libtmux.WindowSpecimport io.github.libtmux.kotlin.*import java.nio.file.Pathimport java.time.Durationimport java.util.UUIDimport kotlinx.coroutines.NonCancellableimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.withContextimport kotlin.time.Duration.Companion.seconds
fun main() = runBlocking { val socket = "libtmux-home-${UUID.randomUUID()}" val config = ServerConfig.builder() .endpoint(ServerEndpoint.namedSocket(socket)) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(5)) .build()
withServer(config) { server -> try { val session = server.newSession("demo")
// Lay out the session: name its window, split it, // add a second window. val first = session.activeWindow ?: error("no active window") val editor = first.rename("editor") val left = editor.activePane ?: error("no active pane") val side = SplitSpec.builder().toRight().detached().build() val right = left.split(side) val tab = WindowSpec.builder().named("logs").detached().build() val logs = session.newWindow(tab)
// Type a command into the new pane and wait for what it prints. right.sendLine("printf 'hello from %s\\n' tmux") val outcome = right.awaitText( "hello from tmux", timeout = 5.seconds ) check(outcome != TextOutcome.TIMED_OUT)
// Move the focus an attached client sees: // the pane, then the window. right.select() logs.select()
// Walk the objects: down from the server, then up from one pane. val sessions = server.sessions() for (each in sessions) { println("session ${each.name}") for (window in each.windows) { val ids = window.panes.joinToString(" ") { "${it.id}" } println(" window ${window.index}:${window.name}: $ids") } }
val pane = server.pane(right.id) ?: error("no pane ${right.id}") val home = pane.window val owner = home.session println( "${pane.id} is in window ${home.name} of session ${owner.name}" ) } finally { withContext(NonCancellable) { server.killServer() } } }}import io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.SplitSpecimport io.github.libtmux.TextOutcomeimport io.github.libtmux.WindowSpecimport io.github.libtmux.kotlin.*import java.nio.file.Pathimport java.time.Durationimport java.util.UUIDimport kotlinx.coroutines.NonCancellableimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.withContextimport kotlin.time.Duration.Companion.seconds
fun main() = runBlocking { val socket = "libtmux-home-${UUID.randomUUID()}" val config = ServerConfig.builder() .endpoint(ServerEndpoint.namedSocket(socket)) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(5)) .build()
withServer(config) { server -> try { val session = server.newSession("demo")
// Lay out the session: name its window, split it, // add a second window. val first = session.activeWindow ?: error("no active window") val editor = first.rename("editor") val left = editor.activePane ?: error("no active pane") val side = SplitSpec.builder().toRight().detached().build() val right = left.split(side) val tab = WindowSpec.builder().named("logs").detached().build() val logs = session.newWindow(tab)
// Type a command into the new pane and wait for what it prints. right.sendLine("printf 'hello from %s\\n' tmux") val outcome = right.awaitText( "hello from tmux", timeout = 5.seconds ) check(outcome != TextOutcome.TIMED_OUT)
// Move the focus an attached client sees: // the pane, then the window. right.select() logs.select()
// Walk the objects: down from the server, then up from one pane. val sessions = server.sessions() for (each in sessions) { println("session ${each.name}") for (window in each.windows) { val ids = window.panes.joinToString(" ") { "${it.id}" } println(" window ${window.index}:${window.name}: $ids") } }
val pane = server.pane(right.id) ?: error("no pane ${right.id}") val home = pane.window val owner = home.session println( "${pane.id} is in window ${home.name} of session ${owner.name}" ) } catch (error: Exception) { throw IllegalStateException( "tmux quickstart failed", error ) } finally { withContext(NonCancellable) { server.killServer() } } }}tmux before the first call: no server is running.
tmux after withServer: no server is running; the call sent no command.
tmux after Server.newSession: added session demo.
tmux after Session.activeWindow: unchanged, the call sent no command.
tmux after Window.rename: renamed window 0:sh to 0:editor.
tmux after Window.activePane: unchanged, the call sent no command.
tmux after Pane.split: added pane %1 to window 0:editor.
tmux after Session.newWindow: added window 1:logs.
tmux after Pane.sendLine: sent keys to pane %1.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Pane.awaitText: read pane %1.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Pane.select: selected pane %1 in window 0:editor.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Window.select: selected window 1:logs.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Server.sessions: unchanged.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Server.pane: unchanged.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Pane.window: unchanged, the call sent no command.
$ printf 'hello from %s\n' tmux hello from tmux $
tmux after Window.session: unchanged, the call sent no command.
tmux after Server.killServer: the server has exited.
Before the first call
No tmux server is running yet.
withServer returned Server
{ "endpoint": "named socket", "configFile": "/dev/null"}The call sent no tmux command.
Server.newSession returned Session
{ "id": "$0", "name": "demo"}2 tmux commands, 1 that changes tmux
-
new-session -d -s demo -P -F #{session_id}$0 -
list-sessions -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{session_name}fa564752183647ce03a9380fcfbd8f84#{session_attached}fa564752183647ce03a9380fcfbd8f84#{session_windows}8881901b696117907c4f36ea379e4b4a ; display-message -p fa564752183647ce03a9380fcfbd8f84:0 ; list-windows -a -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}fa564752…$0fa564752183647ce03a9380fcfbd8f84demofa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f8418881901b696117907c4f36ea379e4b4a fa564752183647ce03a9380fcfbd8f84:0 $0fa564752183647ce03a9380fcfbd8f84@0fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f84shfa564752183647ce03a9380fcfbd8f841fa564752183647ce03a9380fcfbd8f841fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcf…
Before these, the library asked tmux about itself 4 times (its version, its daemon, how a control client reads escapes).
Session.activeWindow returned Window?
{ "id": "@0"}The call sent no tmux command.
Window.rename returned Window
{ "id": "@0", "index": 0, "name": "editor"}2 tmux commands, 1 that changes tmux
-
rename-window -t @0 -- editorno output -
list-sessions -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{session_name}fa564752183647ce03a9380fcfbd8f84#{session_attached}fa564752183647ce03a9380fcfbd8f84#{session_windows}8881901b696117907c4f36ea379e4b4a ; display-message -p fa564752183647ce03a9380fcfbd8f84:0 ; list-windows -a -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}fa564752…$0fa564752183647ce03a9380fcfbd8f84demofa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f8418881901b696117907c4f36ea379e4b4a fa564752183647ce03a9380fcfbd8f84:0 $0fa564752183647ce03a9380fcfbd8f84@0fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f84editorfa564752183647ce03a9380fcfbd8f841fa564752183647ce03a9380fcfbd8f841fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a938…
Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).
Window.activePane returned Pane?
{ "id": "%0"}The call sent no tmux command.
Pane.split returned Pane
{ "id": "%1"}2 tmux commands, 1 that changes tmux
-
split-window -h -d -t %0 -P -F #{pane_id}8881901b696117907c4f36ea379e4b4a%18881901b696117907c4f36ea379e4b4a -
list-sessions -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{session_name}fa564752183647ce03a9380fcfbd8f84#{session_attached}fa564752183647ce03a9380fcfbd8f84#{session_windows}8881901b696117907c4f36ea379e4b4a ; display-message -p fa564752183647ce03a9380fcfbd8f84:0 ; list-windows -a -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}fa564752…$0fa564752183647ce03a9380fcfbd8f84demofa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f8418881901b696117907c4f36ea379e4b4a fa564752183647ce03a9380fcfbd8f84:0 $0fa564752183647ce03a9380fcfbd8f84@0fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f84editorfa564752183647ce03a9380fcfbd8f841fa564752183647ce03a9380fcfbd8f842fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a938…
Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).
Session.newWindow returned Window
{ "id": "@1", "index": 1, "name": "logs"}2 tmux commands, 1 that changes tmux
-
new-window -d -n logs -t $0 -P -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}8881901b696117907c4f36ea379e4b4a$0fa564752183647ce03a9380fcfbd8f84@1fa564752183647ce03a9380fcfbd8f8418881901b696117907c4f36ea379e4b4a -
list-sessions -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{session_name}fa564752183647ce03a9380fcfbd8f84#{session_attached}fa564752183647ce03a9380fcfbd8f84#{session_windows}8881901b696117907c4f36ea379e4b4a ; display-message -p fa564752183647ce03a9380fcfbd8f84:0 ; list-windows -a -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}fa564752…$0fa564752183647ce03a9380fcfbd8f84demofa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f8428881901b696117907c4f36ea379e4b4a fa564752183647ce03a9380fcfbd8f84:0 $0fa564752183647ce03a9380fcfbd8f84@0fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f84editorfa564752183647ce03a9380fcfbd8f841fa564752183647ce03a9380fcfbd8f842fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a938…
Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).
Pane.sendLine returned Unit
null2 tmux commands, 1 that changes tmux
-
display-message -p -t %1 -- #{version}0000-version#{pane_synchronized}fa564752183647ce03a9380fcfbd8f84-end3.8afa564752183647ce03a9380fcfbd8f84-version0fa564752183647ce03a9380fcfbd8f84-end -
send-keys -l -t %1 -- 'printf '\''hello from %s\n'\'' tmux\r'no output
Pane.awaitText returned TextOutcome
"PRESENT_AT_ENTRY"1 tmux command
-
capture-pane -p -J -t %1$ printf 'hello from %s\n' tmux hello from tmux $
Pane.select returned Unit
null1 tmux command, 1 that changes tmux
-
select-pane -t %1no output
Window.select returned Unit
null1 tmux command, 1 that changes tmux
-
select-window -t $0:1no output
Server.sessions returned List<Session>
{ "sessions": 1, "windows": 2, "panes": 3}1 tmux command
-
list-sessions -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{session_name}fa564752183647ce03a9380fcfbd8f84#{session_attached}fa564752183647ce03a9380fcfbd8f84#{session_windows}8881901b696117907c4f36ea379e4b4a ; display-message -p fa564752183647ce03a9380fcfbd8f84:0 ; list-windows -a -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}fa564752…$0fa564752183647ce03a9380fcfbd8f84demofa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f8428881901b696117907c4f36ea379e4b4a fa564752183647ce03a9380fcfbd8f84:0 $0fa564752183647ce03a9380fcfbd8f84@0fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f84editorfa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f842fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a938…
Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).
Server.pane returned Pane?
{ "id": "%1"}1 tmux command
-
list-sessions -f #{W:#{P:#{?#{==:#{pane_id},%1},1,}}} -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{session_name}fa564752183647ce03a9380fcfbd8f84#{session_attached}fa564752183647ce03a9380fcfbd8f84#{session_windows}8881901b696117907c4f36ea379e4b4a ; display-message -p fa564752183647ce03a9380fcfbd8f84:0 ; list-windows -a -f #{W:#{P:#{?#{==:#{pane_id},%1},1,}}} -F #{session_id}fa564752183647ce03a…$0fa564752183647ce03a9380fcfbd8f84demofa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f8428881901b696117907c4f36ea379e4b4a fa564752183647ce03a9380fcfbd8f84:0 $0fa564752183647ce03a9380fcfbd8f84@0fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f84editorfa564752183647ce03a9380fcfbd8f840fa564752183647ce03a9380fcfbd8f842fa564752183647ce03a9380fcfbd8f840fa564752183647ce03a938…
Before these, the library asked tmux about itself 1 time (its version, its daemon, how a control client reads escapes).
Pane.window returned Window
{ "id": "@0", "index": 0, "name": "editor"}The call sent no tmux command.
Window.session returned Session
{ "id": "$0", "name": "demo"}The call sent no tmux command.
Server.killServer returned Unit
null1 tmux command, 1 that changes tmux
-
display-message -p #{pid} ; kill-server3116567
libtmux/libtmux-java
at e8a39689d on a private tmux 3.8a server.
A person matched each call to its lines; the values, commands and tmux states are what the run produced.
Commands appear without the library's if-shell guard, and the socket name and paths are set to fixed values.
Run this example
JDK 25 and tmux on Linux/macOS; Gradle resolves Kotlin 2.4.10
Save the program above as src/main/kotlin/Main.kt 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.
build.gradle.kts
plugins { kotlin("jvm") version "2.4.10" application}
repositories { mavenCentral() }
dependencies { implementation("io.github.libtmux:libtmux-kotlin:0.1.2")}
java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }kotlin { jvmToolchain(25) }
application { mainClass.set(providers.gradleProperty("exampleMain")) }settings.gradle.kts
rootProject.name = "api-example"gradle.properties
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8kotlin.daemon.jvmargs=-Xmx2gorg.gradle.workers.max=2Setup and run
# Save the files above at their displayed relative paths.git clone https://github.com/libtmux/libtmux-java.git libtmux-sourcegit -C libtmux-source checkout --detach e8a39689d8d60968ac978a1dd2a89c6112333896./libtmux-source/gradlew --project-dir . --console=plain --no-daemon --max-workers=2 \ installDist -PexampleMain=MainKtSHELL=/bin/sh build/install/api-example/bin/api-exampleExpected output
session demo window 0:editor: %0 %1 window 1:logs: %2%1 is in window editor of session demoLibrary revision: e8a39689d8d6.
APIs used
Install io.github.libtmux:libtmux-kotlin
implementation("io.github.libtmux:libtmux-kotlin:0.1.0")Guides
Task guides for Kotlin.
Examples
Complete programs, sourced from the code each port tests.
Concepts
The domain model every port shares.