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.
Figure 4-1. Editing dickens and odyssey in Emacs frames
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.
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.
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.
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.)
Hold down Ctrl and click the left mouse button.
Emacs displays a pop-up menu of current buffers by mode (Mac OS X).
To cycle through all the buffers you have, type C-x → to go to the next buffer (in the buffer list) or C-x to go to the previous buffer. (Don't hold down Ctrl while you press the arrow key or Emacs beeps unhappily.)
It's easy to create buffers, and just as easy to delete them when you want to. You may want to delete buffers if you feel your Emacs session is getting cluttered with too many buffers. Perhaps you started out working on a set of five buffers and now want to do something with another five. Getting rid of the first set of buffers makes it a bit easier to keep things straight. Deleting a buffer can also be a useful emergency escape. For example, some replacement operation may have had disastrous results. You can kill the buffer and choose not to save the changes, then read the file again.
Deleting a buffer doesn't delete the underlying file nor is it the same as not displaying a buffer. Buffers that are not displayed are still active whereas deleted buffers are no longer part of your Emacs session. Using the analogy of a stack of pages, deleting a buffer is like taking a page out of the current stack of buffers you are editing and filing it away.
Deleting buffers doesn't put you at risk of losing changes, either. If you've changed the buffer (and the buffer is associated with a file), Emacs asks if you want to save your changes before the buffer is deleted. You will lose changes to any buffers that aren't connected to files, but you probably don't care about these buffers.
Deleting a buffer is such a basic operation that it is on the Emacs toolbar, the X symbol. Now let's learn how to do it from the keyboard to increase your fluency in Emacs.
To delete a buffer, type C-x k (for kill-buffer). Emacs shows the name of the buffer currently displayed; press Enter to delete it or type another buffer name if the one being displayed is not the one you want to delete, then press Enter. If you've made changes that you haven't yet saved, Emacs displays the following message:
Buffer buffer name modified. Kill anyway? (yes or no).
To ditch your changes, type yes, and Emacs kills the buffer. To stop the buffer deletion process, type no. You can then type C-x C-s to save the buffer, followed by C-x k to kill it.
You can also have Emacs ask you about deleting each buffer, and you can decide whether to kill each one individually. Type M-x kill-some-buffers to weed out unneeded buffers this way. Emacs displays the name of each buffer and whether or not it was modified, then asks whether you want to kill it. Emacs offers to kill each and every buffer, including the buffers it creates automatically, like
*scratch*
and *Messages*
. If you kill all the buffers in your session, Emacs creates a new *scratch*
buffer; after all, something has to display on the screen!
Windows are areas on the screen in which Emacs displays the buffers that you are editing. You can have multiple windows on the screen at one time, each displaying a different buffer or different parts of the same buffer. Granted, the more windows you have, the smaller each one is; unlike GUI windows, Emacs windows can't overlap, so as you add more windows, the older ones shrink. The screen is like a pie; you can cut it into many pieces, but the more pieces you cut, the smaller they have to be. You can place windows side-by-side, one on top of the other, or mix them. Each window has its own mode line that identifies the buffer name, the modes you're running, and your position in the buffer. To make it clear where one window begins and another ends, mode lines are usually shaded.
As we've said, windows are not buffers. In fact, you can have more than one window on the same buffer. Doing so is often helpful if you want to look at different parts of a large file simultaneously. You can even have the same part of the buffer displayed in two windows, and any change you make in one window is reflected in the other.
The difference between buffers and windows becomes important when you think about marking, cutting, and pasting text. Marks are associated with buffers, not with windows, and each buffer can have only one mark. If you go to another window on the same buffer and set the mark, Emacs moves the mark to the new location, forgetting the place you set it last.
As for cursors, you have only one cursor, and the cursor's location determines the active window. However, although there is only one cursor at a time, each window does keep track of your current editing location separately—that is, you can move the cursor from one window to another, do some editing, jump back to the first window, and be in the same place. A window's notion of your current position (whether or not the cursor is in the window) is called the point. Each window has its own point. It's easy to use the terms point and cursor interchangeably—but we'll try to be specific.
You can create horizontal windows or vertical windows or both, but personally we place vertical windows with the more advanced esoterica near the end of the chapter. Here we'll discuss creating horizontal windows, finding a file in a new window, and deleting windows.
The most commonly used window command is C-x 2 (for split-window-vertically). This command splits the current window into two, horizontally oriented windows. You can repeat this command to split the screen into more horizontal windows.
Initial state:
Editing our trusty
dickens
buffer.
Type: C-x 2
The screen is divided into two horizontal windows; the mode line demarcates each window.
You can also have Emacs set up windows for you when you start a session. If you want to edit two files in horizontal windows, specify their filenames when you start Emacs at a command prompt. For example, if you wanted to edit dickens and joyce, you would type emacs dickens joyce and Emacs would display these files in two horizontal windows. If you try this with more than two files, Emacs displays two horizontal windows, with a file in one and a list of buffers in the other.
A number of the "other window" commands are just the ordinary command with a 4 inserted in it. For example, to find a file in another window, type C-x 4 f. (If only one window is currently open, Emacs opens another one.) To select a different buffer in another window, type C-x 4 b. Many users find these commands preferable to the normal C-x C-f and C-x b commands because they save you a step: you need not move to the window, give a command, and move back.
Once you've got multiple windows open, it's helpful to be able to scroll them without moving there. To scroll the other window, type C-M-v.
To move from one window to another, type C-x o (o stands for other in this command). If you have more than two windows displayed, Emacs moves from one to the next. There's no way to specify which window to move to, so you may have to type C-x o a few times to get to the one you want if you have more than two windows displayed. (You can also click your mouse in a window if you're using the GUI version.)
Now that you can create windows and can move between them, what else can you do? Practically anything. With our two windows on dickens open, one on top of the other. Initially, both of these windows are looking at the same file.
Type: C-x 2
Two windows open on
dickens
.
We can give any editing commands we want within either window. We can move back and forth in one window without affecting the other. Let's see what happens if we want to edit another file.
Type: C-x C-f blake
Now you have two windows, two buffers, and two files.
By using C-x o, we can edit one file and then the other. We can kill text from one buffer and yank it back in another. For example, let's move the first line of Blake's poem to the top of the dickens buffer.
Type: C-k C-k C-x o M-< C-y Enter
The Blake text has been yanked into the
dickens
buffer.
Editing with multiple buffers in separate windows is particularly useful if, for example, you want to copy material from one file to another or if you want to read a file containing reference material while editing another. Programmers often need to look at several different files at the same time—for example, a header file and a code file, or a function call site and the routine that's being called. Once you get used to the commands for moving between different windows, you may spend most of your time with two or three windows on your screen.
Deleting a window only means that it isn't displayed anymore; it doesn't delete any of the information or any of your unsaved changes. The underlying buffer is still there, and you can switch to it using C-x b. To delete the window you're in, type C-x 0 (zero). If you want to delete all windows but the one you're working on, type C-x 1 (one), meaning "make this my one and only window." As you'd expect, the remaining window "grows" to fill up the rest of the space. You can also delete all windows on a certain buffer by typing: M-x delete-windows-on Enter
Enter.buffername
By now you know that Emacs calls GUI windows "frames." In this section, we'll cover how to create frames, navigate between frames, and delete frames.
To open a new frame, type C-x 5 2 (for make-frame). Emacs makes a new frame containing the current buffer and puts it on top of the current frame.
If your new frame completely overlaps your current frame, you may need to size the new frame to tell them apart. For a more convenient solution, add these lines to your .emacs file:
(setq initial-frame-alist '((top . 10) (left . 30)
(width . 90) (height . 50)))
(setq default-frame-alist '((width . 80) (height . 45)))
These lines set up sizes for the width and height of Emacs frames. The first frame is the size set in initial-frame-alist (in this example, 90 characters wide by 50 lines high with top and left defining an inset), and subsequent frames, specified by default-frame-alist, will be 80 characters wide and 45 lines high. Depending on your display, you can make these numbers smaller or larger.
Here we edit a bit of Henry James.
Type: C-x 5 2
Emacs opens a new frame titled
james
Note the title of your new frame. The first frame in your session, your initial Emacs frame, displays
Emacs@system name
at the top (or Emacs's best guess at the system name). Any other frames you create display the buffer name at the top. In fact, once you have multiple frames, all frames display the buffer name as their title. If you delete all frames but one, the title once again reverts to Emacs@system name
.
Let's say we want to open a frame on our
dickens
buffer.
Type: C-x 5 f dickens Enter
Emacs opens a new frame on
dickens
.
If you type C-x b to move to another buffer, the name at the top of the frame changes to the new buffer's name (and on Linux, it shows the path as well). To move to a buffer and put it in a new frame, type C-x 5 b. You might have guessed that one.
You can move between frames in several ways. You can use the mouse to select a frame or press C-x 5 o to go to another frame. To see a list of current frames, select Frames from the Buffers menu. (If you have only one frame, the Frames option does not appear on this menu.)
To get rid of a frame, press C-x 5 0. Emacs deletes the frame you are in. Deleting a frame, like deleting a window, affects only the display. The underlying buffer is still active, and you can move to it by typing C-x b.
If you try to use C-x 5 0 to delete the only frame that is left, Emacs won't do it. To exit Emacs, type C-x C-c or close the frame as you would any other GUI window using the mouse.
To minimize a frame, either minimize it in the usual way or press C-z. Table 4-1 summarizes the frame commands.
Table 4-1. Frame commands
Keystrokes | Command name | Action |
---|---|---|
C-x 5 o Buffers → Frames | other-frame | Move to other frame. |
C-x 5 0 File → Delete Frame | delete-frame | Delete current frame. |
C-x 5 2 File → New Frame | make-frame | Create a new frame on the current buffer. |
C-x 5 f | find-file-other-frame | Find file in a new frame. |
C-x 5 r | find-file-read-only-other-frame | Finds a file in a new frame, but it is read-only. |
C-x 5 b | switch-to-buffer-other-frame | Make frame and display other buffer in it. |
In this section, we'll learn about saving multiple buffers, renaming buffers, read-only buffers, and operations you can do with the buffer list—not only a useful tool but a good introduction to the principles you'll encounter in the directory editor, Dired, covered in Chapter 5.
You know about saving buffers individually by typing C-x C-s. Once you're using multiple buffers, you should also know that you can save them all at once by typing C-x s (for save-some-buffers). Emacs asks you if you want to save each buffer that is connected with a file (it doesn't offer to save new buffers you've created but have not associated with a file and, of course, it doesn't save its own internal buffers). For each buffer, you can answer y to save it or n not to. You can also type ! to save all the buffers without asking. If you want to save this buffer and no more, type a period (.). If you want to cancel the command and not save the current buffer, press q (of course, any buffers you saved before pressing q are already saved; q does not undo those). You may want to look at the buffer before deciding whether to save it; if so, type C-r. Emacs enters view mode, allowing you to look at the buffer but not make changes. Press q to exit view mode and continue saving buffers.
When you are editing a file, the buffer takes on the name of the file. If you have long filenames, you may find it convenient to rename buffers to shorter names (this renaming doesn't affect the filename, just the buffer name). This feature is mostly useful on versions of Emacs that don't offer good completion capabilities; in GNU Emacs, whenever you have to type a buffer name, you just type the first few unique letters and press Tab to have Emacs complete the name for you. In some circumstances, you may want to rename buffers.
To rename a buffer, type M-x rename-buffer. Emacs asks for the new name; type it and press Enter. The new name is displayed on the mode line. Renaming buffers comes in particularly handy in shell mode, described in Chapter 5. You start one command shell, and then rename the buffer and start another, in this way running as many shells as you have use for simultaneously.
As mentioned earlier, in GNU Emacs only the buffer name is displayed on the mode line, rather than the buffer name and the filename. Even if you rename a buffer that contains a file, Emacs remembers the connection between buffer and file, which you can see if you save the file (C-x C-s) or display the buffer list (described later in the chapter).
What if you have two buffers with the same name? Let's say you are editing a file called outline from your home directory and another file called outline from one of your subdirectories. Both buffers are called
outline
, but Emacs differentiates them by appending <2>
to the name of the second buffer. (You can tell which is which by looking at the buffer list, discussed later in this chapter.) Emacs offers an option that adds a directory to buffers in this situation: select Use Directory in Buffer Names from the Options menu. Let's say you've turned on this option and are editing a file called .localized; Emacs will call this buffer simply .localized
. Now you find a second file of the same name from a subdirectory. Instead of calling this buffer .localized<2>
, Emacs names the buffer directory/.localized
, making it easy for you to tell the buffers apart at a glance. This option has some limitations. It shows only the parent directory, not the full path, and it shows directory names only if multiple buffers have the same name. We wish it would go a bit further and provide the option of including the directory on the mode line for all buffers.
One word of advice: if you have a lot of buffers with names like
proposal
, proposal<2>
, and proposal<3>
around, you're probably forgetting to edit the directory when you ask for a file. If you try to find a file but get the directory wrong, Emacs assumes you want to start a new file. For example, let's say you want to edit the file ~/work/proposal, but instead ask for the file ~/novel/proposal. Since ~/novel/proposal doesn't exist, Emacs creates a new, empty buffer named proposal
. If you correct your mistake (C-x C-f ~/work/proposal), Emacs renames your buffers accordingly: your empty buffer proposal
is associated with ~/novel/proposal; the buffer you want is named proposal<2>
.
Here's a hint for dealing with the very common mistake of finding the wrong file. If you notice that you've found the wrong file with C-x C-f, use C-x C-v to replace it with the one you want. C-x C-v finds a file, but instead of making a new buffer, it replaces the file in the current buffer. It means "get me the file I really meant to find instead of this one." Using this command circumvents the problem of having unnecessary numbered buffers (i.e.,
proposal
, proposal<2>
, and so on) lying around.
While you're working, you may need to read some file that you don't want to change: you just want to browse through it and look at its contents. Of course, it is easy to touch the keyboard accidentally and make spurious modifications. We've discussed several ways to restore the original file, but it would be better to prevent this from happening at all. How?
You can make any buffer read-only by pressing C-x C-q. Try this on a practice buffer and you'll notice that two percent signs (
%%
) appear on the left side of the mode line, in the same place where asterisks (**
) appear if you've changed a buffer. The percent signs indicate that the buffer is read-only.[22] If you try to type in a read-only buffer, Emacs just beeps at you and displays an error message (Buffer is read-only
) in the minibuffer. What happens when you change your mind and want to start editing the read-only buffer again? Just type C-x C-q again. This command toggles the buffer's read-only status—that is, typing C-x C-q repeatedly makes the buffer alternate between read-only and read-write.
Of course, toggling read-only status doesn't change the permissions on a file. If you are editing a buffer containing someone else's file, C-x C-q does not change the read-only status. One way to edit someone else's file is to make a copy of your own using the write-file command, and then make changes. Let's say you want to change a proposal that is owned by someone else. Read the file, write the file as one you own using C-x C-w, then change it from read-only to writable status by pressing C-x C-q. None of this, of course, modifies the original file; it just gives you a copy to work with. If you want to move a minor amount of text from a read-only file to another, you can mark the text then press M-w to copy it. Move to the place you want to put the text and press C-y to paste it.
You can open a file as read-only in a new window by typing C-x 4 r or in a new frame by typing C-x 5 r. This is one of a number of commands in which 4 means window and 5 means frame.
Because you can create an unlimited number of buffers in an Emacs session, you can have so many buffers going that you can't remember them all. At any point, you can get a list of your buffers (yes, we know you know how to do that by holding down Ctrl and clicking the left mouse button, but this is a little different). This list provides you with important information—for example, whether you've changed the buffer since you last saved it.
If you press C-x C-b, Emacs lists your buffers. It creates a new
*Buffer List*
window on the screen, which shows you all the buffers.
Type: C-x C-b
Emacs displays a list of buffers.
You can use this list as an informational display ("these are my buffers") or you can actually work with buffers from this list, as covered in the next section.
Figure 4-3 shows what each of the symbols in the buffer list means.
Figure 4-3. Understanding the buffer list
The buffer list is more than a display. From the buffer list, you can display, delete, and save buffers. To move to the buffer list window, type C-x o. Emacs puts the cursor in the first column. For a particular buffer, press n or C-n to move down a line or p or C-p to move up a line. You can also press Space to move down to the next line and Del to move up. (The up and down arrow keys work, too.) This array of up and down choices may seem confusing, but multiple bindings are given to make it easy to move up and down without consulting a book like this one.
You use a set of one-character commands to work with the buffers that are listed. To delete a buffer, go to the line for the buffer you want to delete and type d or k. The letter
D
appears in the first column. You can mark as many buffers for deletion as you want to. The buffers aren't deleted immediately; when you're finished marking buffers, press x (which stands for "execute") to delete them. If any of the buffers you want to delete are connected with files, Emacs asks if you want to save the changes before doing anything. (Note that it does not ask you about buffers that aren't connected with files, so be sure to save any that you want before deleting them.)
If you change your mind about deleting a buffer before typing x, you can unmark the buffer by going to the appropriate line and typing u. As a convenience, the Del key also unmarks the previous buffer in the list. Why would you do this? Simple: d automatically moves you down one line. If you mark a file for deletion and immediately change your mind, you can press a single Del rather than moving to the previous line and typing u for unmark).
To save a buffer, go to the line for the buffer you want to save and press s. The letter
S
appears in the first column. Press x when you really want to save the buffer. Therefore, you can look at the buffer list, choose which buffers you want to delete and which you want to save, and then type x to do everything at once. Again, you can press u or Del to cancel saves if you change your mind.
One command that affects a buffer immediately when you type it is tilde (~). Typing ~ marks a buffer as unmodified. In effect, this symbol tells Emacs not to save changes automatically (since the buffer is unmodified, Emacs has no reason to save changes with its auto-save feature). Of course, if you have made changes, the changes are still in the buffer; it's just that you're in essence "lying" to Emacs to say that no changes have been made. Also, if you change the buffer again after marking it unmodified, Emacs once again knows it has been modified and saves it automatically in a backup file. The backup filename (not coincidentally) has the format filename~.
You can change a buffer's status from read-write to read-only and back again by pressing %. Pressing % changes the buffer's status immediately. Percentage signs appear on the mode line when a buffer is read-only. When you are editing, you can toggle a buffer between read-write and read-only by pressing C-x C-q, as we discussed earlier.
You can also use the buffer list to display multiple buffers in windows. To display one of the buffers in a full screen, move the cursor into the buffer list's window; use C-n and C-p to move to the line for the buffer that you want, and press 1 (the number one). Emacs displays the buffer in a full-screen window.
If you want to display one of the buffers in place of the buffer list, you can press f. To put a buffer in another window (i.e., one not occupied by the buffer list), type o. Emacs displays the buffer in the other window and puts the cursor there. Pressing C-o has a slightly different result; Emacs displays the buffer in another window but doesn't put the cursor there.
One final buffer display command remains. You can ask Emacs to display multiple buffers and have Emacs create windows for them dynamically. To select buffers to be displayed in windows, press m (for mark) next to the buffers you want. Emacs displays a > next to the buffers you mark with m. To tell Emacs to display the buffers you've marked, press v. Emacs makes horizontal windows to display the buffers you've chosen.
To get rid of the
*Buffer List*
window, type C-x 0 if you are in the buffer list window or C-x 1 (the number one) if you are in another window. Table 4-2 shows a summary of buffer manipulation commands.
Table 4-2. Buffer manipulation commands
Keystrokes | Command name | Action |
---|---|---|
C-x b Buffers → Select Named Buffer | switch-to-buffer | Move to the buffer specified. |
C-x → Buffers → Next Buffer | next-buffer | Move to the next buffer in the buffer list. |
C-x Buffers → Previous Buffer | previous-buffer | Move to the previous buffer in the buffer list. |
C-x C-b Buffers → List All Buffers | list-buffers | Display the buffer list. |
C-x k | kill-buffer | Delete the buffer specified. |
(none) | kill-some-buffers | Ask about deleting each buffer. |
(none) | rename-buffer | Change the buffer's name to the name specified. |
C-x s | save-some-buffers | Ask whether you want to save each modified buffer. |
Table 4-3 summarizes the commands for working with the buffer list.
Table 4-3. Buffer list commands
Keystrokes | Action | Occurs |
---|---|---|
C-n, Space, n, or | Move to the next buffer in the list (i.e., down one line). | Immediately |
C-p, p, or | Move to the previous buffer in the list (i.e., up one line). | Immediately |
d | Mark buffer for deletion. | When you press x |
k | Mark buffer for deletion. | When you press x |
s | Save buffer. | When you press x |
u | Unmark buffer. | Immediately |
x | Execute other one-letter commands on all marked buffers. | Immediately |
Del | Unmark the previous buffer in the list; if there is no mark, move up one line. | Immediately |
~ | Mark buffer as unmodified. | Immediately |
% | Toggle read-only status of buffer. | Immediately |
1 | Display buffer in a full screen. | Immediately |
2 | Display this buffer and the next one in horizontal windows. | Immediately |
f | Replace buffer list with this buffer. | Immediately |
o | Replace other window with this buffer. | Immediately |
m | Mark buffers to be displayed in windows. | When you press v |
v | Display buffers marked with m; Emacs makes as many windows as needed. | Immediately |
q | Quit buffer list. | Immediately |
Depending on your requirements, you may want to work with side-by-side windows in addition to or instead of horizontal windows. For finer control, you may want to know how to size windows (and because they're not GUI windows, you can't do that with the mouse).[23] You may also want to know how to compare files between windows, a good feature for basic file comparison.
To split the window vertically into two side-by-side windows, type C-x 3. You can execute this step repeatedly to create more side-by-side windows.
Type: C-x 3
Emacs creates two vertical windows.
When you create multiple vertical windows, Emacs usually doesn't have enough room to display a full line of text. Because vertical windows don't usually show full lines of text, a right arrow (on graphical implementations) or a dollar sign (on terminal-based implementations) at the end of a line tells you the line is continued.
To see the rest of the line, you need to know how to scroll text to the left and right. To push the text currently being displayed to the left (so you can see what's on the right), type C-x <. Left arrows or dollar signs are displayed on the left side of the window to indicate that there is more text to the left. To push the text being displayed to the right (so you can see what's on the left), type C-x >. You can use these commands whenever one of your lines is too wide, which can happen with or without windows.
How do you move between windows? As we mentioned earlier, C-x o moves you to the "next" window. But how does Emacs determine what that is?
The best way to express it is to say that Emacs moves through the windows in natural reading order, from left to right, then down, and again from left to right. In Figure 4-4, buffer names are numbered to show you how Emacs moves from one window to the next.
Figure 4-4. Moving between windows (Mac OS X)
Alternatively, you can simply select the window you want using the mouse.
Emacs always splits windows into two equal parts. Such a split is often good enough, but sometimes it's not, particularly if you become a window aficionado. When you have four or five or six windows on your screen at once, controlling each window's size becomes important. Otherwise, the windows you are most interested in will eventually become too small, and useful editing is almost impossible when you can see only five or six lines from a file. If you want to make the window you're working on taller, type C-x ^. Emacs lengthens the current window and makes the one below it smaller, accordingly. To make the current window wider, type C-x }. Emacs makes this window wider, at the expense of the one to the right of it.
To make windows smaller, you can shrink them. To shrink a window vertically, type M-x shrink-window. Emacs shrinks the current window by one line and the other windows on the screen grow accordingly. To shrink a window horizontally, type C-x {. This command makes the window one column narrower and enlarges the other windows on the screen horizontally.
Usually you want to work in larger increments than one line or one column at a time, however. When you type C-u preceding any of these commands, the command works in increments of four lines or columns at a time. For example, with two horizontal windows on the screen, let's use C-u C-x ^ to enlarge the
james
window.
Type: C-u C-x ^
Emacs makes the current window larger.
As you would expect, when you make the window larger, it automatically fills with more text from the buffer. There are shortcuts to sizing windows as well. If you have a very small buffer—for example, a one-line buffer containing the vocabulary-building word for the day and its definition—you can shrink the window to the size of the buffer by typing C-x - (for shrink-window-if-larger-than-buffer). If the buffer is larger than the window, this command does nothing. Typing C-x + (for balance-windows) creates windows of equal size again. (This latter command is also useful if you have an odd number of windows; C-x + divides the display equally among them.)
Windows in Emacs can be as big as your screen. There's a limit to how small windows can be, however, and this limit is specified by the variables window-min-height (whose default is four lines) and window-min-width (whose default is ten characters). If you enlarge other windows to the point that their counterparts become less than ten characters wide or four lines high, Emacs deletes the smaller windows. You can set these variables to other values if you want to; more information on setting variables is found in Chapter 10.
Especially if you're looking for minute differences between large files, the compare-windows command comes in handy. To use compare-windows, you must first have the buffers you want to compare in two windows, either side by side or horizontally. Go to the beginning of each buffer, then type M-x compare-windows. Emacs scrolls each buffer to the place where the discrepancy is. It places the point in each buffer at the place of the discrepancy, so using C-x o to move the cursor between buffers will show you exactly where the files differ.[24]
Of course, this maneuver finds only the first difference between the two buffers. Finding the second, third, and so on, is a bit tricky. The compare-windows command works only if the point in both buffers is in exactly the same place. Therefore, you need to move past the discrepancy in both buffers before you can type M-x compare-windows again. The Unix diff command provides a more comprehensive (although somewhat awkward looking) way to find the differences between two files. Emacs also provides an interface to Ediff, with options on the Compare menu (a submenu of the Tools menu). Ediff is far more comprehensive; see Chapter 12 for details.
Table 4-4 summarizes the window commands discussed in this chapter.
Table 4-4. Window commands
Keystrokes | Command name | Action |
---|---|---|
C-x 2 File → Split Window | split-window-vertically | Divide current window into two windows, one above the other. |
C-x 3 | split-window-horizontally | Divide current window into two side-by-side windows. |
C-x > | scroll-right | Scroll the window right. |
C-x < | scroll-left | Scroll the window left. |
C-x o | other-window | Move to the other window; if there are several, move to the next window (see "Navigating Windows"). |
C-x 0 | delete-window | Delete the current window. |
C-x 1 File → Unsplit Windows | delete-other-windows | Delete all windows but this one. |
(none) | delete-windows-on | Delete all windows on a given buffer. |
C-x ^ | enlarge-window | Make window taller. |
(none) | shrink-window | Make window shorter. |
C-x } | enlarge-window-horizontally | Make window wider. |
C-x { | shrink-window-horizontally | Make window narrower. |
C-x - | shrink-window-if-larger-than-buffer | Make window smaller if buffer is smaller than window. |
C-x + | balance-windows | Make windows the same size. |
C-M-v | scroll-other-window | Scroll other window. |
C-x 4 f | find-file-other-window | Find a file in the other window. |
C-x 4 b | switch-to-buffer-other-window | Select a buffer in the other window. |
(none)Tools → Compare (Ediff) → This Window and Next Window | compare-windows | Compare this window with the next window and show the first difference. |
Once you start working with multiple files, remembering just where you were in each one becomes harder. Bookmarks provide a convenient way of marking your place in a file, a place you can easily return to. You might, for example, be working with a file that has a long pathname. Rather than retype the pathname each time you start Emacs, you could just jump to a bookmark you've named current project by having Emacs find the file and put the cursor wherever you set the bookmark.
Bookmarks make the process of finding your place in any file easier. Particularly if you are working on a project several directories down from your home directory or in a totally different filesystem, putting bookmarks in the file makes it easy to get back there.
When you create a bookmark, Emacs creates a bookmark file in your home directory, called .emacs.bmk. It saves any new bookmarks in this file automatically when you exit Emacs.
Bookmarks are stored by user. If you and others access the same online documentation set, you can hold your place with your bookmark and they can hold their places with theirs, never interfering with each other's reading.
From the Edit menu, you can access the Bookmarks menu, which lists all the bookmark commands you'll probably ever need. We feel the menu interface for bookmarks is particularly well developed; even if you don't normally use menus, you might want to make an exception in this case. (At least until you learn the commands. Bookmarks are addictive, and when you use them frequently, the commands are easier to type than to reach by menu.)
To place a bookmark at the cursor position, type C-x r m (for bookmark-set). Emacs asks for a bookmark name, which can be virtually any length (practically speaking, as long as the width of your display) and can include spaces (so
current project
or Moore proposal's greatest flaw
or Othello Act 2 Scene 4
would all be fine). Emacs also puts a default bookmark in parentheses, suggesting the filename if you haven't used a bookmark during this session (in which case it uses the bookmark name). Either press Enter to accept the default or type a bookmark name and then press Enter. You now have a bookmark you can jump to at any time, in any Emacs session.
One subtlety: if you give a new bookmark the same name as an old one, Emacs assumes you just want to move the bookmark, even if it was formerly in another file. So remember to make bookmark names unique unless you are really trying to move them.
To move to a bookmark, press C-x r b (for bookmark-jump). Type the bookmark's name, or type the first few letters and press Tab. Emacs either finishes the bookmark's name or gives you a window of possible choices. Press Enter after the bookmark's name appears. Emacs retrieves the file and places the cursor at the bookmark location; the file is retrieved no matter how complicated its path is.
With menus, there's an easier way to move to a bookmark. When you select Edit → Bookmarks→ Jump to Bookmark, Emacs displays a window of available bookmarks. Select the bookmark you want, and Emacs displays the file with the cursor in the bookmark's position. This is useful if you have set many bookmarks, but we prefer to stick with the keyboard as much as possible.
You may find that you made the name of your bookmark too generic;
current project
may be too vague if you are juggling projects and the one in your hand is the current one. To rename a bookmark, type M-x bookmark-rename. If you do the renaming from the keyboard, Emacs prompts Old bookmark name:
and you type the old name and press Enter. (If you use the menus, you select the old name from a window instead.) Then Emacs asks, New name:
and you type the new name and press Enter, all very straightforwardly. Renaming a bookmark does just that and nothing else: it doesn't change the bookmark's location or its contents; it simply changes its name.
To delete a bookmark, press M-x bookmark-delete. Type the name of the bookmark to delete or select it with the mouse. Deleting a bookmark doesn't in any way affect the file that was marked.
This discussion brings up an interesting question. What happens if you delete text in a file in which you've put a bookmark? Because a bookmark points to a position in a file and not to a piece of text, the bookmark stays in the same place after the text is deleted, just as the cursor remains in the same place after you delete several paragraphs. This fact is more intuitive than it sounds. You don't delete bookmarks by deleting marked text. Let's say you have a file with four lines. You bookmark the third line, then later delete lines two through four. When you jump to that bookmark again, it appears after the first line, the end of the file.
Inserting text works the same way. Bookmarks point to a position in a file, not to text. If you insert a new line before the third line, the bookmark remains at the point in the file where you set it, in this case, the beginning of the new line. If you move text around, the bookmark points to the same location in the file, the line and column where you set it.
What happens if you delete a file that has a bookmark in it? If you delete the whole file or even rename it and then try to access a bookmark attached to the file, Emacs gives you the following error message:
filename nonexistent. Relocate "bookmark name"? (y or n)
If you press y, you can give a new path to the file, which works well if you really just renamed or moved the file but didn't delete it. If you press n, however, Emacs gives you a message, along with some advice:
Bookmark not relocated, consider removing it
In other words, Emacs argues that no one needs bookmarks to nonexistent files, and we're inclined to agree.
Remember the buffer list we discussed earlier in this chapter? Bookmarks have a similar list with one-letter commands that allow you to work with all your bookmarks at once.
To work with a list of bookmarks, type C-x r l (the lowercase letter "L"). The
*Bookmark List*
buffer appears.
Type: C-x r l
Emacs displays a list of bookmarks and the path to the associated files.
If you press Enter, f, or j, Emacs displays the bookmarked file with the cursor in the bookmarked location. From the bookmark list, press d to mark bookmarks for deletion, then x to delete them (unlike in the buffer list, in the bookmark list, deleting is the only reason you need the x command). If you change your mind, press Del to remove the d before you press x. Pressing r renames a bookmark, and Emacs prompts you for the new name. To save all the bookmarks, press s. You can mark several bookmarks and then display their associated files by typing m next to the bookmarks. A > appears beside bookmarks you've marked. When you've marked all you want, type v (for view) and Emacs pulls up the files associated with the bookmarks and displays them in multiple windows (with the cursor at the bookmarked location, of course). If you just want to move to one bookmarked file, you can press v without marking the bookmark first.
You can change the display of the bookmark list slightly by pressing t. By default, the list shows a bookmark's name, followed by the complete path to the file with which it is associated. If you press t (for toggle), only the bookmark names appear.
Table 4-5 summarizes the bookmark list commands. It includes a few commands relating to annotations; we'll cover these in the next section.
Table 4-5. Commands for editing the bookmark list
Command | Action |
---|---|
Enter, f, or j | Go to the bookmark on the current line. |
C-o or o | Open the bookmark on the current line in another window; o moves the cursor to that window; C-o keeps the cursor in the current window. |
d, C-d, or k | Flag bookmark for deletion. |
r | Rename bookmark. |
s | Save all bookmarks listed. |
m | Mark bookmarks to be displayed in multiple windows. |
v | Display marked bookmarks or the one the cursor is on if none are marked. |
t | Toggle display of paths to files associated with bookmarks. |
w | In the minibuffer, display location of file associated with bookmark. |
x | Delete bookmarks flagged for deletion. |
u | Remove mark from bookmark. |
Del | Remove mark from bookmark on previous line or move to the previous line (if there is no mark). |
q | Exit bookmark list. |
Space or n | Move down a line. |
p | Move up a line. |
l | Load a bookmark file (other than the default). |
A | Display all annotations. |
a | Display annotation for current bookmark. |
e | Edit (or create) annotation for the current bookmark. |
You can add annotations to your bookmarks. These annotations can provide any type of information you want: details about the file in question, what you are doing with it, documentation for someone else on your project to review when looking at your files, or really anything you want.
Annotations are most easily added from the bookmark list itself. Open the bookmark list using C-x r l, then move to the line of the bookmark you want to annotate. Type e, the command to edit an annotation.
From the bookmark list, type: e
Emacs opens a
*Bookmark Annotation Compose*
window.
Emacs provides some guidance in this buffer about what to do. It says that all lines that start with a comment mark (#) will be deleted and that you press C-c C-c to save and exit the annotations buffer.
The annotation includes lines that are commented out and won't become part of the annotation, but if you'd like to keep the Author and Date lines (logical portions of an annotation), just uncomment those lines by deleting the initial #. You then add any annotation you would like and press C-c C-c to exit the window.
Annotations exhibit a couple of behaviors that are at least annoying if not bugs. First, Emacs defines a # as the default fill prefix. You must either change that (see Chapter 6 for details) or delete the initial # if Emacs inserts it. Second, and more critically, Emacs doesn't automatically save annotations when you exit Emacs. If you set a bookmark, Emacs saves the bookmarks file automatically (and in fact without asking). If you set an annotation but do not add or move a bookmark during the session, you must save the bookmarks file manually by typing M-x bookmark-save.
After you add an annotation, Emacs puts an asterisk (*) before the bookmark name as a visual indication that the bookmark has been annotated. To display an annotation for the current bookmark, press a. To display all annotations, press A.
When you jump to a bookmark or move to a bookmarked file from the bookmark list, annotations are automatically displayed in another window (but don't edit them in this window; you must use the procedure described earlier). If you open the bookmarked file some other way (using C-x C-f, for example), annotations are not displayed.
In addition to those we've discussed, there are a few more esoteric bookmark commands. These include bookmark-insert, which inserts the text of the bookmarked file at the cursor position; bookmark-write, which prompts for a new filename in which to save bookmarks; and bookmark-load, to load these separate bookmark files. These commands are less useful than the others, but you may think of some clever uses we have not.
Table 4-6 summarizes bookmark commands.
Table 4-6. Bookmark commands
Keystrokes | Command name | Action |
---|---|---|
C-x r m Edit → Bookmarks → Set Bookmark | bookmark-set | Set a bookmark at the current cursor position. |
C-x r b Edit → Bookmarks → Jump to Bookmark | bookmark-jump | Jump to a bookmark. |
(none)Edit → Bookmarks → Rename Bookmark | bookmark-rename | Rename a bookmark. |
(none)Edit → Bookmarks → Delete Bookmark | bookmark-delete | Delete a bookmark. |
(none)Edit → Bookmarks → Save Bookmarks | bookmark-save | Save all bookmarks in default file. |
C-x r l Edit → Bookmarks → Edit Bookmark List | bookmark-menu-list | Move to buffer. |
(none)Edit → Bookmarks → Insert Contents | bookmark-insert | Insert full text of file associated with a given bookmark. |
(none)Edit → Bookmarks → Save Bookmarks As | bookmark-write | Save all bookmarks in a specified file. |
(none)Edit → Bookmarks → Load a Bookmark File | bookmark-load | Load bookmarks from specified file. |
(none)Edit → Bookmarks → Insert Location | bookmark-insert-location | Insert the path to a given bookmark at the cursor position. |
Now that you know how to work with multiple buffers, frames, and windows, why not read the next chapter to discover some of the things you can do with them? Some, like using the directory editor and working with the command line from within Emacs, have been alluded to in this chapter.