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
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
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 (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.