DiredReuseDirectoryBuffer

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.

Libraries that Provide Reuse

Use ‘diredp-toggle-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-toggle-find-file-reuse-dir 1).
Dired+ also provides commands ‘diredp-find-file-reuse-dir-buffer’ and ‘diredp-mouse-find-file-reuse-dir-buffer’ that open a directory in the same buffer – bind them as you like.

Do It Yourself?

For those who do not want to load a library (too easy!) —

The simplest method is to just use ‘a’ in a Dired buffer, instead of ‘e’, ‘f’, or ‘RET’. This uses the standard command ‘find-alternate-file’, which replaces the current buffer with another file. It will give a warning the first time you use it. It’s available since Emacs 22. This method does not affect mouse clicks on directory names, however.

Alternatively you can use this code (mouse clicks is still not affected, but ‘RET’ works):

(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
 ))

And 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))))
Using the advising methods above will still create a new buffer if you invoke ^ (dired-up-directory). To prevent this:
     (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))))))

See Also (Links)

See also Xah Lee's page of Emacs Dired Customizations, especially under the section entitled, “How to make dired use the same buffer for viewing directory?”

Experiences

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 discover 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

That is my experience as well, I struck a fine balance by using the ‘subdir’ commands—that way I don’t need to work on a buffer-per-directory basis. – Iceland_jack

CategoryDirectories