2

I found a function, that will kill all buffers except the current buffer that's open.

(defun kill-other-buffers () 
  "Kill all buffers but the current one.
Doesn't mess with special buffers."
  (interactive)
  (when (y-or-n-p "Are you sure you want to kill all buffers but the current one? ")
    (seq-each
     #'kill-buffer
     (delete (current-buffer) (seq-filter #'buffer-file-name (buffer-list))))))

It works great. However, when I have two windows open with a different buffer in each window, I noticed it kills the visible buffer in the other window too, except for the buffer where the cursor currently in is.

I studied the manuals of Emacs, in order to detect the open buffers that are visible in all windows. But I found not one. So I'm wondering if anyone have a good suggestion for this?

3

You can iterate over the window-list of each frame in frame-list.

Rather than:

(delete (current-buffer) 
        (seq-filter #'buffer-file-name (buffer-list)))

You could use:

(let (buflist)
  (dolist (frame (frame-list))
    (dolist (window (window-list frame t))
      (push (window-buffer window) buflist)))
  (seq-filter (lambda (buf)
                (and (buffer-file-name buf)
                     (not (memq buf buflist))))
              (buffer-list)))

You could simplify that slightly using walk-windows which accepts an ALL-FRAMES parameter:

(let (buflist)
  (walk-windows (lambda (window)
                  (push (window-buffer window) buflist))
                t t)
  (seq-filter (lambda (buf)
                (and (buffer-file-name buf)
                     (not (memq buf buflist))))
              (buffer-list)))
  • I might be mistaken, but when I add your first suggestion and then the second suggestion. They also kill the visible buffers in the other windows. So I wondered if you tried the function kill-other-buffers itself with the buflist modification? – ReneFroger May 4 '17 at 13:33
  • Each of my suggestions produces the list of buffers to kill (if substituted into your original code). You can trivially test each suggestion with M-: and confirm whether the resulting list includes any of the visible buffers. I've just tested your kill-other-buffers function with one of my alternatives, and for me it's working as I expected. – phils May 5 '17 at 12:41
  • You're right, I didn't read your message thoroughly. I evalated your modification for buffer outside the function kill-other-buffers instead inside the function, replacing the (delete [..] ). It works greatly, thanks for that. – ReneFroger May 9 '17 at 10:19
  • One question, I wondered how I could kill with the same function special buffers like Buffer list as well? I noticed they don't get killed (Buffer list, Magit status window, helm, etc). – ReneFroger May 10 '17 at 8:00
  • n.b. That behaviour is based on your original code, which filters the list of candidates to only those which are visiting a file -- for which (buffer-file-name BUFFER) returns non-nil. – phils May 10 '17 at 9:21

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.