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
TerminalHow input bytes flow out of
onData- How to add local line editing and Ghostty shortcuts when no shell is attached
Install the package
Section titled “Install the package”Install ghostty-webgpu with your package manager. Any bundler that serves .wasm assets (Vite, esbuild, Webpack, Bun) works out of the box.
npm install ghostty-webgpuGive the host container a size
Section titled “Give the host container a size”Place a container element with a concrete height in your page:
<div id="terminal" style="height: 20rem; width: 100%"></div>Mount your first terminal
Section titled “Mount your first terminal”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.
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:
await Terminal.create()loadsghostty-vt.wasmandbridge.wasmand constructs an ownedlibghostty-vtsession.await terminal.open(host)measureshost, computes the integer device-pixel cell grid, mounts the canvas, and starts Canvas-first rendering while WebGPU or WebGL2 warms up in the background.terminal.writeln(...)feeds UTF-8 bytes directly intolibghostty-vt. On the main entry, native writes update emulator state synchronously and schedule a paint on the next animation frame.terminal.onData(({ bytes }) => ...)fires whenever keyboard or paste input produces VT bytes. In a real app you forwardbytesstraight to a PTY socket; in this minimal example we loop them back intoterminal.write(bytes).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:
npm install @ghostty-webgpu/line-editor @shaulavo/hotkeysimport { 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) beforeopen().terminal.use(readline())installs the line-editor extension soeditor.read({ prompt: '$ ' })handles editing, history, and Ctrl+C locally.accessibility: {}turns on the opt-in screen-reader text mirror.- Calling
terminal.dispose()rejects the pendingeditor.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()andterminal.open(host), and callterminal.dispose()on teardown.Keep input and output in raw
Uint8Arraybytes viaonDataandwrite.Use
@ghostty-webgpu/line-editorfor local prompts, or remove it when attaching a remote PTY shell.