Книга: Learning GNU Emacs, 3rd Edition

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

Оглавление книги


Генерация: 1.253. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз