Lgrep and rgrep in Emacs

I have problems with greps in Emacs.

a) grep doesn't seem to understand. [ch] to search for .c and .h files. This is the default option provided by Emacs with the lgrep command. This example searches for the word "global" in .c / .h files.

grep -i -nH "global" *.[ch] grep: *.[ch]: No such file or directory Grep exited abnormally with code 2 at Mon Feb 16 19:34:36 

Is this format invalid?

b) Using rgrep , I get the following error:

 find . "(" -path "*/CVS" -o -path "*/.svn" -o -path "*/{arch}" -o -path "*/.hg" -o -path "*/_darcs" -o -path "*/.git" -o -path "*/.bzr" ")" -prune -o -type f "(" -iname "*.[ch]" ")" -print0 | xargs -0 -e grep -i -nH "global" FIND: Wrong parameter format Grep finished (matches found) at Mon Feb 16 19:37:10 

I am using Emacs 22.3.1 on Windows XP with the GNU W32 Utils (grep, find, xargs, etc.). Grep v2.5.3 and find v4.2.20.

What am I missing?

UPDATE:

Too bad that you canโ€™t accept a few answers ... because the solution to my problems is laid out.

 grep -i -nH "global" *.c *.h 

This solves the first problem. Thanks luapyad!

 (setq find-program "c:\\path\\to\\gnuw32\\find.exe") 

emacs really used windows find.exe. Forced gnu32 detection fixes the second issue. Thanks scottfrazer.

However, I still like ack .

+6
grep emacs
source share
6 answers

Well, there is always Ack and Ack.el

+5
source share

I found out using:

 (setq find-program "\"C:/path/to/GnuWin32/bin/find.exe\"") (setq grep-program "\"C:/path/to/GnuWin32/bin/grep.exe\"") 

It works better in windows, as you could make space around the path and eventually go bad.

Note. I used two programs in my .emacs file.

Hope this helps some other programmer who needs it;)

+6
source share

For a), it looks like there simply aren't .c or .h files in the current directory.

For b) Windows is trying to use its own find, not one of the GNU W32 Utils. Try:

(setq find-program "c:\\path\\to\\gnuw32\\find.exe")

+5
source share

Adam Rosenfield's commentary should be expanded in response:

 grep -r --include=\*.[ch] --exclude=\*{CVS,.svn,arch} -i -nH 

To make the example given in this question use this:

 grep -i -nH --include=\*.[ch] "global" * 

It is also useful to set the grep-command variable to provide the default values โ€‹โ€‹of Mx grep :

 (setq grep-command "grep -i -nH --include=\*.[ch] ") 

Some other useful command line options for grep are also listed:

 -n print the line number -s suppress error messages -r recursive 
+3
source share

I think the general problem is that windows cmd "shell" behaves very differently with unix shell regarding regular expression extensions and wildcards.

To answer your (a) above, try using:

 grep -i -nH "global" *.c *.h 

(You will still get an "invalid argument" if no * .c or * .h exist).

Or you can use the command line option --include=\*.[ch] so that make grep does the โ€œproperโ€ matching of file name patterns (see grep --help for other options)

+2
source share

Usually I just use Mx grep and change the command line arguments when prompted, if I need to. But I just tried to run Mx lgrep and got the same thing as you. It just means that no files match *.[ch] in the current directory. You can configure default options to include -r and search recursively through child directories:

 Mx customize-group RET grep RET 

Locate lgrep in this buffer to find / edit the grep template.

As for Mx rgrep , I suspect this has something to do with the version of Windows find that the default options don't like. This command works great for me on Linux. Locate rgrep in the same configuration buffer and configure these parameters until Windows find is happy.

Sorry, I can no longer help with the Windows settings, but I am not familiar with them.

+1
source share

All Articles