Go to the previous, next section.

Undoing Changes

Emacs allows all changes made in the text of a buffer to be undone, up to a certain amount of change (8000 characters). Each buffer records changes individually, and the undo command always applies to the current buffer. Usually each editing command makes a separate entry in the undo records, but some commands such as query-replace make many entries, and very simple commands such as self-inserting characters are often grouped to make undoing less tedious.

C-x u
Undo one batch of changes (usually, one command worth) (undo).
C-_
The same.

The command C-x u or C-_ is how you undo. The first time you give this command, it undoes the last change. Point moves to the text affected by the undo, so you can see what was undone.

Consecutive repetitions of the C-_ or C-x u commands undo earlier and earlier changes, back to the limit of what has been recorded. If all recorded changes have already been undone, the undo command prints an error message and does nothing.

Any command other than an undo command breaks the sequence of undo commands. Starting at this moment, the previous undo commands are considered ordinary changes that can themselves be undone. Thus, to redo changes you have undone, type C-f or any other command that will have no important effect, and then give more undo commands.

If you notice that a buffer has been modified accidentally, the easiest way to recover is to type C-_ repeatedly until the stars disappear from the front of the mode line. At this time, all the modifications you made have been cancelled. If you do not remember whether you changed the buffer deliberately, type C-_ once, and when you see the last change you made undone, you will remember why you made it. If it was an accident, leave it undone. If it was deliberate, redo the change as described in the preceding paragraph.

Whenever an undo command makes the stars disappear from the mode line, it means that the buffer contents are the same as they were when the file was last read in or saved.

Not all buffers record undo information. Buffers whose names start with spaces don't; these buffers are used internally by Emacs and its extensions to hold text that users don't normally look at or edit. Also, minibuffers, help buffers and documentation buffers don't record undo information. Use the command buffer-enable-undo to enable recording undo information in the current buffer.

As editing continues, undo lists get longer and longer. To prevent them from using up all available memory space, garbage collection trims back their sizes to thresholds you can set. (For this purpose, the "size" of an undo list measures the cons cells that make up the list, plus the strings of deleted text.)

Two variables control the range of acceptable sizes: undo-limit and undo-strong-limit. Normally, the most recent changes are kept until their size exceeds undo-limit; all older changes are discarded. But if a change pushes the size above undo-strong-limit, it is discarded as well as all older changes. One exception: the most recent set of changes is sacred; garbage collection never discards that. (In Emacs versions 18.57 and 18.58, these variables are called undo-threshold and undo-high-threshold.)

The reason the undo command has two keys, C-x u and C-_, set up to run it is that it is worthy of a single-character key, but the way to type C-_ on some keyboards is not obvious. C-x u is an alternative you can type in the same fashion on any terminal.

Go to the previous, next section.