Skip to content

Scrollback & history

ghostty-webgpu exposes Ghostty’s native scrollback directly through lineCount() and readLines(). Reading history never moves the user’s scroll position or clears an active selection.

You will learn

  • How to read retained history and rejoin soft-wrapped lines with readLines()

  • How to paginate safely past the 1,024-row per-call cap
  • How Ghostty’s page-granular row and byte retention budgets work
  • When to choose readLines() vs. visibleLines() vs. captureViewport()

history.ts
import type { Terminal } from 'ghostty-webgpu'
export function readHistory(terminal: Terminal) {
const count = terminal.lineCount()
const lines = terminal.readLines(Math.max(0, count - 100), count, { trimRight: true })
return lines.map((line) => line.text).join('\n')
}
  • lineCount() returns total retained history rows plus visible rows on the active screen (including blank visible rows).
  • readLines(start, end, options) returns the clamped, oldest-first half-open range [start, end). Out-of-bounds indices clamp silently to [0, lineCount()).
  • Each TerminalLine contains { text, wrapped }. When wrapped is true, the row soft-wrapped into the next physical row, so you can rejoin wrapped lines when exporting logs. trimRight defaults to true.

A single readLines() call returns at most TERMINAL_READ_LINES_MAX_ROWS (1,024 rows). For larger exports, paginate by advancing start by the count returned.

retention.ts
import type { Terminal } from 'ghostty-webgpu'
export async function chooseRetention(terminal: Terminal) {
await terminal.setAppearance({
scrollbackLimit: 10000,
scrollbackByteLimit: 64 * 1024 * 1024,
})
}
export async function disableHistory(terminal: Terminal) {
await terminal.setAppearance({ scrollbackByteLimit: 0 })
}

Ghostty allocates terminal history in fixed-size memory pages whose row capacity depends on terminal width, styles, and grapheme data. Two independent limits control eviction:

  • appearance.scrollbackLimit: caps physical history rows above the active screen. Because Ghostty prunes whole pages and keeps at least one standard page intact, small values (including 0) can still retain rows that share a page with the active viewport.
  • appearance.scrollbackByteLimit: caps logical page memory across both scrollback and the active screen. Any positive value is raised to Ghostty’s minimum allocation (active screen plus one extra page). Setting scrollbackByteLimit: 0 immediately discards retained history and disables future scrollback.

When both budgets are set, whichever limit is hit first triggers page eviction.

APIReads fromBest for
readLines()Live libghostty-vt active screen + historyExporting logs, inspecting output right after write()
visibleLines()Last frame submitted to the rendererUI overlays and tests checking what is actually painted
captureViewport()Compact serialized viewport snapshotInstant visual first paint before a PTY reconnects (not live state)
Why captureViewport() is not a history backup

captureViewport() serializes only the currently visible cells into a compact payload that paintTerminalViewport can draw immediately on reload. It does not store scrollback, modes, or live PTY state. To restore an interactive session after reload, replay retained PTY bytes or reattach to a persistent server session (see

docs/saved-viewport.md

).

Recap

  • Use lineCount() and readLines(start, end) to read history without touching scroll position or selection.

  • Paginate in chunks of up to 1,024 rows and use line.wrapped to rejoin soft-wrapped lines.

  • Set scrollbackByteLimit: 0 when you want to discard and disable scrollback completely.