caretline

caretline

caretline is a text-editing engine and a terminal text editor. The engine (the caretline crate) puts Helix’s editing model (a rope, multi-range selections, transactions, an undo tree, grapheme-correct motion and soft wrap) inside a strict Elm architecture. The whole editor is one serializable State. Every input is a Msg. A pure update function applies a message and returns Effects for the outside world, and a pure view function turns the state into a grid of cells. The engine does no I/O and has no terminal code, so you can drive it from a terminal, a test, a script or another program, and replay any session exactly. The caretline binary (caretline-cli) is the interactive editor and a headless tool built on top.

$ caretline draft.md                                   # edit a file
$ caretline --new-state draft.md --size 40x6 > s.json  # capture a state
$ caretline --state s.json --keys '<d-down>The end.' --snapshot 40x6

Try it in one line

curl -fsSL https://caretline.app/install.sh | sh -s -- --demo

A prebuilt binary for macOS or Linux, checked against its sha256 and installed to ~/.local/bin, then caretline demo, the welcome (in caretline-cli 0.4.1): what caretline is, each demo as a chapter (a timed showcase, a hands-on tour, an agent co-editing beside you, layers over the text, animated scenes) and the commands to take away. Each demo also runs on its own: caretline demo showcase, tour, agent, layers or scenes. With Rust instead: cargo install caretline-cli. See the Quickstart.

What caretline is, and isn’t

caretline is a text-editing engine. It edits text, moves carets and keeps history; it reads text as blocks and draws the editing surface. It doesn’t know what your text means.

caretline iscaretline isn’t
Text manipulation and navigation: graphemes, words, rows, pagesA notes app, a task manager or an outliner with opinions about what a line means
Carets, several ranges, selectionsA store for anything but text, selections and marks
Undo and redo: Helix’s tree, typing runs, history transformed over changes from elsewhereA syntax highlighter or a language client
Generic structure: marks with payloads, blocks, depth, folds, block operationsStatuses, completion, due dates, priorities or checkboxes
Views: several per document, read-only, follow modesA persistence layer: it returns write_file, it never writes
Changes from elsewhere (external)A sync engine
Rendering the editing surface: cells, roles, rows, hit-testingColours, themes or glyphs for your concepts
State, messages, replay and the JSON protocol

What belongs in your app

Whatever your text means. If a line is a task, a ticket, a citation or a person, your app decides that, keeps any data it needs beside the text, and adds behaviour through four extension points (embedding.md):

Extension pointWhat it isExample
Host commandsPure functions from (document, view, args) to an edit, run by Msg::Command, recorded in traces“Cycle this line’s tag” on a key your app binds
Input rulesTake an editing message before the engine doesTyping [ ] at a line’s start makes a list item
Mark payloadsAny JSON value on a block’s mark, carried through edits, cut and paste, undoA database row id per block
DecorationsText and a role name drawn in a block’s hang or gutter; hit-testing reports whichA badge drawn beside each item

Every extension is a pure function registered under a name on the document’s Host, so state, traces and the protocol still work: replay registers the same functions. The default keymap binds only generic commands (keys.md); your app binds its own keys to its own commands.

What it covers

AreaStatusNotes
Grapheme-correct editingYesCarets never land inside an emoji, a ZWJ sequence, a flag or a combining accent
Wide charactersYesCJK and emoji take two cells, using Helix’s width table (unicode-width 0.1.12). Soft wrap never splits one or lets it overflow the column: it starts the next row
SelectionsYesAnchor and head per range, macOS text-field collapse rules
Multi-range selectionsEngine onlyupdate edits every range. No key creates extra ranges yet, but a state or a Msg sequence can
Transactions with position mappingYesEvery edit is a Helix Transaction; selections map through its changes
Undo and redoYesHelix’s revision tree. A typing run within 1.5 s is one step (up to 256 characters, breaking at a word past 128). Undo restores text and selection
Soft wrapYesVisual-line motion with a goal column, page up and down, Home/End per visual row
No-wrap modeYesconfig.soft_wrap = false scrolls sideways
ClipboardYes, as effectscopy/cut return clipboard_set; the runtime talks to the system clipboard
SavingYes, as effectssave returns write_file; the runtime writes and answers saved or save_failed
MouseYesClick, shift-click, drag and wheel, as click, drag and scroll messages
Serializable stateYesState round-trips through JSON, history and goal column included
Deterministic replayYesA trace (state + messages) replays to the identical state and frame
Headless snapshotsYes--snapshot WxH as plain text or ANSI
State protocol (serve, --listen, send)YesDrive a headless engine or a live editor over JSON lines. See protocol.md
MCP server for agentsYescaretline-mcp: an agent edits a live editor alongside a person, with guarded writes, attribution and replay. See mcp.md
Syntax highlighting, search, multiple buffersNot yet
Keys that add cursorsNot yet
Block structureYesBlock identity that survives edits, depth, folds, block operations, decorations. See structure.md
Markdown lists, headings, fencesYesMarkdown list editing, paste and files over the block structure. See markdown.md
Host extensionsYesCommands, input rules, mark payloads and decorations a host registers. See embedding.md
Command catalogYesEvery editing command with a stable id, and the default keymap as data. See keys.md

The layers

flowchart TB
    rope["ropey: the text as a rope"]
    helix["Helix model (vendored, MPL-2.0)<br/>Selection · Transaction · History · graphemes · DocumentFormatter"]
    elm["caretline<br/>State · Msg · update → Effects · view → Frame · keymap"]
    rope --> helix --> elm
    elm --> tty["Interactive terminal<br/><code>caretline FILE</code>"]
    elm --> cli["Headless CLI<br/><code>--keys --msgs --snapshot --replay</code>"]
    elm --> proto["State protocol<br/><code>serve</code> · <code>--listen</code> · <code>send</code>"]
    elm --> lib["Your program<br/>(Rust library or child process)"]
    proto --> mcp["MCP server for agents<br/><code>caretline-mcp</code>"]

The engine owns the editing rules. A runtime owns everything else: the clock, the terminal, files and the clipboard.

The crates

caretline is published on crates.io as caretline (cargo add caretline, imported as caretline::).

Crate in this repoWhat it isUsed by
caretlinecaretline: plain text on Helix’s model, in the Elm architecture. This documentation is about it.caretline-cli, thc-tui
caretline-cliThe caretline binary: the interactive editor and the headless tools
caretline-mcpThe caretline-mcp binary: an MCP server for agents
caretline-layersNew in 0.4 (crates.io 0.1): placement, tracking and lifecycle for hints, arrows and spotlights over a host’s screen; the host draws them. See layers.md
caretline-tourNew in 0.4 (crates.io 0.1): walkthroughs as data (steps of layers, narration and the host’s own scene), a pure reducer, predicates the host answers, and protocol ops. See tour.md

thc’s TUI is one host: it opens each of its documents as one caretline document of blocks, keys its own per-block data by mark, sends changes from its own store as external changes, and builds its own concepts on top with host commands and decorations (a case study).

Where to go next

You want toRead
Understand how it worksarchitecture.md
Call it from Rustapi.md
Look up a message, an effect or a keymessages.md, keys.md
Work with blocks, marks, folds and decorationsstructure.md
Edit Markdown lists and filesmarkdown.md
Add your own commands, data and drawingembedding.md
Use the caretline commandcli.md
Drive it over JSON linesprotocol.md
Let an agent edit alongside you (MCP)mcp.md
Put it inside your own appembedding.md
Point at things on your app’s screen: hints, arrows, spotlightslayers.md
Show someone round your app with a step-by-step walkthroughtour.md
Write or debug a testtesting.md
Know how fast it is, and its limitsperformance.md

License

caretline is MIT, except src/helix/, which is vendored from Helix and stays under the Mozilla Public License 2.0 file by file. caretline-cli is MIT. See embedding.md for what that means for you.

This page on GitHub: docs/README.md