I use this:
(defun kill-other-buffers ()
"kill all other buffers."
(interactive)
(mapc 'kill-buffer (delq (current-buffer) (buffer-list))))
I need to add one more buffer
Per the comments, you can simply delq
the "one more buffer" you wish to exclude. You would be deleting it from the list which was returned by the initial delq
form:
(delq (get-buffer "init.el")
(delq (current-buffer)
(buffer-list)))
You can, of course, generalise this to act on some arbitrary list of exclusions:
(require 'cl-lib)
(let ((exclusions (list (current-buffer) (get-buffer "init.el"))))
(cl-reduce #'delq exclusions :from-end t :initial-value (buffer-list)))
Or you might find this approach more-readable:
(require 'cl-lib)
(let ((exclusions (list (current-buffer) (get-buffer "init.el"))))
(cl-loop for b being the buffers
unless (memq b exclusions)
collect b))
All three forms should return the same list.
Another approach (depending on your needs) is to use the kill-buffer-query-functions
hook to prevent specific buffers from being killed regardless of how kill-buffer
is invoked.
I use my own small library to prevent certain buffers from being killed but there are various other examples out there. With this approach you can keep your kill-other-buffers command simple and manage your list of protected buffers separately.
delq
to delete it also. Or do you mean the value of(other-buffer (current-buffer))
? If so, usedel
on its return value. SeeC-h f other-buffer
.(get-buffer BUFFER-OR-NAME)
to obtain the value to be deleted from the list.