2

I'm using Emacs on windows, and was trying to run grep from within eshell (M-x eshell). The default shell through M-x is cmd.exe, but eshell has linux utilities so I'm trying to use eshell.

Now, when i try grep with typical arguments, say -i to a case insensitive search, I get a message saying 'pattern "-i" not found'. So, the grep tool seems to think the option is the pattern. But if I give the exact patter without any options, and the exact filename to search in, it works. For example, this works:

> grep "Hello" main.c

It opens up a new buffer with the results which I can click on, or browse through

This is quite inconvenient. Any help to fix this would be appreciated.

Side note, I've tried using the WSL ubuntu bash and that works fine, except the PROMPT looks all messed up, and the results of grep are not clickable.

1
  • Since eshell/grep has many alternatives and fallbacks a screenshot of the buffer you get would be good. Have you grep installed as an external tool? I already postet the most likely problem you might encounter as an answer.
    – Tobias
    Nov 4, 2019 at 8:51

1 Answer 1

2

Looks like Emacs cannot find the external grep tool and uses its internal eshell-poor-mans-grep.

It interprets the first argument as expression and all other arguments as files.

Here is the doc string of poor-mans-grep which pityingly does not clarify the arguments:

eshell-poor-mans-grep is a compiled Lisp function in ‘em-unix.el’.

(eshell-poor-mans-grep ARGS)

A poor version of grep that opens every file and uses ‘occur’. This eats up memory, since it leaves the buffers open (to speed future searches), and it’s very slow. But, if your system has no grep available...

If you want to use an external grep you need to install it and make it known to Emacs (e.g., by correcting the PATH environment variable). One possible choice is mingw-w64.

Note that Emacs has its own powerful grep tools which do not depend on external tools like xah-find and elgrep on melpa.

5
  • Hi Tobias, thanks! Looks like the eshell grep is compiled in. ~ $ which grep eshell/grep is a compiled Lisp function in ‘em-unix.el’. I do have grep.exe on my system since I have git installed. It does use mingw. Do I just need to update my system path variable or something specifically inside emacs? If it's the latter how do I do that? Also, the windows version of grep is a .exe. EShell does not recognize that. Is there a way to incorporate that into eshell?
    – archmuon
    Nov 7, 2019 at 1:24
  • @archmuon Try grep.exe explicitly. You can also try *grep with an explicit star prefix which enforces the external program.
    – Tobias
    Nov 7, 2019 at 2:07
  • 1
    none of those options seem to work in my eShell. I just get the error message "grep" or "grep.exe": external command not found. (I have cd'ed to the folder in the emacs bin directory where I placed the grep.exe) (I have also tried using grep1.exe to avoid any conflicts)
    – archmuon
    Nov 7, 2019 at 19:33
  • Btw, when I do man grep, it doesn't show the poor-mans-grep that you showed above. This is what I get: GREP(1) General Commands Manual GREP(1) NAME grep, egrep, fgrep, rgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN]... [-f FILE]... [FILE...]
    – archmuon
    Nov 7, 2019 at 19:34
  • @archmuon For a test run setenv PATH "$PATH:$exec-directory" and (setq eshell-no-grep-available nil) in eshell. Afterwards try *grep.exe again.
    – Tobias
    Nov 7, 2019 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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