Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Editing and Buffers

Undo and Redo

Emacs treats undo as an editing command. Learn C-/, C-x u, undo in region, how redo works in Emacs 28+, and why the undo history never loses a change.

Undo is where Emacs differs most from other editors. Instead of a stack that you pop and discard, Emacs treats undo itself as a change that can be undone. The result: no edit is ever lost, but you need a small mental model to use redo. This lesson builds that model.

The commands

Keys Command Effect
C-/ undo Undo the previous change
C-x u undo Same (easier on some keyboards)
C-_ undo Same, the older key (works in terminals)
C-? or C-M-_ undo-redo Redo, available from Emacs 28

Press C-/ repeatedly to walk back through the history. Each press undoes one chunk: Emacs groups consecutive typed characters, so a burst of typing is undone together, roughly 20 characters at a time, while commands such as C-w or M-% are separate steps. The mode line * disappears when you undo back to the saved state.

On some terminals C-? and C-/ are indistinguishable or not sent at all. Use M-x undo-redo or bind a key of your own, for instance (global-set-key (kbd "C-S-z") #'undo-redo).

How undo undoes itself

Suppose you type A, then B, then C, and undo twice: the buffer shows A. The two undos are now recorded as edits. If you now type D, the old redo path is not thrown away: the full history is A, B, C, undo, undo, D. To bring back C, you press C-g (or any non-undo command) to break the undo run, then press C-/ again, which undoes the undos:

  1. Type ABC, undo, undo (buffer shows A).
  2. Press C-g or move the cursor: this ends the sequence.
  3. Press C-/: it undoes the last undo, and B returns.
  4. Press C-/ again: C returns.

So in classic Emacs, "redo" means "break the chain, then undo the undos". With undo-redo (Emacs 28 and newer) you can instead undo, then redo one step at a time with the new command, as long as you did not make a new edit in between.

JS Stand-in: undo as a recorded change

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The stand-in's bookkeeping is simplified, but it shows the central point: undo adds to the history rather than discarding it.

Undo in a region

If the region is active when you press C-/, only changes within the region are undone. Select a paragraph, press C-/ repeatedly, and the rest of the buffer stays as it is. Without an active region, undo covers the entire buffer. (Setting the variable undo-in-region is not needed; this is default behavior in Transient Mark mode.)

The undo limits

Emacs keeps a finite history, set by undo-limit (160,000 bytes), undo-strong-limit (240,000) and undo-outer-limit. If you make very large edits, older undo entries may be dropped. Many users raise the limits:

ELISP
(setq undo-limit 800000
      undo-strong-limit 12000000
      undo-outer-limit 120000000)

Undo information is per buffer, and lives only for the life of the buffer; it is lost when you kill the buffer. Reverting a buffer from disk (M-x revert-buffer) also clears it, although M-x revert-buffer itself can be undone by C-/ in modern versions.

Undo trees and visualizers

Because history is linear in Emacs, once you have branched edits it is easy to lose track of an older state. The package undo-tree shows the history as a tree you can navigate; vundo is a lighter alternative that draws the tree in a small buffer (M-x vundo). Both are installed from the package archives, which a later chapter covers. Another safety net is file-based version control: saved states belong in Git, not in the undo list. Remember the helpers M-x revert-buffer and auto-save files described in Files, Saving and Quitting.

JS Stand-in: grouping typed characters into one undo step

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key undoes the last change?

    C-/, C-x u and C-_ all run undo.
  2. 2. In traditional Emacs undo, how do you 'redo'?

    Undo is itself recorded, so undoing an undo restores text. Emacs 28 also adds undo-redo.
  3. 3. What happens if you press C-/ while a region is active?

    Undo in region limits the undo to the selected text.
  4. 4. Where is undo history kept?

    Each buffer has its own undo list, which disappears when the buffer is killed.

Summary

  • C-/, C-x u and C-_ undo; undoing is recorded as a change, so no history is lost.
  • Break the chain and undo again to redo, or use undo-redo (Emacs 28+).
  • With an active region, undo affects only that region.
  • Raise the limits for big edits; use vundo or undo-tree to browse history; keep real history in Git.

Next: Buffers and Ibuffer