Core Library0.xAPI Reference

Choose documentation 3

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.

method libtmux-swift latest

Server.sendKeys(_:to:literally:)

Sends keys to a pane.

Source and package
Qualified name
Server.sendKeys(_:to:literally:)
Declared in
Server
Package
libtmux-swift
Source
Sources/LibTmux/PaneInteraction.swift
Other languages
Perlmethod Erlang PowerShell Julia Elixir Haskell Common LispPythonRubyLuaTypeScriptRustGo Java C#C++KotlinScalaOCaml F# Clojure
sendKeys(keys: [String], to: Pane, literally: Bool) -> ()

Description#

  • Parameters:
    • keys: what to send, one argument per key or literal string.
    • pane: the pane to send them to.
    • literally: sends the text as characters rather than letting tmux read names like Enter or C-c out of it. Use it for anything that came from a user.

Examples#

Send input and capture its output. Type a printf command literally, send Enter as a key, and wait for its output. Compare a complete output row so terminal echo cannot satisfy the check. The five-second deadline bounds the example; the 25-millisecond pause avoids busy polling. Use Swift 6.2 or newer and tmux 3.3a or newer on Linux or macOS. In an empty directory, save this consumer package as Package.swift; SwiftPM fetches the library from release 0.1.1:

// swift-tools-version: 6.2
import PackageDescription
let package = Package(
name: "ApiExample",
platforms: [.macOS(.v13)],
dependencies: [
.package(url: "https://github.com/libtmux/libtmux-swift.git", from: "0.1.1")
],
targets: [
.executableTarget(
name: "ApiExample",
dependencies: [
.product(name: "LibTmux", package: "libtmux-swift"),
.product(name: "TmuxFixture", package: "libtmux-swift"),
],
path: "Sources"
)
]
)

Create a Sources directory and save this complete program as Sources/Example.swift. The withTmuxServer fixture owns a private tmux server and stops it after success or failure. Ordinary application code uses Server for its own socket; these examples use the fixture until their setup moves to ordinary server objects. View source

import Foundation
import LibTmux
import TmuxFixture
@main
struct ApiCapture {
static func main() async {
do {
try await withTmuxServer { @Sendable server in
guard let pane = try await server.panes().first else {
throw ExampleError.paneMissing
}
let marker = "libtmux-swift-api"
try await server.sendKeys(["printf '%s\\n' \(marker)"], to: pane, literally: true)
try await server.sendKeys(["Enter"], to: pane)
let clock = ContinuousClock()
let deadline = clock.now.advanced(by: .seconds(5))
while clock.now < deadline {
let lines = try await server.capture(pane, includingHistory: true)
if lines.contains(marker) {
print(marker)
return
}
try await Task.sleep(for: .milliseconds(25))
}
throw ExampleError.outputTimedOut
}
} catch {
FileHandle.standardError.write(Data("Example failed: \(error)\n".utf8))
exit(EXIT_FAILURE)
}
}
enum ExampleError: Error {
case paneMissing
case outputTimedOut
}
}

Build and run the saved program. An operation or cleanup error produces a failing exit status. Set LIBTMUX_TMUX_BIN to an absolute executable path to select a tmux version:

Terminal window
$ swift run ApiExample

Expected output:

libtmux-swift-api
Parameters
Raises

tmux command#

The tmux commands behind sendKeys(_:to:literally:).

tmux C API#

The C declarations in tmux that implement these commands.

Guides#

Pages that discuss sendKeys(_:to:literally:).

In other ports#

Send keystrokes to a pane

Equivalents of Send keystrokes to a pane in other languages
Language Equivalent
Python libtmux.Pane.send_keys
Ruby LibTmux::Pane#send_keys
Lua libtmux.Pane:send_keys
Erlang tmux_pane:send_keys/2
Haskell No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing.
Common Lisp libtmux:send-keys
TypeScript pane.Pane.sendKeys
Rust pane.Pane.send_keys
Go tmux.Pane.SendKeys
Java no direct equivalent; Pane.paste writes a buffer instead
Clojure libtmux.core/send-keys!
Kotlin io.github.libtmux.kotlin.Pane.sendKeys
Scala io.github.libtmux.scaladsl.Pane.sendKeys
C# LibTmux.Pane.SendKeysAsync
C++ libtmux::Pane::send_text
OCaml Libtmux_unix.Client.send_keys
Perl LibTmux::Server::send_keys Explicit Server operation. Captured entities remain local observations; use a new snapshot to observe changes.

No equivalent is recorded in PowerShell, Julia, Elixir or F#.

Esc

Type to search.

Reference previewOpen reference