Выбрать главу

Figure 4-2. Editing dickens and odyssey in Emacs windows

From now on, when we say frame, we mean a separate GUI window. When we say window, we mean a portion of the current Emacs display. And from a practical standpoint, we emphasize that this is not an either-or proposition. Even if you prefer multiple frames, you will still use Emacs-style windows sometimes. Emacs itself will see to that.

4.1.2 Buffers: Independent of Windows and Frames

Now what about buffers? Essentially, both windows and frames are ways to display a buffer, which, as defined in Chapter 1, may contain a copy of a file or not. Buffers may contain files. They may be Emacs-generated buffers, like *Messages*, *scratch*, or *Help*. Or they may be buffers that you create but haven't written to a file.

Most GUI applications tie certain files to certain GUI windows or, in Emacspeak, frames. Emacs's detachment of buffers from their display (whether a split display or a separate frame) is more powerful and flexible. To be honest, most of the time we prefer using a single Emacs frame and switching between buffers using C-x b. It's much easier than mousing between frames or dealing with a split screen, though each has its advantages in some situations.

4.1.3 More About Buffers

How do you know how many buffers are active in Emacs and what they are? There are three ways: the buffer list (which appears in a window when you type C-x C-b), the Buffers menu (which lists active buffers and commands for navigating them), and the Buffer pop-up menu (accessed by holding down Ctrl and clicking the left mouse button, which lists buffers by mode).

Emacs creates its own specialized buffers. The names for these internal buffers generally have the format *buffer name*. *Help*, *scratch*, and *Buffer List* are just a few of the buffers that Emacs creates.

When you start Emacs, it generates two buffers:

*Messages*

*scratch*

*Messages* is a buffer where Emacs accumulates messages from its startup and from the minibuffer. *scratch* is just what it sounds like: a temporary scratchpad where you can type. It won't be saved unless you explicitly write it to a file using C-x C-w.

Of course, typically you edit files with Emacs. These files are then copied into buffers of the same name. If you ask for help, you'll also have a *Help* buffer.

The number of buffers you can have really has no limit. Most of the time, only one or two buffers are displayed, but even if you can't see them, all the buffers you create in an Emacs session are still active. You can think of them as a stack of pages, with the one being displayed as the top page. At any time, you can turn to another page (another buffer), or you can create a new page.

Each buffer has an associated major mode that determines much about how Emacs behaves in that buffer. For example, text mode, designed for writing text, behaves differently from Lisp mode, which is designed for writing Lisp programs.

You can display multiple buffers in separate windows or frames or both. The important thing to remember is that all the buffers you create are active even if they are not currently displayed.

4.2 Working with Multiple Buffers

If you want to create a buffer that contains a file, simply type C-x C-f to find the file. Emacs automatically creates a second buffer and moves you there. If you already have a copy of the file in a buffer, C-x C-f just moves you to the existing buffer. This move is sensible and probably really what you want anyhow; if C-x C-f read the file from disk every time, you could end up with many versions of the same file that were each slightly different. If the filename you give C-x C-f doesn't exist, Emacs assumes you want to create a new file by that name and moves you to a blank buffer.

4.2.1 Switching Buffers

C-x C-f is always followed by a filename. The command for moving between buffers, C-x b, is followed by a buffer name. Did you realize that the mode line doesn't display filenames but only buffer names? Some versions of Emacs show both, but GNU Emacs shows only the buffer name. The buffer name and the filename, if any, are the same unless you change them (see the section "Renaming Buffers," later in this chapter).

To move between the buffers, type C-x b. Emacs shows you a default buffer name. Press Enter if that's the buffer you want, or type the first few characters of the correct buffer name and press Tab. Emacs fills in the rest of the name. Now press Enter to move to the buffer.

You can do the following with C-x b:

If you type C-x b followed by: Emacs:
A new buffer name Creates a new buffer that isn't connected with a file and moves there.
The name of an existing buffer Moves you to the buffer (it doesn't matter whether the buffer is connected with a file or not).

If you want to create a second (or third or fourth, etc.) empty buffer, type C-x b. Emacs asks for a buffer name. You can use any name, for example, practice, and press Enter. Emacs creates the buffer and moves you there. For example, assume you've been working on your tried-and-true dickens buffer. But you'd like something new, so you start a new buffer to play with some prose from James Joyce.

Type: C-x b joyce

You typed a new buffer name.

Type: Enter

Now you have a new buffer named joyce to type in.

This procedure isn't all that different from using C-x C-f; about the only difference is that the new buffer, joyce, isn't yet associated with a file. Therefore, if you quit Emacs, the editor won't ask you whether or not you want to save it.

C-x b is especially useful if you don't know the name of the file you are working with. Assume you're working with some obscure file with an unusual name such as .saves-5175-pcp832913pcs.nrockv01.ky.roadrunner.com. Now assume that you accidentally do something that makes this buffer disappear from your screen. How do you get .saves-5175-pcp832913pcs.nrockv01.ky.roadrunner.com back onto the screen? Do you need to remember the entire name or even a part of it? No. Before doing anything else, just type C-x b. The default buffer is the buffer that most recently disappeared; type Enter and you'll see it again.

Alternatively, the Buffer Menu popup lists buffers by major mode, and you can choose one. Hold down Ctrl and click the left mouse button to see a pop-up menu of your current buffers. (The Buffers menu at the top of the screen also shows all current buffers.)