Skip to content
Line-Aware Editing
step 1/5

Reading — step 1 of 5

Read

~1 min readEditing Operations

Line-Aware Editing

Text editors are line-oriented at the user level: cursor moves up/down by line, line numbers display, selections often span lines.

Track lines:

  • For each line: byte offset where it starts.
  • Update on insert/delete.
python

On insert: increment all line_starts after pos by len(inserted), and add new line_starts for newlines in inserted text.

On delete: similar, decrement and remove crossed newlines.

Modern editors maintain this incrementally for O(log N) updates (e.g., piece-table extends with line break index per piece).

Tab handling:

  • Tabs expand to spaces visually (e.g., tab = 8 cols, or 4 cols).
  • Some editors store as actual tab char; visually align.
  • Configuration nightmare.

Unicode awareness:

  • Byte position vs character position vs grapheme cluster.
  • Emoji (👨‍👩‍👧‍👦 = 7 codepoints + ZWJ joiners) are one visible character.
  • Backspace deletes the whole grapheme cluster.
  • Cursor moves visually, not by codepoint.

UTF-8 encoded:

  • ASCII = 1 byte.
  • Latin = 2 bytes.
  • BMP = 3 bytes.
  • Emoji + supplementary = 4 bytes.

For internal storage, byte position is fine. For display, need to know grapheme boundaries.

Bidirectional text (Arabic, Hebrew):

  • Mixed LTR + RTL.
  • Visual order ≠ logical order.
  • Unicode bidi algorithm handles.
  • Most editors get this approximately right; edge cases break.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Line-Aware Editing — Build a Text Editor