Core Library0.xHome

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.

Edit this page on GitHub

tmux client library for Scala

Control tmux, connect to and create servers, navigate sessions, windows, and panes, send keys, watch for changes via control mode, capture pane contents.

Install

libraryDependencies += "io.github.libtmux" %% "libtmux-scala" % "0.1.0"

At a glance

Server → Session → Window → Pane. Each level of the tmux hierarchy has its own entry in the Scala API, with the tmux object it wraps.

TypeWraps
io.github.libtmux.scaladsl.Server A tmux server
io.github.libtmux.scaladsl.Session A session, ID $n
io.github.libtmux.scaladsl.Window A window in a session, ID @n
io.github.libtmux.scaladsl.Pane A pane in a window, ID %n

What it does

Start with Scala

Example details
import io.github.libtmux.{ServerConfig, ServerEndpoint}
import io.github.libtmux.scaladsl.{config => _, *}
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import scala.util.Using
object Main {
def main(args: Array[String]): Unit = {
val socket = s"libtmux-home-${UUID.randomUUID()}"
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build()
Using.resource(Server.open(config)) { server =>
val session = server.newSession("work")
session.newWindow("logs")
val panes = server.panes()
val named = panes.filter(_.info.title().nonEmpty)
val commands = named.map(_.info.currentCommand())
assert(panes.size == 2)
println(s"${session.name}: ${panes.size} panes")
println(s"commands: ${commands.distinct.sorted.mkString(", ")}")
}
}
}
import io.github.libtmux.{ServerConfig, ServerEndpoint}
import io.github.libtmux.scaladsl.{config => _, *}
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import scala.util.Using
import scala.util.control.NonFatal
object Main {
def main(args: Array[String]): Unit = {
val socket = s"libtmux-home-${UUID.randomUUID()}"
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build()
Using.resource(Server.open(config)) { server =>
try {
val session = server.newSession("work")
session.newWindow("logs")
val panes = server.panes()
val named = panes.filter(_.info.title().nonEmpty)
val commands = named.map(_.info.currentCommand())
assert(panes.size == 2)
println(s"${session.name}: ${panes.size} panes")
println(s"commands: ${commands.distinct.sorted.mkString(", ")}")
} catch {
case NonFatal(error) =>
throw new IllegalStateException(
"tmux quickstart failed", error
)
}
}
}
}
import io.github.libtmux.{ServerConfig, ServerEndpoint}
import io.github.libtmux.scaladsl.{config => _, *}
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import scala.util.Using
object Main {
def main(args: Array[String]): Unit = {
val socket = s"libtmux-home-${UUID.randomUUID()}"
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build()
Using.resource(Server.open(config)) { server =>
try {
val session = server.newSession("work")
session.newWindow("logs")
val panes = server.panes()
val named = panes.filter(_.info.title().nonEmpty)
val commands = named.map(_.info.currentCommand())
assert(panes.size == 2)
println(s"${session.name}: ${panes.size} panes")
println(s"commands: ${commands.distinct.sorted.mkString(", ")}")
} finally {
server.killServer()
}
}
}
}
import io.github.libtmux.{ServerConfig, ServerEndpoint}
import io.github.libtmux.scaladsl.{config => _, *}
import java.nio.file.Path
import java.time.Duration
import java.util.UUID
import scala.util.Using
import scala.util.control.NonFatal
object Main {
def main(args: Array[String]): Unit = {
val socket = s"libtmux-home-${UUID.randomUUID()}"
val config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build()
Using.resource(Server.open(config)) { server =>
try {
val session = server.newSession("work")
session.newWindow("logs")
val panes = server.panes()
val named = panes.filter(_.info.title().nonEmpty)
val commands = named.map(_.info.currentCommand())
assert(panes.size == 2)
println(s"${session.name}: ${panes.size} panes")
println(s"commands: ${commands.distinct.sorted.mkString(", ")}")
} catch {
case NonFatal(error) =>
throw new IllegalStateException(
"tmux quickstart failed", error
)
} finally {
server.killServer()
}
}
}
}
Run this example

JDK 25 and tmux on Linux/macOS; Scala 3.9.0 Direct facade

Save the program above as src/main/scala/Main.scala 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 {
scala
application
}
repositories { mavenCentral() }
dependencies {
implementation("io.github.libtmux:libtmux-scala_3:0.1.0")
implementation("org.scala-lang:scala3-library_3:3.9.0")
}
java { toolchain { languageVersion.set(JavaLanguageVersion.of(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 5c181887918351119bfdb3a046719fa792048e59
./libtmux-source/gradlew --project-dir . --console=plain --no-daemon --max-workers=2 \
installDist -PexampleMain=Main
SHELL=/bin/sh build/install/api-example/bin/api-example

Expected output

work: 2 panes
commands: sh

Library revision: 5c1818879183.

Typed API

What you get over the tmux CLI

Esc

Type to search.

Reference previewOpen reference