In Dired, when you choose a directory to visit, it is normally visited in a new buffer – the Dired buffer you chose it in is not deleted.
Some people don’t like this behavior, because as you navigate around the directory tree you accumulate Dired buffers, one for each directory you visit.
In Emacs 22 and later, you can use
‘a’ in a Dired buffer, instead of
‘RET’. This method does not affect mouse clicks on directory names, however. Alternatively you can use this code (mouse clicks is still not affected, but
(put 'dired-find-alternate-file 'disabled nil)
When moving to parent directory by `^´, Dired by default creates a new buffer for each movement up. The following rebinds `^´ to use the same buffer.
(add-hook 'dired-mode-hook (lambda () (define-key dired-mode-map (kbd "^") (lambda () (interactive) (find-alternate-file ".."))) ; was dired-up-directory ))
‘toggle-diredp-find-file-reuse-dir’, to let you choose whether or not to reuse Dired buffers. This changes all key bindings for
‘diredp-mouse-find-file’to use the same buffer for visited directories.
‘toggle-diredp-find-file-reuse-dir’interactively to toggle between the two visit-directory behaviors, or put this in your .emacs to just turn on directory-buffer reuse:
‘diredp-mouse-find-file-reuse-dir-buffer’that open a directory in the same buffer – bind them as you like.
Here are two ways to advise
‘dired-find-file’, so it reuses a directory buffer. (But see AdviceVsHooks.)
(eval-after-load "dired" '(progn (defadvice dired-advertised-find-file (around dired-subst-directory activate) "Replace current buffer if file is a directory." (interactive) (let* ((orig (current-buffer)) ;; (filename (dired-get-filename)) (filename (dired-get-filename t t)) (bye-p (file-directory-p filename))) ad-do-it (when (and bye-p (not (string-match "[/\\\\]\\.$" filename))) (kill-buffer orig))))))
;; Another way of achieving this:
;; we want dired not not make always a new buffer if visiting a directory ;; but using only one dired buffer for all directories. (defadvice dired-advertised-find-file (around dired-subst-directory activate) "Replace current buffer if file is a directory." (interactive) (let ((orig (current-buffer)) (filename (dired-get-filename))) ad-do-it (when (and (file-directory-p filename) (not (eq (current-buffer) orig))) (kill-buffer orig))))
(eval-after-load "dired" ;; don't remove `other-window', the caller expects it to be there '(defun dired-up-directory (&optional other-window) "Run Dired on parent directory of current directory." (interactive "P") (let* ((dir (dired-current-directory)) (orig (current-buffer)) (up (file-name-directory (directory-file-name dir)))) (or (dired-goto-file (directory-file-name dir)) ;; Only try dired-goto-subdir if buffer has more than one dir. (and (cdr dired-subdir-alist) (dired-goto-subdir up)) (progn (kill-buffer orig) (dired up) (dired-goto-file dir))))))
I used to hate those Dired buffers cluttering up my Emacs so I had one of these hacks enabled for years. However, for some years now I have gone back to the default behaviour which has (at least) one advantage that is worth more to me than to have less buffers: performance when opening Dired buffers for directories on slow servers. I open a lot of directories on shares at different servers at work, many in remote locations. As with many Emacs concepts I have now got used to it and suspect that there was actually some thought behind this from the beginning even if it seems strange at first. Also, since I have F5 bound to a helper command to kill buffers, it is really easy to get rid of the dired buffers when I want to. And, sometimes when I drill down from one directory to a sub directory I discovver that I wanted to go to the parent. Then for me I can just kill the current one (F5) or switch to the previous one (bound to F1 in my setup). So, that’s my experience and I just thought I should share it :) – MaDa
‘subdir’commands—that way I don’t need to work on a buffer-per-directory basis. – Iceland_jack