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()
Read retained lines
Section titled “Read retained lines”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
TerminalLinecontains{ text, wrapped }. Whenwrappedistrue, the row soft-wrapped into the next physical row, so you can rejoin wrapped lines when exporting logs.trimRightdefaults totrue.
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.
Configure retention budgets
Section titled “Configure retention budgets”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 (including0) 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). SettingscrollbackByteLimit: 0immediately discards retained history and disables future scrollback.
When both budgets are set, whichever limit is hit first triggers page eviction.
Choosing the right read API
Section titled “Choosing the right read API”| API | Reads from | Best for |
|---|---|---|
readLines() | Live libghostty-vt active screen + history | Exporting logs, inspecting output right after write() |
visibleLines() | Last frame submitted to the renderer | UI overlays and tests checking what is actually painted |
captureViewport() | Compact serialized viewport snapshot | Instant 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()andreadLines(start, end)to read history without touching scroll position or selection.Paginate in chunks of up to 1,024 rows and use
line.wrappedto rejoin soft-wrapped lines.Set
scrollbackByteLimit: 0when you want to discard and disable scrollback completely.