38

If I have multiple files marked, how do I find/visit all those marked files in emacs, beside running dired-find-file on each of them?

Is there a build-in command, or do I need some extra lisp code?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
polyglot
  • 9,945
  • 10
  • 49
  • 63

3 Answers3

35

In Emacs 23.2 and higher, the dired-x.el module is available, and it gives you access to a command that does exactly what you want. After you load it (just (load "dired-x"), normally), you'll be able to invoke the function dired-do-find-marked-files. Here's its built-in documentation:

(dired-do-find-marked-files &optional NOSELECT)

Find all marked files displaying all of them simultaneously.
With optional NOSELECT just find files but do not select them.

The current window is split across all files marked, as evenly as possible.
Remaining lines go to bottom-most window.  The number of files that can be
displayed this way is restricted by the height of the current window and
`window-min-height'.

To keep dired buffer displayed, type C-x 2 first.
To display just marked files, type C-x 1 first.

So after dired-x is loaded, you can just use M-x dired-do-find-marked-files RET and you'll get exactly what your question asks for: all marked files will be visited as though you'd run dired-find-file on all of them.

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
xwl
  • 840
  • 10
  • 12
  • My dired-mode doesn't recognize this command, and I'm on 23.4 – Malabarba Mar 29 '12 at 13:44
  • 1
    @Bruce Connor: probably You don't have dired-x installed. Try to add `(add-hook 'dired-load-hook (function (lambda () (load "dired-x"))))` in `.emacs`. – Adobe Apr 02 '12 at 13:54
  • 3
    'F' key is keyboard shortcut for dired-do-find-marked-files, at least in Emacs 24.4. – thdox Nov 15 '14 at 13:50
  • I will also add that you can use a prefix argument to prevent Emacs from splitting the frame into a new window for each file opened. So for example, you can use `C-u M-x dired-do-find-marked-files`, or alternatively `C-u C-F` to use the default keybinding. – dpritch Mar 29 '19 at 15:12
26

If you add this to your .emacs, you'll be able to open the files via the keybinding 'F'.

(eval-after-load "dired"
  '(progn
     (define-key dired-mode-map "F" 'my-dired-find-file)
     (defun my-dired-find-file (&optional arg)
       "Open each of the marked files, or the file under the point, or when prefix arg, the next N files "
       (interactive "P")
       (let* ((fn-list (dired-get-marked-files nil arg)))
         (mapc 'find-file fn-list)))))

Obviously you can just override the built-in 'f' if you want.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 5
    Thanks for this! Small stylistic point but you don't need to use `let*` in this case (`let` is sufficient). And really we could probably just simplify that last form to `(mapc 'find-file (dired-get-marked-files nil arg))`. – camdez Nov 06 '14 at 15:03
6

You could try dired+ which provides many extensions to dired including the ability to select multiple files and find/view all of them.

luapyad
  • 3,860
  • 26
  • 20