caretline

The caretline command

caretline is the interactive editor and a headless tool for states, snapshots and replay. It comes from the caretline-cli crate. Every output on this page is from a real run.

Install

curl -fsSL https://caretline.app/install.sh | sh -s -- --demo   # a prebuilt binary, then the welcome demo
curl -fsSL https://caretline.app/install.sh | sh -s -- --version 0.5.0   # a specific release
curl -fsSL https://caretline.app/install.sh | sh -s -- --ref main -- doctor --keys   # try an unreleased branch or commit
cargo install caretline-cli   # or build it
cargo install --locked --path crates/caretline-cli   # from a checkout
cargo run -p caretline-cli -- draft.md                # or run it from the checkout

--version X.Y.Z installs that release. --ref REF builds a branch or commit of brancusi/caretline from source with cargo into ~/.cache/caretline/ref and leaves the installed binary alone (it needs Rust and git). -- ARGS then runs caretline ARGS on the terminal: it is how each change’s Try it: line (see AGENTS.md) shows the change working.

See the Quickstart for what the installer does and what the demos show.

Commands at a glance

CommandDoes
caretline demo [welcome|tour|scenes|agent|layers|showcase]Built-in demos, no files needed; with no name, the welcome. See the Quickstart and Layers over the tour. --snapshot WxH prints a demo’s first frame; demo agent --headless runs the agent against a headless editor and prints a JSON report
caretline [FILE]Edit FILE interactively (created on first save)
caretline FILE --trace T.jsonlEdit, recording the session to a trace
caretline FILE --no-mouseEdit without capturing the mouse (by default: click, Shift-click, drag, and a drag held on an edge row keeps scrolling, see messages.md)
caretline --new-state FILE [--size WxH]Print an initial state for FILE as JSON
caretline --state S.jsonEdit a saved state interactively
caretline --state S.json --keys SCRIPT …Apply a key script, headless
caretline --state S.json --msgs M.jsonl …Apply messages, headless (- reads stdin)
caretline --replay T.jsonl …Start from a trace with all its messages applied, headless
… --snapshot WxH [--format text|ansi]Print the rendered frame
… --dump-state OUT.jsonWrite the final state (- for stdout)
… --effectsPrint the effects update returned, one JSON per line
… --size WxHResize before applying messages
caretline serve, caretline send, --listenThe state protocol, see protocol.md
caretline benchProtocol throughput and latency (build with --release)
caretline --outline FILEEdit FILE as Markdown blocks: lists, headings and blocks with their own keys, Markdown in and out (also for --new-state and serve)
caretline --layout FILEAs --outline, with the outline layout: markers in a hang with plain glyphs, a column per depth (also for serve)
caretline doctorIn Ghostty: the keys Ghostty’s own bindings take before caretline sees them (derived from a model of Ghostty’s keyboard), and the config lines that hand them over (see Edit a file)
caretline doctor --keysLive key echo: the bytes the terminal sent, the key caretline decoded and the command it runs. Ctrl-C twice stops
caretline sim TEXT KEYS [--legacy] [--mine] [--config LINE]... [--size WxH]Run a key script through a simulated Ghostty, the editor and a terminal emulator, in process (see Try keys without a terminal)
caretline keys [--outline] [--json]Every key and the command it runs, from caretline’s command catalog. In the editor, F1 or Alt-? shows the same
… --no-status-barHide the status bar (config.status_bar = false): every row shows text
… --max-fps FPSEditor: repaint at most this many times a second, coalescing changes in between (default 120; 0 repaints after every batch of input). See performance.md
… --frame-clock FPSEditor: start with a frame clock (a frame message FPS times a second). Off by default
… --statsEditor: print the repaint count and mean repaint time on exit
… --trace-limit LINESEditor and serve: bound the in-memory trace trace.get serves (default 100,000 lines; see protocol.md)
caretline serve … --exit-with-parentExit (removing the socket) once the process that started the server is gone, even if it was killed: for servers a test or a supervisor starts (see protocol.md)

A run is headless when any of --snapshot, --dump-state, --msgs, --keys or --replay is given. Headless runs never perform effects: no file is written and the clipboard isn’t touched. --effects shows what would have happened.

Edit a file

caretline draft.md

The keys are macOS text-field keys with Ctrl twins (see messages.md). Ctrl-S saves and Ctrl-Q quits; with unsaved changes, press Ctrl-Q twice. Cmd (⌘) keys need a terminal that speaks the kitty keyboard protocol (kitty, WezTerm, Ghostty, iTerm2 with the option on). caretline turns it on when the terminal supports it. A terminal’s own key bindings still come first: Ghostty’s defaults keep Cmd-A (it selects the whole terminal screen), Cmd-Up and Cmd-Down and their Shift forms (jump between prompts), Cmd-Z and Cmd-Shift-Z for themselves. caretline doctor runs each of caretline’s keys through a model of Ghostty’s keyboard (its default bindings, and how it encodes the keys it keeps for the application), lists the keys Ghostty takes and prints the lines that hand them over, such as keybind = super+a=unbind. caretline doctor --keys echoes each key you press (the bytes, the decoded key, the command), to see what a terminal really sends. Without the kitty protocol, the decoder still reads xterm’s modifyOtherKeys form (CSI 27;m;code ~, which legacy Ghostty sends for Shift-Enter and the like). CARETLINE_KEYBOARD=legacy leaves the kitty keyboard protocol off, to test that path. Copy also writes the system clipboard (pbcopy, wl-copy, xclip or xsel, else OSC 52), and paste reads it when it can.

Saving writes a temporary sibling file and renames it over the target, so a failed write never leaves half a file.

Try keys without a terminal

caretline sim TEXT KEYS runs a key script through a simulated Ghostty (its default bindings and key encoding), the decoder, the editor and a terminal emulator, all in process. For each key it prints the key’s path (what Ghostty sent, or that it kept the key, and the command that ran) and the selection, then the final screen, checked against the editor’s state after every key.

In TEXT, ▮ is a caret and ⟦…⟧ surrounds a selection. KEYS is a key script: letters, and <d-a> (Cmd-A), <s-left>, <c-a-bs>, <cr>, <wait:500>.

FlagMeaning
--legacyWithout the kitty keyboard protocol
--mineYour Ghostty’s bindings (ghostty +list-keybinds), not the 1.3.1 defaults
--config LINEA Ghostty config line on top, repeatable
--size WxHThe terminal size (default 40x8)
caretline sim 'hello ▮world' '<d-a>x' --config 'keybind = super+a=unbind'
caretline sim '▮one
▮two' 'x<s-right>'

The first prints <d-a>: sent ⎋[97;9u → SelectAll and ⟦hello world▮⟧: with the binding unbound, Cmd-A reaches the editor. Without the --config line Ghostty keeps the key, and the path says so.

The same harness runs as tests: caretline-cli’s harness fuzz prints any failure as a caretline sim command, and each file in tests/goldens/harness/ opens with the command that shows it (see Testing).

The state, snapshot and replay workflow

This walks through every headless flag on a small file.

1. Capture a state

$ printf '# Plan\n\nShip the editor docs.\n' > plan.md
$ caretline --new-state plan.md --size 40x6 > s0.json
$ caretline --state s0.json --snapshot 40x6
# Plan

Ship the editor docs.


 plan.md                            1:1

--new-state uses 80x24 unless you pass --size. The state is pretty-printed JSON: the text, the selection, the scroll, the viewport, the undo history and the config. You can edit it by hand; loading repairs anything out of range.

You can also write a state from scratch. Only text is needed; everything else takes --new-state’s defaults (a caret at 0, 80x24, a fresh history, a clean document):

$ cat > min.json <<'EOF'
{"text": "hello\nworld\n", "selection": {"ranges": [{"anchor": 0, "head": 5}]}, "viewport": {"width": 20, "height": 4}}
EOF
$ caretline --state min.json --keys 'bye' --snapshot 20x4
bye
world

 [scratch] [+]  1:4

See architecture.md for every default.

2. Drive it with keys, and save the result

$ caretline --state s0.json --keys '<d-down>Then review them.' --snapshot 40x6 --dump-state s1.json
# Plan

Ship the editor docs.
Then review them.

 plan.md [+]                       4:18

<d-down> is ⌘↓ (document end). The status bar shows the file name, [+] for unsaved changes, and line:col.

3. Select, undo, inspect

$ caretline --state s1.json --keys '<s-a-left><s-a-left>' --snapshot 40x6
# Plan

Ship the editor docs.
Then review them.

 plan.md [+]                12 sel  4:6
$ caretline --state s1.json --keys '<c-z>' --snapshot 40x6
# Plan

Ship the editor docs.


 plan.md                            4:1

The undo history is part of the state, so s1.json can still undo the typing it recorded. After the undo, the document matches the saved revision again, so [+] is gone.

4. See effects instead of performing them

$ caretline --state s1.json --keys '<c-s>' --effects --dump-state /dev/null
{"effect":"write_file","path":"plan.md","text":"# Plan\n\nShip the editor docs.\nThen review them."}
$ cat plan.md
# Plan

Ship the editor docs.

The file is unchanged: headless runs report effects and never perform them.

5. Apply a message file

$ cat m.jsonl
{"msg":"move","dir":"backward","by":"word","extend":true}
{"msg":"copy"}
{"msg":"move","dir":"forward","by":"doc_end"}
{"msg":"insert_newline"}
{"msg":"paste"}
$ caretline --state s1.json --msgs m.jsonl --effects --snapshot 40x6
{"effect":"clipboard_set","text":"them."}
# Plan

Ship the editor docs.
Then review them.
them.
 plan.md [+]                        5:6

--msgs - reads the messages from stdin. A JSON array works too.

6. Resize

--size resizes before the messages; --snapshot resizes after them if its size differs. At 20 columns the long line wraps:

$ caretline --state s1.json --size 20x6 --snapshot 20x6
# Plan

Ship the editor
docs.
Then review them.
 plan.md [+]   4:18

7. Record and replay a session

caretline draft.md --trace t.jsonl          # edit, then quit
caretline --replay t.jsonl --snapshot 80x24 # the final frame, exactly
caretline --replay t.jsonl --dump-state -   # the final state

--trace appends, so restarting into the same file adds a new state line and replay continues from it. The file gets every line, also past --trace-limit, which bounds only the in-memory trace. --replay reads any trace: a full file, the output of caretline send trace.get --raw (one segment), or of trace.get all; a state line in the middle (a state.set or checkpoint) restarts the replay from that state. The fixture session.trace.jsonl is a recorded session:

$ caretline --replay crates/caretline-cli/fixtures/session.trace.jsonl --snapshot 36x8

Shift and the arrows grow one from
the caret's end,
and a plain arrow collapses it to
the edge.

arrows grow one from the caret's
 select.md [+]                 5:33

Snapshot formats

--format text (the default) prints the rows with trailing spaces trimmed. --format ansi adds styling: the selection in reverse colours, the status bar highlighted, and the caret as an underlined reverse cell (a snapshot has no terminal cursor).

$ caretline --state s1.json --keys '<s-a-left>' --snapshot 40x6 --format ansi | sed -n 4p | cat -v
^[[0mThen review ^[[0m^[[7;4mt^[[0m^[[30;46mhem.^[[0m                       ^[[0m

Here ^[[7;4m marks the caret cell and ^[[30;46m the rest of the selection.

Fixtures

crates/caretline-cli/fixtures holds saved states with their text and ANSI snapshots. The CLI tests check that each one still renders the same.

FixtureShows
wrapped-paragraphA Markdown paragraph soft-wrapped at 44 columns, caret on a wrapped row
emoji-lineEmoji (skin tone, ZWJ family, flags), combining accents and wide CJK, with a selection
mid-selectionA selection across lines, ending inside a wrapped row
after-undoAn edit undone, with the redo step still in the history
no-wrap-tableWrapping off: a long table row scrolled sideways
session.trace.jsonlA recorded session; session.snapshot.txt is its replay at 36x8
outline-triptrip.md opened as blocks: a heading, paragraphs, a nested list, an image block and a numbered list, with blank rows drawn as virtual rows
outline-editedThe same after keys: a nested item added under another, an item moved up
$ caretline --state crates/caretline-cli/fixtures/emoji-line.state.json --keys '<s-right><s-right>' --snapshot 40x6
Emoji 👍🏽 and family 👨‍👩‍👧, accents café
and résumé, wide 漢字かな, flags 🇫🇷🇯🇵.
Second line: ½ ⅓ → ✓


 emoji.md                   5 sel  1:19
$ caretline --state crates/caretline-cli/fixtures/no-wrap-table.state.json --keys '<home>' --snapshot 48x6
| Name | Role | Notes |
|---|---|---|
| Ada | engine | Wrapping is off here, so long t
| Lin | review | Short row. |

 nowrap.md                                  3:1

The status bar’s N sel counts Unicode scalar values, while line:col counts graphemes.

Collaborating with a person

While someone types in caretline FILE --listen, push changes so their caret never moves:

caretline send --latest set-text draft-v2.md     # text.set: only what differs changes
caretline send --latest keys '<d-down>- a line'  # through this connection's own view

See protocol.md.

Animate a live editor

caretline demo scenes plays six ASCII scenes (warp, donut, cube, tunnel, plasma, fire) inside the editor, pushed by a client on its own socket with the frame op (see the Quickstart). With --bench, it plays them into an editor that is already running instead, paced against absolute deadlines, and reports the frames per second it achieved:

caretline draft.md --listen                                       # in one terminal
caretline demo scenes --bench                                     # in another
caretline demo scenes --bench --fps 120 --scene donut,plasma --seconds 5
FlagDoes
--fps 60,120,0Target rates to play each scene at (0 is unthrottled). Default 60,120,0. Without --bench, one rate (default 60)
--seconds SHow long each scene plays at each rate (default 3; without --bench, 8 per scene)
--scene NAMESSome of warp,donut,cube,tunnel,plasma,fire (default all)
--socket PATHThe editor’s socket (default: the newest live editor)
--frames N, --size WxHFrames precomputed per scene (180), the scene size (the editor’s text area)

For 120 fps on screen, the terminal must paint that fast too: in WezTerm set config.max_fps = 120, on a 120 Hz display.

The welcome

caretline demo (or caretline demo welcome; new in 0.4.1, where 0.4.0 opened the tour) opens the welcome page: what caretline is, every other demo as a chapter, and commands to try next. ↑/↓ (or j/k, Tab) choose, Enter or 1–5 start a chapter, p switches the callout between pixels and cells, q quits. A chapter that ends comes back to the page, ticked. --snapshot WxH [--keys …] prints the page headless; --dir, --no-mouse and --reduced-motion pass through to the chapters.

Timed interactive showcase

caretline demo showcase (or ./scripts/demo.sh from a checkout) runs twelve timed slides with real message-driven edits, multiple carets, overlays, a shared-document pane, guarded remote writes, structural edits, serialization/replay checks, branded typography, ASCII scenes, fractional-pixel motion and a continuous warp-logo finale. No agent is needed. The Quickstart has the slide sequence, all controls and examples for the live listener.

The deck is read-only to ordinary printable keys; presentation shortcuts drive it. Socket clients can edit the underlying state. Slide navigation resets the document and closes its secondary views. Use a direct Ghostty terminal at 100×30 for the best pixel presentation; small windows and unsupported terminals use the same content with adaptive cell overlays. Terminal pastes are consumed, while explicit socket edits remain allowed. Typography samples use the site’s licensed Geist/Geist Mono outlines at three raster sizes, not terminal font escape sequences. The pixel motion is anti-aliased fractional-device-pixel geometry (not RGB/LCD subpixel text rendering), targeted at 24 updates/s. The ASCII section reuses the existing scene renderers; the finale loops until paused or quit. --reduced-motion freezes that continuous motion. The showcase keeps at most 512 trace lines to bound continuous-frame history; subscribers can still collect the full stream.

Layers over the tour

caretline demo layers shows a hint with an arrow, a ring and a spotlight over the tour’s text, placed by caretline-layers. The hint points at a word in the “Move” section and follows it as the view scrolls; the text is read-only here.

KeyDoes
↑ ↓ PgUp PgDnScroll; the hint follows its word
← → Home EndMove the caret
sSpotlight on or off
pPixels or cells (pixels only where the probe allowed them)
tThe pixel transport: inline (t=d, works over SSH) or temporary files (t=t, local)
q, Esc, ⌃CQuit

At startup it probes the terminal (a graphics query, XTVERSION and the cell size, fenced by DA1, 200 ms at most) and uses pixels when the graphics query says OK, the cell size comes back and the terminal is Ghostty or kitty. CARETLINE_LAYERS=auto|pixels|cells overrides that (pixels also tries an untested terminal, or one inside a multiplexer). After a font-size change it asks for the cell size again.

caretline demo layers
CARETLINE_LAYERS=cells caretline demo layers
caretline demo layers --snapshot 80x24                 # the first frame, in cells
caretline demo layers --snapshot 60x20 --keys '<down><down>s'   # scrolled, spotlight off

An 80×24 or larger window gives the hint and controls room. For a short presentation, toggle s, scroll with ↓/↑, compare modes with p, try t in pixel mode, and quit with q. F1 or Alt-? covers the demo with the keys overlay (including its images); Esc closes that overlay and restores the demo. With no keys overlay open, Esc quits the layers demo.

Errors

Errors go to stderr with exit code 1:

$ caretline --state s0.json --snapshot 80
caretline: bad size "80": expected WIDTHxHEIGHT, like 80x24
$ caretline --state s0.json --keys '<nope>'
caretline: unknown key <nope>
$ echo '{"msg":"jump"}' | caretline --state s0.json --msgs -
caretline: line 1: unknown variant `jump`, expected one of `insert_text`, `insert_newline`, … at line 1 column 13

Limits

This page on GitHub: docs/cli.md