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.
Guides
Task guides for Scala.
Examples
Complete programs, sourced from the code each port tests.
API reference
Types and functions of the Scala API.
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.
| Type | Wraps |
|---|---|
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
import io.github.libtmux.{ServerConfig, ServerEndpoint}import io.github.libtmux.scaladsl.{config => _, *}import java.nio.file.Pathimport java.time.Durationimport java.util.UUIDimport 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.Pathimport java.time.Durationimport java.util.UUIDimport scala.util.Usingimport 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.Pathimport java.time.Durationimport java.util.UUIDimport 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.Pathimport java.time.Durationimport java.util.UUIDimport scala.util.Usingimport 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-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 5c181887918351119bfdb3a046719fa792048e59./libtmux-source/gradlew --project-dir . --console=plain --no-daemon --max-workers=2 \ installDist -PexampleMain=MainSHELL=/bin/sh build/install/api-example/bin/api-exampleExpected output
work: 2 panescommands: shLibrary revision: 5c1818879183.
Typed API
- Typed objects instead of parsed format strings:
io.github.libtmux.scaladsl.Pane - Filtering and queries over tmux objects:
io.github.libtmux.scaladsl.query.Expr
What you get over the tmux CLI
- Connect to or start a tmux server:
io.github.libtmux.scaladsl.Server.open - Traversal from a server down to its panes:
io.github.libtmux.scaladsl.Server.panes - Send keys to a pane:
io.github.libtmux.scaladsl.Pane.sendKeys - Watch for changes through tmux control mode:
io.github.libtmux.scaladsl.Server.control - Capture pane contents:
io.github.libtmux.scaladsl.Pane.capture - Arguments passed as a list, with no shell quoting:
io.github.libtmux.scaladsl.Server.cmd