: Learning GNU Emacs, 3rd Edition

2.6.2 Undoing Changes

2.6.2 Undoing Changes

What happens if you make a mistake while you're editing? You can undo your changes by pressing C-_ or C-x u (for undo; conveniently, the toolbar also has an undo icon, a curved left arrow). By typing undo repeatedly, you can gradually work your way back to a point before your mistake.[16] Although the undo command is very powerful, saving your file frequently, if not compulsively, is nevertheless a good idea. We usually save a file whenever we stop typingeven if only for a few seconds. Train your fingers to press C-x C-s whenever you pause; it's a good habit to form.

If you're used to typing C-z to undo, you can easily change Emacs's behavior to match your habits. See "Making Emacs Work the Way You Want" at the end of this chapter for information on CUA mode.

What if you'd like to redo a command after you type undo? There is no formal redo command, but you can use undo in the following way. Just move the cursor in any direction, and type C-_ or C-x u again. Emacs redoes the last command you undid. You can repeat it to redo previous undos.

Although undo is an important command, it can be slow if you want to undo a large number of changes. Table 2-11 summarizes three methods for undoing changes and circumstances in which you might want to use them.

Table2-11.Methods for undoing changes

If you: Use this command:
Don't like the recent changes you've made and want to undo them one by one C-_ orC-x u(undo)
Want to undo all changes made since you last saved the file M-x revert-buffer Enter
Want to go back to an earlier version of the file (the file as it was when you started this editing session) C-x C-f filename~ Enter C-x C-w filenameEnter

We've already talked about undoing changes with undo; next we describe how to revert a buffer from a file and how to go back to an earlier version.


: 0.304. /Cache: 3 / 1