Skip to content
Undo/Redo
step 1/5

Reading — step 1 of 5

Read

~1 min readEditing Operations

Undo/Redo

User expects Ctrl+Z to reverse the last action. Implementation:

Command pattern:

  • Each edit is a command with do() and undo().
  • Stack of commands; pop to undo.
python

Undo stack:

python

Redo: separate stack. Cleared on new edit.

Grouping:

  • Single Ctrl+Z should undo a "logical" action.
  • Typing a word: each letter shouldn't undo individually.
  • Group consecutive insertions within timeout (e.g., 1 second).
  • Or: explicit boundaries (Enter, paste, etc.).
python

Persistent undo (rope-based):

  • Tree of versions; each edit creates a new node.
  • Undo = move pointer to parent.
  • Redo = move to child.
  • Branching: undo + edit creates a new branch.
  • Like Git for editor state.

Vim's undo tree is famous: g- and g+ navigate it.

Memory:

  • Per-edit memory cost.
  • Big paste = big undo entry.
  • Truncate after some max history depth or memory limit.

Discussion

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

Sign in to post a comment or reply.

Loading…