manpagez: man pages & more
info emacs
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

21.1 Undo

The undo commands undo 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 as back these 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. Meanwhile, self-inserting characters are usually grouped to make undoing less tedious.

C-x u

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

To begin to undo, type the command C-x u (or its aliases, C-_ or C-/). This undoes the most recent change in the buffer, and moves point back to where it was before that change.

Consecutive repetitions of C-x u (or its aliases) undo earlier and earlier changes in the current buffer, back to the limit of the current buffer's undo records. If all the recorded changes have already been undone, the undo command just signals an error.

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 canceled. 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 below.

Any command other than an undo command breaks the sequence of undo commands. Starting from that moment, the previous undo commands become ordinary changes that you can undo. Thus, to redo changes you have undone, type C-f or any other command that will harmlessly break the sequence of undoing, then type undo commands again. On the other hand, 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.

Ordinary undo applies to all changes made in the current buffer. You can also perform selective undo, limited to the region.

To do this, specify the region you want, then run the undo command with a prefix argument (the value does not matter): C-u C-x u or C-u C-_. This undoes the most recent change in the region. To undo further changes in the same region, repeat the undo command (no prefix argument is needed). In Transient Mark mode (see section Transient Mark Mode), any use of undo when there is an active region performs selective undo; you do not need a prefix argument.

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

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

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 20000. The variable undo-strong-limit sets a stricter limit: a previous command (not the most recent one) which pushes the size past this amount is itself forgotten. The default value of undo-strong-limit is 30000.

Regardless of the values of those variables, the most recent change is never discarded unless it gets bigger than undo-outer-limit (normally 3,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 section Reporting Bugs.

The reason the undo command has three key bindings, C-x u, C-_ and C-/, is that it is worthy of a single-character key, but C-x u is more straightforward for beginners to remember and type. Meanwhile, C-- on a text-only terminal is really C-_, which makes it a natural and easily typed binding for undoing.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© 2000-2024
Individual documents may contain additional copyright information.