22

Sometimes my buffer list has 10+ dired windows, and I end up using buffer-menu to manually mark and kill all of them. Is there a faster way to close all open dired windows?

  • I have accepted the answer that most directly answers the question. However, I have since started applying an ounce of prevention instead: emacs.stackexchange.com/a/1041/93 – nispio Oct 14 '14 at 16:28
32

You could do the following:

M-x ibuffer

* / to mark buffers in dired mode.

D to delete them.

  • 1
    I had never heard of ibuffer before. I will definitely be mapping C-x C-b to ibuffer. – nispio Sep 24 '14 at 19:28
  • You can also press t (dired-toggle-marks) to mark all (if none was previously marked). – caisah Sep 25 '14 at 4:25
  • @caisah dired-toggle-marks is the name of the dired command. Pressing t will also toggle all marks in ibuffer, but the non-dired buffers usually outnumber the dired buffers, so toggling doesn't often make sense in this context. – nispio Oct 7 '14 at 13:29
  • @nispio You are right. I don't know why, but I was thinking all buffers were dired... – caisah Oct 7 '14 at 15:15
  • 1
    Note: this does not work correctly when there are no dired buffers, because the D will not apply to the dired buffers, but to the first buffer in the list. – daniel kullmann Oct 20 '16 at 13:28
10

Instead of add another function to kill all dired buffers, I suggest you take advantage of filter groups in ibuffer, it allow you to group buffer by many condition.

Here is a example to set filter groups:

(setq-default ibuffer-saved-filter-groups
              `(("Default"
                 ;; I create a group call Dired, which contains all buffer in dired-mode
                 ("Dired" (mode . dired-mode))
                 ("Temporary" (name . "\*.*\*"))
                 )))

Then C-x C-b open ibuffer window, you can see something like this:

example1

Move cursor to [ Dired ] (with M-p, M-n), then press d to mark as delete

example2

then press x to execute.

I prefer this way because I don't need to think about how I should bind my key to the new function, and my buffers are better organized.

  • Filter groups did not display for me when I first launched ibuffer after evaluating your code. I had to hit / R (bound to ibuffer-switch-to-saved-filter-groups) to get the groups. – itsjeyd Oct 7 '14 at 9:17
  • (ibuffer-switch-to-saved-filter-groups "Default") You can add this line of code into ibuffer hook, filter groups name should be changed if it is not Default – Rangi Lin Oct 7 '14 at 9:40
  • 1
    Yes, that's what I ended up doing. Didn't know about filter groups before reading your answer, thanks for mentioning them ;) – itsjeyd Oct 7 '14 at 10:17
10

Do you have many Dired buffers because you wanted them all at one point? IOW, did you want to create each of them as separate buffers?

If not, the solution is simple: tell Dired to reuse an existing Dired buffer when you hit RET a directory name to open it in Dired.

Here is how to do that:

  1. Load library Dired+ (dired+.el):

    (require 'dired+)
    
  2. Tell Dired to reuse Dired buffers:

    (diredp-toggle-find-file-reuse-dir 1)
    

The effect is that when you hit RET (or click the mouse) on a directory in Dired, find-alternate-file is used, so the original Dired buffer is replaced (deleted) by the new one.

You can toggle this behavior anytime, using C-M-R (aka C-M-S-r) in Dired.

  • This is the solution I have settled on now. I never wanted 20 open Dired buffers in the first place. Thanks for suggesting it (and creating it.) – nispio Oct 10 '14 at 18:44
  • The links are dead, getting a 404 : ( – Zelphir Kaltstahl Oct 9 '18 at 8:36
  • 1
    @Zelphir: Thanks for the heads-up. Should be OK now. (Emacs Wiki changed its URLs generally, so old posts like this one got broken links.) – Drew Oct 9 '18 at 14:43
8

Something like this (scroll a bit)?

(defun kill-dired-buffers ()
     (interactive)
     (mapc (lambda (buffer) 
           (when (eq 'dired-mode (buffer-local-value 'major-mode buffer)) 
             (kill-buffer buffer))) 
         (buffer-list)))
5

You can also use helm-mini:

  • Run M-x helm-mini.
  • Type into the prompt: *Di (partial word for Dired, no need to enter more). As you type, the buffers are narrowed incrementally. Now, only Dired buffer left. Press M-a to mark all.
  • Press TAB to switch to action menu and press K to select action Kill buffer(s).
  • Press RET to execute the action.

The good thing with helm-mini is that it can replace normal Emacs C-x b, so you won't have to use two key bindings, one for switching buffer and one for an actual buffer manager. helm-mini is all-in-one. Read more in my helm-mini guide.

helm-mini can also fuzzy match buffer and allow to open multiple buffers by marking with C-SPC or M-a (stand for Mark all). You can also narrow to buffer contain certain string by appending @ before a pattern, for example, @test means select only buffers contain the string "test".

1

I would try something like this:

(require 'dash)

(defun my-kill-dired-buffers ()
  (interactive)
  (kill-some-buffers
   (-filter (lambda (buffer)
              (save-excursion
                (set-buffer buffer)
                (eq major-mode 'dired-mode)))
            (buffer-list))))
  • 1
    I believe -filter requires dash.el from MELPA. You should probably list that as a requirement. – Vamsi Sep 24 '14 at 20:07
1

What you might actually be looking for is a way to clean up unwanted buffers automatically, in which case you should check out midnight.el, which is part of Emacs.

But generally speaking, ibuffer makes it very easy to operate on buffers in bulk, as Francisco suggested.

1

If you use Icicles, then you can use C-x k to kill all Dired buffers -- or all buffers in any given mode.

After hitting C-x k, use C-x M + to keep only the buffers with a certain mode. Hit TAB to see the modes, and choose dired-mode. Then use C-! to kill all of the buffers with that mode.

C-x k C-x M + dired-mode C-!

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.