Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||||||||||
Hour 4: Basic Editing |
||||||||||||||||||||||
|
The simplest way to edit a file in Emacs is to start Emacs with this filename as an argument. When you have Emacs running, you can load a file by pressing C-x C-f (find-file). This asks you for a filename, as can be seen in Figure 4.11.
Emacs suggests the current buffers path to you to start with. The reason for this is that most of the time you open files located in the same directory as the one you are currently editing. If the file you are searching for is in another directory, you can edit the suggested path. An alternative is to type the new path (see Figure 4.12).
A double slash (//) means ignore everything up to this point and interpret the rest of the line as a path from the root of the filesystem. Likewise /~/ means forget all up to this point and interpret the rest of the line as a directory rooted in your home directory. (/usr/local/~/Emacs is thus equal to .emacs.) This is very handy if you want to open a file in another place on your hard drive (for example, your home directory). Simply type the path as if Emacs hasn't suggested anything.
If the file you name doesn't exist on the hard drive, it is interpreted as a request for a new file. Thus creating a new file is equal to opening a nonexistent file. When you have opened a nonexistent file, Emacs reminds you that this is a new file by displaying a message in the minibuffer (see Figure 4.13).
The following are two reasons you might open a nonexistent file:
You intend to edit a new file.
You typed the wrong name (that is, it was another file you wanted to edit).
In the latter case, you can kill the buffer created for the new file with the command C-x k (kill-buffer) and then try once again to open a file. A better alternative, though, is to press C-x C-v (find-alternate-file), which does both things in one go.
Note - In Emacs, loading a file or opening a file is called finding a file. This is for historical reasons. Thus the command that loads in a file is called find-file and not load-file. |
To save a buffer, simply press C-x C-s (save-buffer). This saves the content of the buffer to the file from which it was originally loaded. If the buffer does not correspond to a loaded file--which is the case if you write text in the initial buffer that you see when Emacs starts--Emacs asks for a filename for the buffer.
You might sometimes get in a situation where you read in text from one file and need to write it to another file. Pressing C-x C-s does not do it, because it saves your buffer to the file it was read from without asking for a name. Instead you can press C-x C-w (write-file), which asks for a filename and saves the buffer to that file. You can also use this method if you have edited a buffer that you haven't read in from a file (for example, the *scratch* buffer, which you are presented with when you start Emacs).
Note - When you have written the content of your buffers to a new file, all subsequent saves will be to this file too! |
When you are finished editing, you can exit Emacs by pressing C-x C-c (save-buffers-kill-emacs). If any of your buffers contain unsaved information, Emacs asks you whether you want to save them first.
Sams Teach Yourself Emacs in 24 Hours |
||||||||||||||||||||||
Hour 4: Basic Editing |
||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.