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– nispioCommented Oct 14, 2014 at 16:28
8 Answers
You could do the following:
M-x
ibuffer
* /
to mark buffers in dired mode.
D
to delete them.
-
1I had never heard of ibuffer before. I will definitely be mapping
C-x C-b
toibuffer
.– nispioCommented Sep 24, 2014 at 19:28 -
1You can also press
t
(dired-toggle-marks
) to mark all (if none was previously marked).– caisahCommented Sep 25, 2014 at 4:25 -
@caisah
dired-toggle-marks
is the name of the dired command. Pressingt
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.– nispioCommented Oct 7, 2014 at 13:29 -
@nispio You are right. I don't know why, but I was thinking all buffers were dired...– caisahCommented Oct 7, 2014 at 15:15
-
1Note: 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. Commented Oct 20, 2016 at 13:28
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:
Move cursor to [ Dired ]
(with M-p
, M-n
), then press d
to mark as delete
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.
-
1Filter groups did not display for me when I first launched
ibuffer
after evaluating your code. I had to hit/ R
(bound toibuffer-switch-to-saved-filter-groups
) to get the groups.– itsjeydCommented Oct 7, 2014 at 9: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 notDefault
Commented Oct 7, 2014 at 9:40 -
1Yes, that's what I ended up doing. Didn't know about filter groups before reading your answer, thanks for mentioning them ;)– itsjeydCommented Oct 7, 2014 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 : martinowen.net/blog/2010/02/03/tips-for-emacs-ibuffer.html Commented Jan 4, 2020 at 13:52
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:
Load library Dired+ (
dired+.el
):(require 'dired+)
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.)– nispioCommented Oct 10, 2014 at 18:44
-
-
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.)– DrewCommented Oct 9, 2018 at 14:43
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)))
You can also use helm-mini
:
- Run
M-x helm-mini
. - Type into the prompt:
*Di
(partial word forDired
, no need to enter more). As you type, the buffers are narrowed incrementally. Now, onlyDired
buffer left. PressM-a
to mark all. - Press
TAB
to switch to action menu and pressK
to select actionKill 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".
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))))
-
1I believe
-filter
requiresdash.el
from MELPA. You should probably list that as a requirement.– VamsiCommented Sep 24, 2014 at 20:07
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.
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-!