1

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

2
  • The question isn't very clear. What other buffer? If you know what it is then just use delq to delete it also. Or do you mean the value of (other-buffer (current-buffer))? If so, use del on its return value. See C-h f other-buffer.
    – Drew
    May 23 at 21:02
  • If you want to exclude a buffer with a known name then you'll probably be looking for (get-buffer BUFFER-OR-NAME) to obtain the value to be deleted from the list.
    – phils
    May 24 at 1:02
1

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.

1
  • Thanks! Is just what I needed
    – mcardozo
    May 24 at 19:26
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.