build-a-text-editor / lesson-13.md
Lesson 13 · The cursor

Moving by words

Hopping a word at a time is what makes navigation fast. Today you add word-wise motion that skips to the start of the next word and back to the start of the previous one.

The goal

Move the cursor to the next word's start and the previous word's start along a line.

Start here - the target
TO DO
Scenario: Word-wise horizontal motion
Givenan editor over "foo bar baz" with the cursor at Col 0
WhenMoveWordRight and MoveWordLeft are called
ThenMoveWordRight gives Col 4 (start of "bar"), then Col 8 (start of "baz"), then Col 11 (the line end, no next word)
Andfrom Col 8, MoveWordLeft gives Col 4, then Col 0, and stays at Col 0
Background

Word motion turns navigation from a crawl into a hop. The rule is defined by what counts as a word character - here a letter or digit - with everything else (spaces, punctuation) acting as a separator. MoveWordRight skips the rest of the word you are in, then skips the separators after it, and lands on the first character of the next word. MoveWordLeft does the reverse: skip separators to the left, then skip the word behind them, stopping at that word’s start.

The edges are where this earns its test. Moving right off the last word has no next word to find, so it clamps at the line end rather than running past it; moving left from the first word stops at column 0. Keeping the motion within a single line for now keeps the rule crisp - one line, one scan, clear word boundaries - and matches how MoveLeft and MoveRight stayed on their line too. The behavior is the familiar Ctrl-arrow jump, built from one clear definition of where a word begins.

Make it work
// a "word char" is a letter or digit; anything else separates words.
// MoveWordRight: skip the current run of word chars, then skip the
// separators, landing on the next word's first char (or the line end).
// MoveWordLeft is the mirror: skip separators left, then skip word chars.
func isWord(c byte) bool { /* letter or digit */ }
CheckpointDONE
The cursor can jump word by word along a line. Commit and stop here.