-->
Previous Table of Contents Next


Undoing a Command

In emacs, you can “undo” your most recent action or change to the buffer, as long as you have not saved that change to the disk file. You can undo by pressing <Ctrl-x><u>. By repeatedly using this command, you can undo the changes made to the buffer.


NOTE:  emacs keeps track of the “undone” material first in memory buffers and then in a file, so theoretically you could undo every change you’ve made to a buffer up to the limits of your disk space. Practically, though, you’ll find the undo command useful for only the last few commands or edits you’ve done.

Unfortunately, you can’t use the undo command to undo writing something to a file as opposed to writing to the buffer.

If you want to reread a file from disk (thus overwriting your current buffer changes), you can press <Ctrl-x><Ctrl-r>. This command reads the specified file into the current buffer, erasing its previous contents. Thus, if you specify the same filename, emacs replaces the current buffer with the contents of the file on disk. This is a quick way to undo many changes without exiting and restarting emacs.

But what if the emacs has autosaved the file or you’ve saved the file with unwanted changes? Well, emacs creates a backup file the first time you save a file, but not until you save the file. The name of this backup file is the same as the filename, except with a # character at the beginning and end of the name. Thus, if your file was named emacs-prtc.1, the backup filename is #emacs-prtc.1#. If you accidentally overwrite your current file with unwanted changes, you may be able to use the backup file to start over.

Writing Files and Saving the Buffer

You’ve seen how to write the buffer to a file and quit emacs. Sometimes, however, you want to save the buffer to a file without quitting emacs. You should save the file regularly during an editing session. If the system goes down because of a crash or a power failure, you may lose your work if you haven’t saved it recently. To save the buffer, press <Ctrl-x><Ctrl-s>.

If you started emacs without specifying a filename, you must provide a filename if you want to save the file to disk. In this case, you press <Ctrl-x><Ctrl-s>, type the filename, and press <Return>.

You may want to save the buffer to a new filename different from the one you originally started with. For example, you start emacs with the file emacs-pract.1, make some changes to the file, and want to save the changes to a new file without losing the original emacs-pract.1 file. To save the file with a new filename, press <Ctrl-x><Ctrl-w>. emacs prompts you for the filename. The buffer is then written to the named file. If the command is successful, you see the name of the file.

If you specify the name of an existing file, a message appears in the mini-buffer, asking whether you want to overwrite the file. Simply answer the question appropriately.

Using Files

If you want to load another file to edit, emacs can let you load a new file into the current buffer, or load a file into a new buffer, leaving the current buffer alone. emacs also lets you insert the contents of a file into the current buffer.

To replace the current buffer with the contents of another file, press <Ctrl-x><Ctrl-v>. emacs prompts for a filename in the mini-buffer. If you don’t remember the entire filename or if the name is rather long, you can use the completion option of emacs. When emacs prompts for a filename, you can enter just the first few letters in the name and then press <Tab>. emacs then expands the filename to match any files with those first few letters. If more than one file matches, emacs displays a window containing all the files matching the characters you entered and allows you to choose one.

To retrieve a file into a new buffer, press <Ctrl-x><Ctrl-f>. Enter the filename at the mini-buffer prompt. emacs normally names the buffer after the filename, but you can change the name of the buffer by pressing <Esc><x>, entering the new name for the buffer, and pressing <Return>. emacs prompts for the new name. Enter the new buffer name and press <Return>. The mode line displays the new name.

To insert a file into the current buffer, simply move the cursor to the desired position in the file and press <Ctrl-x><i>.


Previous Table of Contents Next