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?
You could do the following:
* / to mark buffers in dired mode.
D to delete them.
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 . "\*.*\*")) )))
C-x C-b open
ibuffer window, you can see something like this:
Move cursor to
[ Dired ] (with
M-n), then press
d to mark as delete
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.
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:
Tell Dired to reuse Dired buffers:
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-S-r) in Dired.
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
- Type into the prompt:
*Di(partial word for
Dired, no need to enter more). As you type, the buffers are narrowed incrementally. Now, only
Diredbuffer left. Press
M-ato mark all.
TABto switch to action menu and press
Kto select action
RETto 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
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))))
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.
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-!