: Learning GNU Emacs, 3rd Edition

4.2.2 Deleting Buffers

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!

: 0.324. /Cache: 3 / 0