Sams Teach Yourself Emacs in 24 Hours
Hour 6: Editing Several Files at Once
Up to now, you have seen how you can use Emacs to edit files, but only one at a time. You might, however, often want to edit several files at a time. For example, consider this situation. You are editing your current project which contain several C files. While you edit them, you need to see a definition in another file. Suddenly your boss comes in and demands that you write a given letter at once. (You had promised him that you'd have it in two weeks.) While you edit the letter, the telephone rings. It is your girlfriend, who wants you to add a few items to your shopping list (which you, of course, have on your computer).
Do you get my drift? The letter for your boss and the shopping list can, of course, be edited by a separate Emacs, which you start for only this purpose. But it is not realistic that you would start a separate Emacs for each file in your programming project.
Fortunately, Emacs supports editing in multiple buffers at the same time.
When you open a new file with C-x C-f (find-file) a new buffer is created with the content of the file and the buffer is shown to you in the current window. The old buffer is not destroyed by this action; it is merely hidden. You have many ways to get back to the other buffer. The most basic is to press C-x b (switch-to-buffer). Emacs then asks you for the name for the buffer to switch to. (In many cases this is the same name as the file; later you will see other cases.)
This task shows you the most basic way to get to another buffer.
1. Press C-x b (switch-to-buffer). Emacs asks you for the name of the buffer to switch to (see Figure 6.1). As the default it suggests the buffer that you last visited (that is, the buffer before your current one). If you want to switch to this buffer, simply press Enter.
2. Now you can start to type the name of the buffer that you want to switch to. To make Emacs complete as much of the name as possible for you, press the Tab key. If it can't unambiguously fill in any letters for you, it lists the possible completions, as you can see in Figure 6.2.
3. When you have finished typing the name, press Enter to tell Emacs to open this file for you.
Caution - If you type the name of a nonexistent buffer, Emacs creates a new empty buffer. This buffer is not associated with any file, and thus no auto-saving is done! In most cases, you should kill this buffer right away and switch to the one you intended to move to in the first place.
When you finish with a buffer, you can kill it by pressing C-x k (kill-buffer). Emacs asks you for the name of the buffer you want to kill, in the same way it asks for a name when you switch buffers. If the buffer has a file loaded into it, Emacs forgets all about the file, too. If the file has changed since it was last saved to disk, Emacs suggests that you save it before you kill it, as you can see in Figure 6.3.
Note - Note that this is one of the answers where you must type yes or no (that is, spelled out) and subsequently press the Enter key. This is to avoid accidentally killing a buffer that has been modified.
Emacs has a special interface for managing buffers. This interface itself is simply a special buffer with special keybindings. You'll see these buffers a lot!
To get to this buffer, press C-x C-b (list-buffers). This splits your current window in two, as you can see in Figure 6.4.
From this buffer you can select, delete, and save files and much more. Each of the functions are bound to a single key. You are not allowed to insert text into this buffer, therefore it's okay to use the letter f for something else, for example. When pressing a key, the operation operates on the line in which point is located. The most important bindings in this buffer include
1--Pressing 1 makes the buffer on the current line the only visible one in the given frame.
o or C-o--By pressing the letter o or C-o, the buffer is selected in another window. The difference between the two is that o moves the cursor to the other window, whereas C-o doesn't.
In both cases the buffer menu is still visible. In Figure 6.5, C-o has been pressed.
d, C-d, and x--Pressing d marks the buffer on the current line for deletion, and moves down to the next line. Likewise C-d marks for deletion but moves to the previous line. The buffer is not deleted right away. To do the actual deletion, you must press x (for execute). The reason for this two-stage deletion is that you can execute a macro, for example, that marks the buffers. When the macro has been executed, you can verify the list before you waste the buffers. If any of the buffers are modified, you are asked whether you want to save them before you delete them.
In the first column the letter D indicates that the given buffer is marked for deletion, whereas the letter S in the second column indicates that it has been marked for saving (see Figure 6.6).
When a buffer is loaded from a file it gets the same name as the file's name excluding the directory path. This makes it easy to find a given buffer, if you know which file it contains. This is most definitely the most intuitive name for the buffer. There is one exception, namely if a buffer exists with the same name. This is possible if two files with the same name from different directories are loaded into Emacs. In this case a number is appended to the end of the filename. Thus if Emacs already contains a buffer with the name index.html, and you load another file with this name into Emacs, the second buffer is called index.html<2>. The third buffer is called index.hmtl<3>, and so on. The files that the buffers were loaded from are, of course, not renamed.
Caution - If you have two buffers in Emacs called index.html and index.html<2>, the second is not renamed to index.html when the first is killed. That is, when a buffer has been named, it will not be renamed.
Besides the buffers which you create, Emacs also creates and manages some itself. Up to now, you have seen a few of these, namely the message buffer, containing old messages from the minibuffer, and the buffer menu buffer described in the previous section. To make it easier for you to distinguish these buffers from those you create yourself, the names for these buffers have a star at the beginning and the end. Examples of these buffer names include *Messages*, *Buffer List*, and *Help*.
In Hour 3, "Getting Started with Emacs," you saw that the buffer list was available from the Buffers entry in the menu bar. In Figures 6.7 and 6.8 you can see what it looks like.
There is a bit of difference between GNU Emacs and XEmacs at this point. The main difference is that the files are sorted by category in XEmacs (that is, HTML files in one group and Lisp files in another), which is not the case in GNU Emacs.
Tip - Using the msb library, GNU Emacs can be enhanced to have categorized entries in the Buffers submenu. It is, however, beyond the scope of this book to describe this library, because it requires some knowledge of Lisp programming.
If you want to go to a buffer, and you can't remember its name very well, there are hints in the following two subsections. Both tell you about a different package that makes it simple to get to a buffer using the keyboard. If, on the other hand, you think that pressing C-x b.and using the Tab key to find the correct buffer is enough for you, you can skip the rest of this section. But please come back here when you have begun to use Emacs in such a way that you often have more than 15-20 buffers alive at the same time. (No, that is not unusual at all!)
When you press C-x b.Emacs suggests the name of the previous buffer you visited. This is especially useful if you switch between two buffers. Initially you open the first buffer and then the next one, but from then on you can simply press C-x b RET .to go to the other one (given, of course, that you do not switch to another buffer in between).
This approach, however, does not work if you edit three files that you have to frequently switch between. An alternative is to use the library yic-buffer, which lets you travel through the list of buffers without naming any files. Simply think of all the buffers as keys in a key ring. With the functions from yic-buffer you have the capability to go to the next or previous buffer in this ring.
To install this library, copy the file yic-buffer.el from the CD to your Lisp directory, and insert the following into your .emacs file:
By default this library binds C-x C-p.to go to the previous buffer , whereas C-x C-n.is bound to go to the next buffer . Thus with these two functions you can cycle through all the buffers. Why C-x C-p? Well, C-p goes to the previous line. (You saw that in Hour 2, "Using Emacs in Microsoft Windows," where I mentioned that arrow-up was enough in most cases.) Therefore it was logical for the author to bind it to C-x C-p. If you would rather bind it to Ctrl-Page-Up and Ctrl-Page-Down, insert the following into your .emacs file:
(global-set-key [(control prior)] 'bury-buffer) (global-set-key [(control next)] 'yic-next-buffer)
As a shortcut, it also binds C-x C-o. to the functionality of pressing C-x o RET (described previously) and C-x C-k.to the functionality of pressing C-x k RET (that is, kill the current buffer without asking--it of course still asks whether it is modified).
When switching buffers with C-x b, you can press the Tab key to make Emacs fill in as much as possible unambiguously. If no additional characters can be filled in unambiguously, Emacs shows you the possible completions in a buffer. In some cases, you might either find it annoying that this extra buffer suddenly is shown, or find it irritating that the completions aren't shown all the time. If this is the case, you should most definitely try the library called iswitchb.
To load it, insert the following lines into your .emacs file:
(require 'iswitchb) (iswitchb-default-keybindings)
When it is loaded, the key sequence C-x b is bound to the function iswitchb-buffer. Therefore when you press C-x b, Emacs shows you a list of possible completions as you can see in Figure 6.9.
At first, all possible completions are shown, because you have not typed anything yet. When you type something, the list is immediately updated with the limited set of buffer names that match the text you have typed (see Figure 6.10). Please note that a buffer name matches if it contains the text as a substring. Thus if you type fig, the buffer name config.el matches, because it contains fig as a substring.
If you press the Enter key when there still is more than one element left to complete, Emacs switches to the buffer listed in the front of the list.
Pressing C-s rotates the list one element to the left. That is, the second element becomes the first one, the third becomes the second, and the first wraps over to become the last one. Likewise C-r rotates the list one element to the right. This can be seen in Figure 6.11, where C-s has been pressed.
When you leave work or school, or anywhere you use Emacs, you might need to turn off the computer (or you might do this to save energy). Turning off the computer does, of course, also mean shutting down Emacs. If you are working on a set of files (for example, a lot of C files in a project or the HTML documents for your home page), it might be desirable to be able to make a dump of Emacs. This dump should contain enough information to make Emacs configure itself to the state it had before you exited it, so you later may get back to it and continue your work. The library called desktop does in fact do this.
This library makes Emacs save the list of buffers and some information about these buffers to a file. Each directory can contain one such file (its name is .emacs.desktop). When Emacs is started it checks whether such a file is located in the startup directory and, if this is the case, the state is read from this file.
To make it work, insert the following lines into your .emacs file:
(load "desktop") (desktop-load-default) (desktop-read)
When a desktop has been loaded, it is saved to the same file on exit, but the first time you need to tell it to save. To do this, press M-x and type desktop-save.
Note - The window layout for the buffer is not saved to a file.
Sams Teach Yourself Emacs in 24 Hours
Hour 6: Editing Several Files at Once
© Copyright Macmillan USA. All rights reserved.