I'm trying to write a function that opens all marked files externally in their default programs. I've tried using (dired-get-marked-files) to get the filepaths, but that only returns the marked files in the current buffer. Is it possible to get all marked files from all dired buffers?

1 Answer 1


This should do it:

(defun foo ()
  "Return a list of marked files from all Dired buffers."
  (let ((files  ())
        (here   ()))
    (dolist (buf  (mapcar #'cdr dired-buffers))
      (when (buffer-live-p buf)
        (with-current-buffer buf
          (setq here  (dired-get-marked-files nil nil nil t)))
        (when (or (null (cdr here))  (eq t (car here)))
          (setq here  (cdr here)))
        (setq files  (nconc here files))))
    (setq files  (delete-dups files))))

The reason to use (dired-get-marked-files nil nil nil t), instead of just (dired-get-marked-files), is because the latter returns the file where the cursor is when no files are explicitly marked. Presumably you want to include only files that are explicitly marked.

The reason for the code involving here, that is, what's returned for a given Dired buffer, is to take care of that nothing-marked case, i.e., to remove the case of a file that's returned because no files are marked.

The reason to use delete-dups is that it's possible that you have more than one Dired buffer that has the same (marked) file (even the same absolute file name). For example, you might have a Dired buffer for directory toto, and one for directory toto/titi, and in the buffer for toto its subdir titi might be inserted, and the same file in directory titi might be marked in both Dired buffers.


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.