11

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.)

  • 4
    `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
  • 1
    Agreed. The emacs experience on windows is improved dramatically with something like [msys2](https://sourceforge.net/projects/msys2/). – InHarmsWay Jan 25 '17 at 12:20
  • 1
    If you only need to know if a regular expression is present in files, then you can use the `% g` command in dired. –  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
  • Possible duplicate of [How to search for files from a root directory and get the results as a dired buffer?](http://emacs.stackexchange.com/questions/18593/how-to-search-for-files-from-a-root-directory-and-get-the-results-as-a-dired-buf) – Drew Jan 25 '17 at 16:00
  • Phils, InHarmsWay, thank you. Sure, I can install some additional software, I just wanted to find out if it is possible without those extra tools. I'm absolutely grateful to have your answers. Should I consider closing this question and asking "what tools to install on Windows 10 to get a unix-like environment around Emacs"? –  Jan 25 '17 at 16:30
  • You can try [elgrep](https://github.com/TobiasZawada/elgrep). – Tobias Jan 25 '17 at 17:43
  • M-x grep works fine without additional tool on windows 10. Do followings: `M-x grep` `enter` then minibar shows: grep -nH --null Change it to: grep -nHR -e searchText then `enter`. source: https://stackoverflow.com/a/1332102/4466255 – Kushdesh Nov 21 '21 at 13:08

4 Answers4

7

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).

npostavs
  • 9,033
  • 1
  • 21
  • 53
7

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.

Xah Lee
  • 1,756
  • 12
  • 11
7

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

Tobias
  • 32,569
  • 1
  • 34
  • 75
2

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...

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