Killing Buffers
Killing a buffer makes its name unknown to Emacs and makes the memory space it occupied available for other use.
The buffer object for the buffer that has been killed remains in existence as long as anything refers to it, but it is specially marked so that you cannot make it current or display it. Killed buffers retain their identity, however; if you kill two distinct buffers, they remain distinct according to eq although both are dead.
If you kill a buffer that is current or displayed in a window, Emacs automatically selects or displays some other buffer instead. This means that killing a buffer can change the current buffer. Therefore, when you kill a buffer, you should also take the precautions associated with changing the current buffer (unless you happen to know that the buffer being killed isn’t current). See Current Buffer.
If you kill a buffer that is the base buffer of one or more indirect buffers (see Indirect Buffers), the indirect buffers are automatically killed as well.
The buffer-name of a buffer is nil if, and only if, the buffer is killed. A buffer that has not been killed is called a live buffer. To test whether a buffer is live or killed, use the function buffer-live-p (see below).
- Command: kill-buffer &optional buffer-or-name
-
This function kills the buffer buffer-or-name, freeing all its memory for other uses or to be returned to the operating system. If buffer-or-name is
nilor omitted, it kills the current buffer.Any processes that have this buffer as the
process-bufferare sent theSIGHUP(hangup) signal, which normally causes them to terminate. See Signals to Processes.If the buffer is visiting a file and contains unsaved changes,
kill-bufferasks the user to confirm before the buffer is killed. It does this even if not called interactively. To prevent the request for confirmation, clear the modified flag before callingkill-buffer. See Buffer Modification.This function calls
replace-buffer-in-windowsfor cleaning up all windows currently displaying the buffer to be killed.Killing a buffer that is already dead has no effect.
This function returns
tif it actually killed the buffer. It returnsnilif the user refuses to confirm or if buffer-or-name was already dead.(kill-buffer "foo.unchanged") ⇒ t (kill-buffer "foo.changed") ---------- Buffer: Minibuffer ---------- Buffer foo.changed modified; kill anyway? (yes or no) yes ---------- Buffer: Minibuffer ---------- ⇒ t
- Variable: kill-buffer-query-functions
Before confirming unsaved changes,
kill-buffercalls the functions in the listkill-buffer-query-functions, in order of appearance, with no arguments. The buffer being killed is the current buffer when they are called. The idea of this feature is that these functions will ask for confirmation from the user. If any of them returnsnil,kill-bufferspares the buffer’s life.
- Variable: kill-buffer-hook
This is a normal hook run by
kill-bufferafter asking all the questions it is going to ask, just before actually killing the buffer. The buffer to be killed is current when the hook functions run. See Hooks. This variable is a permanent local, so its local binding is not cleared by changing major modes.
- User Option: buffer-offer-save
This variable, if non-
nilin a particular buffer, tellssave-buffers-kill-emacsto offer to save that buffer, just as it offers to save file-visiting buffers. Ifsave-some-buffersis called with the second optional argument set tot, it will also offer to save the buffer. Lastly, if this variable is set to the symbolalways, bothsave-buffers-kill-emacsandsave-some-bufferswill always offer to save. See Definition of save-some-buffers. The variablebuffer-offer-saveautomatically becomes buffer-local when set for any reason. See Buffer-Local Variables.
- Variable: buffer-save-without-query
This variable, if non-
nilin a particular buffer, tellssave-buffers-kill-emacsandsave-some-buffersto save this buffer (if it’s modified) without asking the user. The variable automatically becomes buffer-local when set for any reason.
- Function: buffer-live-p object
This function returns
tif object is a live buffer (a buffer which has not been killed),nilotherwise.
Copyright © 1990-1996, 1998-2021 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Killing-Buffers.html