Выбрать главу

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.)

4.2.2 Deleting Buffers

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!

4.3 Working with Windows

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.

4.3.1 Creating Horizontal 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.

4.3.2 Moving Between Windows

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.)