GNU Emacs
Emacs
Dashboard

16.1 Undo

The undo command reverses recent changes in the buffer’s text. Each buffer records changes individually, and the undo command always applies to the current buffer. You can undo all the changes in a buffer for as far back as the buffer’s records go. Usually, each editing command makes a separate entry in the undo records, but some commands such as query-replace divide their changes into multiple entries for flexibility in undoing. Consecutive character insertion commands are usually grouped together into a single undo record, to make undoing less tedious.

C-/``C-x u``C-_

Undo one entry in the current buffer’s undo records ( undo).

To begin to undo, type C-/ (or its aliases, C-_ or C-x u) 6. This undoes the most recent change in the buffer, and moves point back to where it was before that change. Consecutive repetitions of C-/ (or its aliases) undo earlier and earlier changes in the current buffer. If all the recorded changes have already been undone, the undo command signals an error.

Any command other than an undo command breaks the sequence of undo commands. Starting from that moment, the entire sequence of undo commands that you have just performed are themselves placed into the undo record. Therefore, to re-apply changes you have undone, type C-f or any other command that harmlessly breaks the sequence of undoing; then type C-/ one or more times to undo some of the undo commands.

Alternatively, if you want to resume undoing, without redoing previous undo commands, use M-x undo-only. This is like undo, but will not redo changes you have just undone. To complement it, M-x undo-redo will undo previous undo commands (and will not record itself as an undoable command).

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 (see The Mode Line). 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. If you do not remember whether you changed the buffer deliberately, type C-/ once. When you see the last change you made undone, you will see whether it was an intentional change. If it was an accident, leave it undone. If it was deliberate, redo the change as described above.

Alternatively, you can discard all the changes since the buffer was last visited or saved with M-x revert-buffer (see Reverting a Buffer).

When there is an active region, any use of undo performs selective undo: it undoes the most recent change within the region, instead of the entire buffer. However, when Transient Mark mode is off (see Disabling Transient Mark Mode), C-/ always operates on the entire buffer, ignoring the region. In this case, you can perform selective undo by supplying a prefix argument to the undo command: C-u C-/. To undo further changes in the same region, repeat the undo command (no prefix argument is needed).

Some specialized buffers do not make undo records. Buffers whose names start with spaces never do; these buffers are used internally by Emacs to hold text that users don’t normally look at or edit.

When the undo information for a buffer becomes too large, Emacs discards the oldest records from time to time (during garbage collection). You can specify how much undo information to keep by setting the variables undo-limit, undo-strong-limit, and undo-outer-limit. Their values are expressed in bytes.

The variable undo-limit sets a soft limit: Emacs keeps undo data for enough commands to reach this size, and perhaps exceed it, but does not keep data for any earlier commands beyond that. Its default value is 160000. The variable undo-strong-limit sets a stricter limit: any previous command (though not the most recent one) that pushes the size past this amount is forgotten. The default value of undo-strong-limit is 240000.

Regardless of the values of those variables, the most recent change is never discarded unless it gets bigger than undo-outer-limit (normally 24,000,000). At that point, Emacs discards the undo data and warns you about it. This is the only situation in which you cannot undo the last command. If this happens, you can increase the value of undo-outer-limit to make it even less likely to happen in the future. But if you didn’t expect the command to create such large undo data, then it is probably a bug and you should report it. See Reporting Bugs.


Footnotes

(6)

Aside from C-/, the undo command is also bound to C-x u because that is more straightforward for beginners to remember: ‘ u’ stands for “undo”. It is also bound to C-_ because typing C-/ on some text terminals actually enters C-_.