26

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?

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

8 Answers8

37

You could do the following:

M-x ibuffer

* / to mark buffers in dired mode.

D to delete them.

Francisco Dibar
  • 486
  • 3
  • 5
  • 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
  • 1
    You can also press `t` (`dired-toggle-marks`) to mark all (if none was previously marked). – caisah Sep 25 '14 at 04: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 07 '14 at 13:29
  • @nispio You are right. I don't know why, but I was thinking all buffers were _dired_... – caisah Oct 07 '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
13

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.

Rangi Lin
  • 987
  • 9
  • 15
  • 1
    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 07 '14 at 09:17
  • 1
    `(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 07 '14 at 09: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 07 '14 at 10:17
  • To clarify Rangi Lin's comment : ```(add-hook 'ibuffer-mode-hook #'(lambda () (ibuffer-switch-to-saved-filter-groups "Default")))``` A different example here : http://martinowen.net/blog/2010/02/03/tips-for-emacs-ibuffer.html – kotchwane Jan 04 '20 at 13:52
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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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 09 '18 at 08: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 09 '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)))
nicael
  • 1,598
  • 2
  • 13
  • 20
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".

Tu Do
  • 6,772
  • 20
  • 39
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-!
Drew
  • 75,699
  • 9
  • 109
  • 225
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))))
shosti
  • 5,048
  • 26
  • 33
  • 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.

sanityinc
  • 2,871
  • 13
  • 17