20.4 Killing Buffers

If you continue an Emacs session for a while, you may accumulate a large number of buffers. You may then find it convenient to kill the buffers you no longer need. (Some other editors call this operation close, and talk about “closing the buffer” or “closing the file” visited in the buffer.) On most operating systems, killing a buffer releases the memory Emacs used for the buffer back to the operating system so that other programs can use it. Here are some commands for killing buffers:

C-x k buffer RET

Kill buffer buffer (kill-buffer).

M-x kill-some-buffers

Offer to kill each buffer, one by one.

M-x kill-matching-buffers

Offer to kill all buffers matching a regular expression.

C-x k (kill-buffer) kills one buffer, whose name you specify in the minibuffer. The default, used if you type just RET in the minibuffer, is to kill the current buffer. If you kill the current buffer, another buffer becomes current: one that was current in the recent past but is not displayed in any window now. If you ask to kill a file-visiting buffer that is modified, then you must confirm with yes before the buffer is killed.

The command M-x kill-some-buffers asks about each buffer, one by one. An answer of yes means to kill the buffer, just like kill-buffer. This command ignores buffers whose names begin with a space, which are used internally by Emacs.

The command M-x kill-matching-buffers prompts for a regular expression and kills all buffers whose names match that expression. See Syntax of Regular Expressions. Like kill-some-buffers, it asks for confirmation before each kill. This command normally ignores buffers whose names begin with a space, which are used internally by Emacs. To kill internal buffers as well, call kill-matching-buffers with a prefix argument.

The Buffer Menu feature is also convenient for killing various buffers. See Operating on Several Buffers.

If you want to do something special every time a buffer is killed, you can add hook functions to the hook kill-buffer-hook (see Hooks).

If you run one Emacs session for a period of days, as many people do, it can fill up with buffers that you used several days ago. The command M-x clean-buffer-list is a convenient way to purge them; it kills all the unmodified buffers that you have not used for a long time. An ordinary buffer is killed if it has not been displayed for three days; however, you can specify certain buffers that should never be killed automatically, and others that should be killed if they have been unused for a mere hour. These defaults, and other aspects of this command’s behavior, can be controlled by customizing several options described in the doc string of clean-buffer-list.

You can also have this buffer purging done for you, once a day, by enabling Midnight mode. Midnight mode operates each day at midnight; at that time, it runs clean-buffer-list, or whichever functions you have placed in the normal hook midnight-hook (see Hooks). To enable Midnight mode, use the Customization buffer to set the variable midnight-mode to t. See Easy Customization Interface.