for @file{..} and typing @kbd{f} there.
@end table
+@defopt dired-kill-when-opening-new-dired-buffer
+ When visiting a new sub-directory in Dired, Emacs will (by default)
+open a new buffer to display this new directory, and leave the old
+Dired buffer as is. If this user option is non-@code{nil}, the old
+Dired buffer will be killed after selecting the new directory. This
+means that if you're traversing a directory structure in Dired, you
+won't end up with more than a single Dired buffer.
+@end defopt
+
@node Marks vs Flags
@section Dired Marks vs.@: Flags
** Dired
++++
+*** New user option 'dired-kill-when-opening-new-dired-buffer'.
+If non-nil, Dired will kill the current buffer when selecting a new
+directory to display.
+
---
*** Behavior change on 'dired-clean-confirm-killing-deleted-buffers'.
Previously, if 'dired-clean-up-buffers-too' was non-nil, and
:group 'dired
:version "28.1")
+(defcustom dired-kill-when-opening-new-dired-buffer nil
+ "If non-nil, kill the current buffer when selecting a new directory."
+ :type 'boolean
+ :version "28.1")
+
\f
;;; Internal variables
(progn
(if other-window
(dired-other-window up)
- (dired up))
+ (dired--find-possibly-alternative-file up))
(dired-goto-file dir)))))
(defun dired-get-file-for-visit ()
(defun dired-find-file ()
"In Dired, visit the file or directory named on this line."
(interactive)
- (dired--find-file #'find-file (dired-get-file-for-visit)))
+ (dired--find-possibly-alternative-file (dired-get-file-for-visit)))
+
+(defun dired--find-possibly-alternative-file (file)
+ "Find FILE, but respect `dired-kill-when-opening-new-dired-buffer'."
+ (if (and dired-kill-when-opening-new-dired-buffer
+ (file-directory-p file))
+ (progn
+ (set-buffer-modified-p nil)
+ (dired--find-file #'find-alternate-file file))
+ (dired--find-file #'find-file file)))
(defun dired--find-file (find-file-function file)
"Call FIND-FILE-FUNCTION on FILE, but bind some relevant variables."