I hope it's a simple problem..

I'd like to search for the regexp 'thisWord' only in the marked files of a dired buffer.

Usually I used in the dired buffer:

  1. M-x grep and then in the mini-buffer
  2. grep -r -nH 'thisWord' .

But this searches all the files of the directory not just the marked ones.

Well, the dired mode of emacs has the possibility to restrict the search on the marked files with just using A (dired-do-find-regexp). But the results are listed in the unusual "xref format", I need the "grep format":

./file-a.org:16253: ..thisWord...

I there a simple way to get the search results of the marked files in the "grep format"?

3 Answers 3


Eshell's grep is compatible with grep(1) and uses Emacs's internal grep interface, thus you can try something like the following:

M-x dired-do-eshell-command RET grep -nH --color your-search--pattern RET
(defun dired-do-eshell-command (command)
  "Run an Eshell command on the marked files."
  (interactive "sEshell command: ")
  (let ((files (dired-get-marked-files t)))
     (format "%s %s" command (mapconcat #'identity files " ")))))
  • Thanks! It works. I will try to use it not just for grep commands.
    – rl1
    Commented Feb 19, 2017 at 19:12
  • You can use string-join instead of (mapconcat #'identity ...) if you have Emacs 24.4 or later. Commented Feb 19, 2017 at 23:02
  • @TianxiangXiong you're right, it is provided by subr-x.el (which is not loaded by Emacs at startup, thus it is probably necessary to (require 'subr-x) before calling string-join).
    – xuchunyang
    Commented Feb 20, 2017 at 5:24
  • 1
    – phils
    Commented Feb 20, 2017 at 10:14

If you use library Dired+ (dired+.el) then you can use command dired-do-grep (bound by default to M-g in Dired mode) to do what you request.

diredp-do-grep is an interactive compiled Lisp function in dired+.el.

(diredp-do-grep COMMAND-ARGS)

Run grep on marked (or next prefix arg) files.

A prefix argument behaves according to the ARG argument of dired-get-marked-files. In particular, C-u C-u operates on all files in the Dired buffer.

  • Thanks! I couldn't test it. I install new packages only if it is necessary.
    – rl1
    Commented Feb 19, 2017 at 19:11
  • @user52408: Probably best to mention in the text of your question if you're unwilling to use code which isn't part of Emacs.
    – phils
    Commented Feb 20, 2017 at 10:09
  • @phils Oh, there is a misunderstanding. I'm not unwilling, but it prefer solutions "within emacs". I can mention this next time.
    – rl1
    Commented Feb 20, 2017 at 14:29
(defun dired-grep-marked-files ()
"`i` case insensitive; `n` print line number;
`I` ignore binary files; `E` extended regular expressions."
  (let* ((files (dired-get-marked-files))
         (search-term (read-string "regex:  "))
             " "
             "-inIE --color=always -C2"
             " "
             " "
             (mapconcat 'identity files " "))))
    (when (and files search-term grep-command)
      (compilation-start grep-command 'grep-mode (lambda (mode) "*grep*") nil))))
  • s/'identity/'shell-quote-argument/
    – phils
    Commented Feb 20, 2017 at 10:15

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.