4

There is a folder with many sub-folders, files and I would like to search for a given piece of text in all of these files with Emacs on a Windows 10 machine. (I know about M-x grep, but it does not work in Windows environment.)

Is there any way to start such recursive search from within Emacs on Windows 10 without installing additional software or extensions?

(The question marked as a "possible duplicate" is different. My question is about searching in files' contents. The another question is about searching for files.)

  • 2
    M-x grep works just fine in Windows if you install grep. In general, Emacs works best with certain Unix-y command line tools installed as well. If you have no issue installing Emacs, I can't imagine why you'd have an issue with installing the additional software that Emacs wants to be able to use. – phils Jan 25 '17 at 11:33
  • Agreed. The emacs experience on windows is improved dramatically with something like msys2. – InHarmsWay Jan 25 '17 at 12:20
  • If you only need to know if a regular expression is present in files, then you can use the % g command in dired. – DoMiNeLa10 Jan 25 '17 at 12:34
  • I don't have time to check now, but this smells like a duplicate. I'm pretty sure there are other questions about searching a set of files etc. – Drew Jan 25 '17 at 15:31
5

You can use Windows' builtin findstr command with M-x rgrep:

(when (eq system-type 'windows-nt)
  (with-eval-after-load 'grep
    ;; findstr can handle the basic find|grep use case
    (grep-apply-setting 'grep-find-template
                        "findstr /S /N /D:. /C:<R> <F>")
    (setq find-name-arg nil)))

It's not as versatile as grep, but it's fine for searching for fixed strings. Also note that Emacs sometimes suggests globs like *.[ch] but you need to use *.c *.h instead (use C-q SPC to enter the space).

4

The Elisp package elgrep is a swiss army knife for searching files. It is available on melpa. There are no dependencies on external tools as long as you do not search the contents of pdf documents.

Only an excerpt of the features:

  • By default you can do a simple regexp search in the files of the specified directory.
  • You can recurse into subdirectories. The minimal and maximal recursion depth can be set.
  • You can specify a regexp for the names of the files that should be searched.
  • You can specify a regexp for the names of the files that should be excluded from the search.
  • You can split files into records by regular expressions. By default the full file is one record. You can also use Elisp functions for finding the beginning and/or end of a record.
  • You can specify multiple search expressions as preconditions that must be present in the record to generate matches for the first regular expression.
    Preconditions can be negated by a leading !. A negated precondition must not occur in the record to be selected for output.
  • The search function can be changed. E.g., with cexp-search-forward from the library cexp you easily get a powerfull BibTeX database query.
  • You can display the search results with specified context.
    Example: The buffer is narrowed to a record when that record is searched. So you can specify elgrep/point-min and elgrep/point-max as context boundaries to display the full record if it matches.
    By default only the line with the match is shown.
  • You can edit the search results and write the edited stuff back to the corresponding files if the files are editable in Emacs.
  • Search queries are automatically saved. They are preserved in the elgrep-call-list if you name them. This list is saved to disk at exiting Emacs and reloaded when elgrep is loaded.

Example of the buffer with the search results:

*elgrep* buffer

The Elgrep menu settings that returned these search results (only a few are modified):

enter image description here

3

you can try the xah-find package. https://github.com/xahlee/xah-find

it is pure elisp, and designed just for emacs without external grep tools.

0

You could try Emacs Helm which is available to install from MELPA Stable. The Helm framework allows you to easily and intuitively work with files, directories and buffers. Moreover, it provide simple access to common Helm commands such as grep, find, locate, etc...

  • If you don't have grep (or similar) installed, I don't imagine helm is going to help? – phils Oct 22 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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