If you're used to typing C-zto 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 undoin the following way. Just move the cursor in any direction, and type C-_or C-x uagain. Emacs redoes the last command you undid. You can repeat it to redo previous undos.
Although undois an important command, it can be slow if you want to undo a large number of changes. Table 2-11summarizes three methods for undoing changes and circumstances in which you might want to use them.
Table 2-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-_ or C-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 filename Enter |
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.
2.6.3 Reverting a Buffer from a File
If the undocommand isn't useful, there's another way to restore a file to an earlier state. If you want to get the file back to the state that is stored on disk, type M-x revert-buffer Enter. Emacs asks the following question:
Revert buffer from file filename ? (yes or no)
The filename is the name of your original file. Type yesif you want to restore the file, or noif you've changed your mind. Emacs copies the file stored on disk into the buffer, conveniently forgetting everything that happened since the last time you saved the file. Although this command is called revert-buffer, note that it can revert only buffers associated with files.
2.6.4 Going Back to a Previous Version: Backup Files
The first time you save a file during an editing session, Emacs creates a backup file. If something disastrous happens, and the other techniques for undoing changes won't help you, you can always return to the backup file. The name of the backup file is the same as the name of the file you're editing, with a tilde ( ~) added. For example, if you are editing the file text , the backup file is text~ .
Emacs doesn't provide any special commands for restoring a buffer from the backup copy. The easiest way to do this is to edit the backup copy and then save it as the real file. For example, if you were working with a file called text , you could: exit Emacs by typing C-x C-c, then start Emacs again by typing emacs text~ . After the backup file is displayed, save it as the real file by typing C-x C-w text Enter. As a safeguard, Emacs asks you before it writes over the original file:
File text exists; overwrite? (y or n)
Type yto overwrite the original file with the backup file.
GNU Emacs also has a numbered backup facility. If you turn on numbered backups, Emacs creates a backup file (with the suffix ~n~ ) every time you save your file. n increments with each successive save. If you are nervous about deleting older versions, it might be worth using: you can keep all of your old versions forever, if you want to. However, numbered backups can also waste disk space; a happy medium may be to tell Emacs to keep the last n versions, where n is the number of versions you want to keep. The variables that control numbered backups are described in Appendix A. If you are interested in full-blown version control, check out VC mode, discussed in Chapter 12. Table 2-12summarizes the commands for stopping commands and undoing changes.
Table 2-12. Stopping and undoing commands
Keystrokes |
Command name |
Action |
C-g |
keyboard-quit |
Abort current command. |
C-x u |
advertised-undo [17] |
Undo last edit (can be done repeatedly). |
C-_ Edit → Undo |
undo |
Undo last edit (can be done repeatedly). |
( none ) |
revert-buffer |
Restore buffer to the state it was in when the file was last saved (or auto-saved). |
2.6.5 Recovering Lost Changes
We've just discussed how to eliminate changes you don't want to keep; getting back changes you've lost is a different kind of problem. You might lose changes if the power goes out momentarily or if the computer you're working on suddenly freezes or is turned off accidentally. You might also lose changes if you exit Emacs abnormally. Luckily, Emacs, being the watchful editor that it is, saves your file for you every so often in auto-save files . If you watch carefully, you'll see the message Auto savingin the minibuffer from time to time. Using auto-save files, you can get back most, if not all, of your changes. The name of an auto-save file is the same as the name of the file you are editing, with a sharp ( #) added to the beginning and the end. For example, if you are editing the file text , its auto-save file is #text# .
To recover text from an auto-save file, type M-x recover-file Enter. Emacs opens a window that lists both the file and its associated auto-save file so that you can compare the time at which they were created, their size, and so forth. Emacs asks you the following question:
Recover auto-save file #text# ? (yes or no)
Type yesto confirm that you want to copy the contents of the auto-save file into the current file or noif you change your mind. (If you are unsure, you might want to use C-x C-fto read the auto-save file #text# into a buffer first and look it over carefully before using the recover-filecommand. If you really want to compare the differences between the two versions, see "Comparing Files Between Windows" in Chapter 4.)
When does Emacs create auto-save files? Emacs creates an auto-save file every few hundred keystrokes or if Emacs is terminated abnormally. [18]You can change the frequency with which Emacs creates auto-save files by changing the variable auto-save-interval. By default, Emacs creates an auto-save file every 300 keystrokes. For more information on changing variable values, see Chapter 10.
There's one more important fact to know about Emacs and auto-save files. If you delete a large portion of a file, Emacs stops auto-saving the file and displays a message telling you so. To make Emacs start auto-saving again, save the file with C-x C-sor type M-1 M-x auto-save Enter(that's the number 1).
Now you've learned enough commands for most of the editing you'll do with Emacs. At this point, you may want to learn how to make Emacs turn on certain features like auto-fill mode automatically, so you don't have to turn them on every time you enter Emacs. The next section provides a brief introduction to customization; this topic is covered in much greater detail in Chapter 10.
Читать дальше