I looked around to be sure, that none had this issue earlier.

Okay, sometimes I'm having too many buffers to manage. So I would like to kill all these buffers, except the buffers that are currently opened in my windows and elscreen.

I suspect there should be an existing function for that, like in Vim but I couldn't found it inside Emacs and wiki pages.

Any suggestion would be greatly appreciated.

  • 2
    If you are using ibuffer (this is the default buffer manager since Emacs 24.3 (I think?), then C-x C-b will bring up the buffer list, where you can mark the current buffer (usually the one on the top of the list) by pressing m, then revert the selection by pressing t, then kill all selected buffers by pressing D (these are the same keybindings you'd use in Dired).
    – wvxvw
    May 20 '15 at 15:14
  • 1
    What @wvxvw said, plus if you use projectile too, there is ibuffer-projectile package you can install. That will allow you to kill all buffers specific to a projectile project in one fell swoop (m on project name, D) May 20 '15 at 15:44
  • @wvxvw ibuffer is not the default. But you can always invoke ibuffer-list-buffers manually, no matter what you use.
    – Malabarba
    May 20 '15 at 18:11
  • @Malabarba ah, right, now I see that that's some modification that I've made and have forgotten, right then.
    – wvxvw
    May 20 '15 at 18:46

I don't really think you want (ought) to do it, but this does it:

(defun foo ()
  "Kill all buffers not currently shown in a window somewhere."
  (dolist (buf  (buffer-list))
    (unless (get-buffer-window buf 'visible) (kill-buffer buf))))

Function buffer-list gives you all buffers. Function get-buffer-window checks whether a given buffer is shown in a window. Argument visible to get-buffer-window means check all windows on visible frames (ignore invisible and iconified frames).

  • Thanks for sharing. It works for a part, but not fully. I had a buffer open in the tab of elscreen, but it got deleted too. But I upvoted it anyway, even when it works only for windows.
    – ReneFroger
    May 20 '15 at 19:34
  • Dunno what that means. If you are saying that (get-buffer-window BUF 'visible) returns nil for a buffer that you think is visible, then consider filing an Emacs bug report: M-x report-emacs-bug. I also don't know what you mean by "even when it works only for windows." But again, if you think that something doesn't work in all cases where you think it should, M-x report-emacs-bug.
    – Drew
    May 20 '15 at 20:39
  • @Drew : obviously elscreen is something that has buffers "open/handled but not visible" in some way. If there's a (elscreen-manages-this-buffer buf) kind of function, the OP should add it to the conditions.
    – YoungFrog
    May 21 '15 at 10:21
  • @YoungFrog: Dunno what is obvious. But if a buffer is visible in the ordinary sense, i.e., displayed in a visible window, then get-buffer-window should DTRT wrt that buffer. If it does not, that sounds like a bug, to me.
    – Drew
    May 21 '15 at 13:43
  • @Drew: From the initial question and the comments so far, it seemed obvious to me that these buffers are not visible in the ordinary sense.
    – YoungFrog
    May 21 '15 at 13:49

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.