KotlinHome

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.

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.

Example details
import io.github.libtmux.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.SplitSpec
import io.github.libtmux.TextOutcome
import io.github.libtmux.WindowSpec
import io.github.libtmux.kotlin.*
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import kotlinx.coroutines.runBlocking
import 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.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.SplitSpec
import io.github.libtmux.TextOutcome
import io.github.libtmux.WindowSpec
import io.github.libtmux.kotlin.*
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import kotlinx.coroutines.runBlocking
import 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.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.SplitSpec
import io.github.libtmux.TextOutcome
import io.github.libtmux.WindowSpec
import io.github.libtmux.kotlin.*
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import 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.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.SplitSpec
import io.github.libtmux.TextOutcome
import io.github.libtmux.WindowSpec
import io.github.libtmux.kotlin.*
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import 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()
}
}
}
}
No tmux server yet

tmux before the first call: no server is running.

No tmux server yet

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

%0 sh
[demo]

tmux after Server.newSession: added session demo.

%0 sh
[demo]

tmux after Session.activeWindow: unchanged, the call sent no command.

%0 sh
[demo]

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

%0 sh
[demo]

tmux after Window.activePane: unchanged, the call sent no command.

%0 sh
%1 sh
[demo]

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

%0 sh
%1 sh
[demo]

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

%0 sh
%1 sh
[demo]

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

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

tmux after Pane.awaitText: 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 Server.pane: unchanged.

%2 sh
[demo]

tmux after Pane.window: unchanged, the call sent no command.

%2 sh
[demo]

tmux after Window.session: unchanged, the call sent no command.

No sessions

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
  1. new-session -d -s demo -P -F #{session_id} $0
  2. 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
  1. rename-window -t @0 -- editor no output
  2. 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
  1. split-window -h -d -t %0 -P -F #{pane_id}8881901b696117907c4f36ea379e4b4a %18881901b696117907c4f36ea379e4b4a
  2. 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
  1. new-window -d -n logs -t $0 -P -F #{session_id}fa564752183647ce03a9380fcfbd8f84#{window_id}fa564752183647ce03a9380fcfbd8f84#{window_index}8881901b696117907c4f36ea379e4b4a $0fa564752183647ce03a9380fcfbd8f84@1fa564752183647ce03a9380fcfbd8f8418881901b696117907c4f36ea379e4b4a
  2. 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

null
2 tmux commands, 1 that changes tmux
  1. display-message -p -t %1 -- #{version}0000-version#{pane_synchronized}fa564752183647ce03a9380fcfbd8f84-end 3.8afa564752183647ce03a9380fcfbd8f84-version0fa564752183647ce03a9380fcfbd8f84-end
  2. send-keys -l -t %1 -- 'printf '\''hello from %s\n'\'' tmux\r' no output

Pane.awaitText returned TextOutcome

"PRESENT_AT_ENTRY"
1 tmux command
  1. capture-pane -p -J -t %1 $ printf 'hello from %s\n' tmux hello from tmux $

Pane.select returned Unit

null
1 tmux command, 1 that changes tmux
  1. select-pane -t %1 no output

Window.select returned Unit

null
1 tmux command, 1 that changes tmux
  1. select-window -t $0:1 no output

Server.sessions returned List<Session>

{
"sessions": 1,
"windows": 2,
"panes": 3
}
1 tmux command
  1. 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
  1. 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

null
1 tmux command, 1 that changes tmux
  1. display-message -p #{pid} ; kill-server 3116567
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-8
kotlin.daemon.jvmargs=-Xmx2g
org.gradle.workers.max=2

Setup and run

# Save the files above at their displayed relative paths.
git clone https://github.com/libtmux/libtmux-java.git libtmux-source
git -C libtmux-source checkout --detach e8a39689d8d60968ac978a1dd2a89c6112333896
./libtmux-source/gradlew --project-dir . --console=plain --no-daemon --max-workers=2 \
installDist -PexampleMain=MainKt
SHELL=/bin/sh build/install/api-example/bin/api-example

Expected output

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

Library revision: e8a39689d8d6.

APIs used

Install io.github.libtmux:libtmux-kotlin

implementation("io.github.libtmux:libtmux-kotlin:0.1.0")
<dependency>
  <groupId>io.github.libtmux</groupId>
  <artifactId>libtmux-kotlin</artifactId>
  <version>0.1.0</version>
</dependency>

API references

All Kotlin versions and install instructions

Esc

Type to search.

Reference previewOpen reference