how to get grep working in Emacs on Windows (EmacsW32)
Asked Answered
F

5

13

M-x grep, M-x lgrep, M-x rgrep don't work in EmacsW32 for me.

I do M-x lgrep and it says grep is not a command:

grep -i -n "hello" * NUL
'grep' is not recognized as an internal or external command,
operable program or batch file.

Grep finished with no matches found at Sun Jan 31 05:59:06

Also what is that NUL thing? EmacsW32 homepage says it ships with Gnuwin32 utilities but it seems the work to configuring to actually use the Gnuwin32 grep is left to users.

How can I configure it to use either the shipped Gnuwin32 grep or the cygwin grep? Are both fine?

Fahland answered 30/1, 2010 at 21:29 Comment(3)
Yes, both greps should be fine. NUL is a fake file, the equivalent of /dev/null in Unix, that behaves like an empty file. I think that the intention is that if you forget to put a list of files on the command line, grep will not fail with an error message (since there is file NUL), but if you do think about providing files, NUL will be ignored (since it never matches). If that's the reason, it's a bit of hair-splitting on the part of Emacs creators.Durance
Did you set your PATH variable to contain a directory that had a grep command? If grep is provided with EmacsW32, it should be in a prominent location.Durance
If you have git-bash installed, you can include ...\Git\usr\bin in your Windows PATH variable where ... is the path to your git-bash installation.Luau
L
6

This article has some tips on how to get this working.

I got this working the other day, you can set the PATH environment variable inside emacs, and if you have cygwin and/or gnuw32 installed just set the path to those. This is a snippet from my .emacs that's applicable on windows only. I set to variables (cygwin-bin, gnu-bin) to the path where the programs are installed. Then build the path to those. One drawback is it blows away the rest of my path. Which hasn't been an issue so far, but If I was smarter with LISP I could probably figure something out. Anyway, hope this helps.

;;windows only stuff

(when (string-equal system-type "windows-nt")

(progn

(setq cygwin-bin "c:\\apps\\cygwin\\bin")

(setq gnu-bin "C:\\apps\\GnuWin32\\gnuwin32\\bin")

(setenv "PATH"

(concat cygwin-bin ";" gnu-bin ";"))

(setq exec-path

'(cygwin-bin gnu-bin)))) 

I should add, I arrived at this solution because putting qnuwin32 in front of the path in windows seems to be a risky proposition, you run the risk of messing up other programs on your machine. So this seemed to be a good compromise.

Lycanthrope answered 31/1, 2010 at 16:32 Comment(1)
Is it still necessary to setev "PATH" if we are setting the variable exec-path?Precatory
I
5

Windows doesn't come with a grep utility (in fact it does have findstr, but it's not exactly the same), so you need to install something like cygwin or unix utils for windows. After installing those, you need to add them to %PATH%, so that windows would know where to look for them. You can do that either in command line by running cmd.exe and issuing a command set PATH=<your_path>;%PATH% or in some settings widow you get by right clicking a My Computer icon (called something like environment variables here are some pictures of it).

Note, windows has a utility called find and it is also in system %PATH%, that can make certain commands (like M-x grep-find) not work correctly, so it is important you put unix-find first in your %PATH%.

Implausible answered 31/1, 2010 at 13:29 Comment(1)
If you already have Git for windows installed, add the following to the end of your PATH: C:\Program Files\Git\usr\binTransversal
F
1

I think the EmacsW32 user first need to set the user option w32shell-shell via the menu Options > Customize EmacsW32, which is set to none originally, and the user should set it to either cmd or cygwin. Setting it to cmd means M-x grep will use the built-in Gnuwin32 grep (and cmd.exe as a shell) and if the setting is saved correctly, the dotemacs will contain lines like the following:

(custom-set-variables
 ...  
 '(w32shell-shell (quote cmd))
 ...)

The custom-set-variable form should be placed as close to the beginning of the dotemacs as possible.

For some reason in my system, this is not enough, M-x grep will insist to use "grep -n" as its template rather than "grep -nH -e" and it always appends "NUL". In that case, add the following line in the dotemacs file.

(grep-compute-defaults)
Fahland answered 7/2, 2010 at 18:52 Comment(0)
B
0

Here's a no-nonsense answer that works:

http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/39cd9d63b07a9988#

Enjoy!

Bever answered 27/2, 2010 at 10:14 Comment(0)
N
0

I use Cygwin (an old version). For Emacs I use libraries cygwin-mount.el and setup-cygwin.el, loaded in that order. Works just fine.

Nada answered 21/8, 2011 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.