Skip to content

Quick start

ghostty-webgpu runs Ghostty’s unpatched libghostty-vt core in WebAssembly and renders it to WebGPU, WebGL2, Canvas 2D, or DOM. In a few minutes you will mount a working terminal, see how sizing and input work, and try a live interactive prompt.

You will learn

  • How to install the package and give the host container a real size
  • How to create, open, write to, and dispose a Terminal

  • How input bytes flow out of onData

  • How to add local line editing and Ghostty shortcuts when no shell is attached

Install ghostty-webgpu with your package manager. Any bundler that serves .wasm assets (Vite, esbuild, Webpack, Bun) works out of the box.

Terminal window
npm install ghostty-webgpu

Place a container element with a concrete height in your page:

index.html
<div id="terminal" style="height: 20rem; width: 100%"></div>

Creation and opening are both asynchronous because Terminal.create() loads and compiles the WebAssembly modules, and open(host) mounts the DOM host and initializes the renderer.

terminal.ts
import { Terminal } from 'ghostty-webgpu'
export async function mountTerminal(host: HTMLElement) {
const terminal = await Terminal.create()
await terminal.open(host)
terminal.writeln('Hello from Ghostty.')
terminal.onData(({ bytes }) => {
terminal.write(bytes)
})
terminal.focus()
return () => terminal.dispose()
}

Here is what happens at each step:

  1. await Terminal.create() loads ghostty-vt.wasm and bridge.wasm and constructs an owned libghostty-vt session.
  2. await terminal.open(host) measures host, computes the integer device-pixel cell grid, mounts the canvas, and starts Canvas-first rendering while WebGPU or WebGL2 warms up in the background.
  3. terminal.writeln(...) feeds UTF-8 bytes directly into libghostty-vt. On the main entry, native writes update emulator state synchronously and schedule a paint on the next animation frame.
  4. terminal.onData(({ bytes }) => ...) fires whenever keyboard or paste input produces VT bytes. In a real app you forward bytes straight to a PTY socket; in this minimal example we loop them back into terminal.write(bytes).
  5. terminal.dispose() tears down observers, event listeners, GPU resources, and WASM memory. Always call the disposer when your view or component unmounts.
Why open() paints at full size with no flash

Before constructing the renderer, TerminalHost.open() measures the container’s content box, font metrics, padding, and scrollbar reservation, then resizes the native libghostty-vt grid and styles the canvas to those exact pixel dimensions.

In default frame presentation mode, CanvasTerminalRenderer paints the first frame immediately while WebGPU or WebGL2 compiles its shaders on a separate offscreen canvas. Once the GPU canvas paints a matching frame, the host swaps it in place with zero layout shift.

Add local line editing and Ghostty shortcuts

Section titled “Add local line editing and Ghostty shortcuts”

A bare terminal only displays bytes and emits encoded keystrokes; normally a shell like Bash or Zsh handles prompts, Backspace, arrow keys, and command history. When you are building a local prompt with no remote shell, add @ghostty-webgpu/line-editor and @shaulavo/hotkeys:

Terminal window
npm install @ghostty-webgpu/line-editor @shaulavo/hotkeys
echo.ts
import { Terminal } from 'ghostty-webgpu'
import { attachTerminalHotkeys } from 'ghostty-webgpu/hotkeys'
import { readline, type ReadlineApi } from '@ghostty-webgpu/line-editor'
export async function mountEcho(host: HTMLElement) {
const terminal = await Terminal.create({ accessibility: {} })
attachTerminalHotkeys(terminal)
const editor = terminal.use(readline())
await terminal.open(host)
terminal.writeln('Hello from Ghostty. Type a line and press Enter.')
terminal.focus()
// Disposing the terminal rejects the pending read, which ends the loop.
echoLines(editor.api).catch(() => undefined)
return () => terminal.dispose()
}
async function echoLines(editor: ReadlineApi) {
for (;;) {
const result = await editor.read({ prompt: '$ ' })
if (result.kind === 'submit' && result.text) editor.print(result.text)
}
}
  • attachTerminalHotkeys(terminal) binds Ghostty’s keyboard shortcuts (Cmd+C, Cmd+K, font zoom, page scroll) before open().
  • terminal.use(readline()) installs the line-editor extension so editor.read({ prompt: '$ ' }) handles editing, history, and Ctrl+C locally.
  • accessibility: {} turns on the opt-in screen-reader text mirror.
  • Calling terminal.dispose() rejects the pending editor.read() promise cleanly, ending the loop.

Click the live terminal below, type a line, and press Enter:

Hello from Ghostty. Type a line and press Enter.

Recap

  • Give the host element a real height before calling await terminal.open(host).

  • Always await both Terminal.create() and terminal.open(host), and call terminal.dispose() on teardown.

  • Keep input and output in raw Uint8Array bytes via onData and write.

  • Use @ghostty-webgpu/line-editor for local prompts, or remove it when attaching a remote PTY shell.