7

If I'm in dired and have a subfolder with another dired buffer (a common situation when navigating up/down directory) and want to delete a folder, I hit Dx to mark for deletion, then execute.

Emacs then asks if I want to trash the folder. I confirm, then it asks if I want to kill the dired buffer of the folder as well.

I can't think of a situation where I'd like to trash the folder but keep the buffer. Is there a way to always trash the buffer when trashing the folder?

1
  • A Dired buffer does not always correspond to a particular folder. A Dired buffer can contain any number of folders and files, from any part(s) of the file system. Presumably your question applies only when a Dired buffer maps one-to-one to a single folder.
    – Drew
    Commented Sep 6, 2017 at 17:11

2 Answers 2

3

I think it is for safety. since it can be surprising if Emacs deletes an ordinary buffer without asking user. You can file a bug report if you want to. Meanwhile you can always advice y-or-n-p to do what you want.

(define-advice dired-clean-up-after-deletion
    (:around (old-fun &rest r) kill-dired-buffer-quietly)
  (define-advice y-or-n-p (:around (old-fun prompt) just-yes)
    (if (string-prefix-p "Kill Dired buffer" prompt)
        t
      (funcall old-fun prompt)))
  (unwind-protect (apply old-fun r)
    (advice-remove 'y-or-n-p #'y-or-n-p@just-yes)))
3

You can add the following snippet in your init file:

(eval-after-load  'dired                                                                                                               
  '(defun dired-clean-up-after-deletion (fn)
     "My. Clean up after a deleted file or directory FN.                                                                               
Remove expanded subdir of deleted dir, if any."
     (save-excursion (and (cdr dired-subdir-alist)
                          (dired-goto-subdir fn)
                          (dired-kill-subdir)))

     ;; Offer to kill buffer of deleted file FN.                                                                                       
     (if dired-clean-up-buffers-too
         (progn
           (let ((buf (get-file-buffer fn)))
             (and buf
                  (save-excursion ; you never know where kill-buffer leaves you                                                        
                    (kill-buffer buf))))
           (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))
                 (buf nil))
             (and buf-list
                  (while buf-list
                    (save-excursion (kill-buffer (car buf-list)))
                    (setq buf-list (cdr buf-list)))))))
     ;; Anything else?                                                                                                                 
     ))

Courtesy of pmielke

You may also want to browse this Emacs wiki page about buffer reuse in Dired

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.