Recently, there was some discussion in the comp.emacs newsgroup
regarding dired and the way `dired-find-file' handles directories.
`dired-find-file' will always create a new buffer for each directory.
If you work in many directories, these dired buffers can accumulate
and quickly become unwieldy.
The original post asked for a way to reuse the same dired buffer when
changing directories. Someone suggested using 'i',
`dired-maybe-insert-subdir', instead of 'RET'. This isn't ideal,
however, because it simply inserts the contents of the subdirectory
into the current dired buffer and doesn't remove the old directory's
contents. It also forces users to use two different keys to navigate
through directories and select files, 'i' and 'RET', instead of just
being able to use 'RET'.
I would really like to have the new directory contents replace the old
in a dired buffer. If my cursor is positioned over a file, however, I
want dired to do the normal thing: open the file in a new buffer.
This seems to me like the most intuitive behavior. I hacked out some
lisp code that does this:
(defun dired-follow-file ()
"In dired, visit the file or directory on this line.
If a directory is on the current line, replace the current
dired buffer with one containing the contents of the directory.
Otherwise, invoke `dired-find-file' on the file."
(let ((filename (dired-get-filename)))
;; if the file is a directory, replace the buffer with the
;; directory's contents
(if (file-directory-p filename)
;; otherwise simply perform a normal `dired-find-file'
Now, I simply need to remap the dired keys
(local-set-key "\C-m" 'dired-follow-file)
(local-set-key "e" 'dired-follow-file)
(local-set-key "f" 'dired-follow-file)))
and (abracadabra) dired replaces the old directory with the new one in
the same buffer when I press 'RET'. (Note that it's always easy to
get back to the parent directory because you can just select '..' in
the new directory listing.)
I posted this lisp snippet to comp.emacs and was asked to also send it
to <bug-gnu-emacs(a)gnu.org> and <xemacs-beta(a)xemacs.org>. Here it
is. I hope this is helpful.
Show replies by thread