4.5. More About Buffers


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.

4.5.1 Saving Multiple Buffers

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.

4.5.2 Renaming 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.

4.5.3 Read-Only Buffers

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.[1] 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.

[1] The exception to the rule that ** means changed and %% means read-only is the *scratch* buffer. Because Emacs doesn't warn you if you kill the *scratch* buffer, even if it is changed, it wants to give you some indication that there are unsaved changes. Instead of %%, the *scratch* buffer puts %* on the mode line.

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.

4.5.4 Getting a List of Buffers

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


4.5.5 Working with 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 bBuffers

switch-to-buffer

Move to the buffer specified.

C-x 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-bBuffers

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




Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net