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.
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!
Get Learning GNU Emacs, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.