One of the most universally useful features of Emacs is the ability to edit multiple buffers at once and to display more than one buffer using windows and frames. The commands for doing this are simple; you learn only a few commands and yet experience a tremendous boost in productivity. The more you use multiple buffers, frames, and windows, the more uses you'll think of for them.
In this chapter, we discuss how to use buffers, windows, and frames. First we cover the most commonly used commands, then, in the case of buffers and windows, move on to some more esoteric commands. At the end of the chapter, we discuss bookmarks, a method for marking your place in a file.
Conceptually, Emacs is different from most applications in two important ways. First, its window terminology is different. Second, Emacs buffers are not tied to windows or frames, unlike most applications.
Let's get our terms
straight first. GUI windows are not
Emacs windows. Emacs calls GUI windows frames.
In part, this terminology is necessary because Emacs predates GUIs
and is still often used on terminals without GUI windows. Emacs
windows are split screens. We've seen them already;
for example, when you ask for keyboard help, you see it displayed in
a *Help*
buffer at the bottom of your screen.
Figures Figure 4-1 and Figure 4-2
show Emacs frames and Emacs windows. In Figure 4-1,
we see our dickens
and odyssey
buffers in two separate frames. Figure 4-2 shows a
single frame displaying two Emacs windows, one on top of the other,
showing these two files.
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.
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.
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.
Get Learning GNU Emacs, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.