@c This is part of the Emacs manual.
@c Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc.
@c See file emacs.texi for copying conditions.
@node Undo, Minibuffer, Basic, Top
@chapter Undoing Changes
@cindex undo
@cindex mistakes, correcting

  Emacs allows all changes made in the text of a buffer to be undone, up
to a certain amount of change.  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 @code{query-replace} make many
entries, and very simple commands such as self-inserting characters are
often grouped to make undoing less tedious.

@table @kbd
@item C-x u
Undo one batch of changes---usually, one command worth (@code{undo}).
@item C-_
The same.
@end table

@kindex C-x u
@kindex C-_
@findex undo
  The command @kbd{C-x u} or @kbd{C-_} is how you undo.  The first time
you give this command, it undoes the last change.  Point moves back to
where it was before the command that made the change.

  Consecutive repetitions of the @kbd{C-_} or @kbd{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, you can
redo changes you have undone by typing @kbd{C-f} or any other command that
will have no important effect, and then using more undo commands.

  If you notice that a buffer has been modified accidentally, the easiest
way to recover is to type @kbd{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 @kbd{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.

  You cannot undo mere cursor motion; only changes in the buffer
contents save undo information.  However, some cursor motion commands
set the mark, so if you use these commands from time to time, you can
move back to the neighborhoods you have moved through by popping the
mark ring (@pxref{Mark Ring}).

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

  The variable @code{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 @code{undo-strong-limit} sets a harsher
limit: the command which pushes the size past this amount is itself
forgotten.  Its default value is 30000.

  Regardless of the values of those variables, the most recent change is
never discarded, so there is no danger that garbage collection occurring
right after an unintentional large change might prevent you from undoing
it.

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