Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 4: Basic Editing

Previous HourNext Hour

Sections in this Hour:

 

Editing a File


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.

Figure 4.11
Type C-x C-f (find-file) to open a file.

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).

Figure 4.12
Double slashes in pathnames.

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).

Figure 4.13
Opening a nonexistent file is equal to creating a new one.

The following are two reasons you might open a nonexistent file:

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.

Saving Buffers to a 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!

Exiting Emacs After Editing

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.

Tip - If it's hard for you to remember the commands in this section, it might help you to know that both are available from the Files menu in GNU Emacs and from the File menu in XEmacs.

Even though these keybindings can be difficult--and tough to remember--don't interpret this as an indication that Emacs is hard to use. Realize that you won't use these commands as much you do others (such as cutting and pasting); therefore, these commands don't need to be as accessible as the others.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 4: Basic Editing

Previous HourNext Hour

Sections in this Hour: