如何让 grep 在 Windows 上的 Emacs 中工作 (EmacsW32)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2169520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:51:30  来源:igfitidea点击:

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

windowsemacsgrepemacsw32

提问by Yoo

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

Mx grep、Mx lgrep、Mx rgrep 对我来说在 EmacsW32 中不起作用。

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

我做了 Mx lgrep,它说 grep 不是命令:

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.

还有那 NUL 是什么东西?EmacsW32 主页说它附带 Gnuwin32 实用程序,但似乎配置以实际使用 Gnuwin32 grep 的工作留给了用户。

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

如何配置它以使用随附的 Gnuwin32 grep 或 cygwin grep?两个都好吗?

采纳答案by Tom Willis

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

前几天我让它工作了,你可以在 emacs 中设置 PATH 环境变量,如果你安装了 cygwin 和/或 gnuw32,只需设置这些的路径。这是我的 .emacs 中的一个片段,仅适用于 Windows。我将变量(cygwin-bin、gnu-bin)设置为安装程序的路径。然后建立通向这些的路径。一个缺点是它吹走了我剩下的路。到目前为止,这还不是问题,但如果我对 LISP 更聪明,我可能会想出一些办法。无论如何,希望这会有所帮助。

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

我应该补充一点,我得到这个解决方案是因为将 qnuwin32 放在 Windows 中的路径前面似乎是一个冒险的提议,你冒着搞乱机器上其他程序的风险。所以这似乎是一个很好的妥协。

回答by Dmitry

Windows doesn't come with a greputility (in fact it does have findstr, but it's not exactly the same), so you need to install something like cygwinor 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.exeand issuing a command set PATH=<your_path>;%PATH%or in some settings widow you get by right clicking a My Computericon (called something like environment variableshereare some pictures of it).

Windows 没有附带grep实用程序(实际上它有findstr,但并不完全相同),因此您需要为 windows安装类似cygwinunix utils 之类的东西。安装后,您需要将它们添加到%PATH%,以便 Windows 知道在哪里查找它们。您可以在命令行中通过运行cmd.exe和发出命令来执行此操作,也可以在set PATH=<your_path>;%PATH%某些设置窗口中通过右键单击“我的电脑”图标(此处称为环境变量之类的东西,是它的一些图片)来执行此操作。

Note, windows has a utility called findand 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%.

请注意,windows 有一个名为的实用程序find,它也在 system 中%PATH%,它可以使某些命令(如M-x grep-find)无法正常工作,因此将 unix-find 放在%PATH%.

回答by Yoo

I think the EmacsW32 user first need to set the user option w32shell-shellvia 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:

我认为EmacsW32用户首先需要w32shell-shell通过 menu设置用户选项Options > Customize EmacsW32,它最初设置为none,用户应该将其设置为cmd或cygwin。将它设置为 cmd 意味着 Mx grep 将使用内置的 Gnuwin32 grep(和 cmd.exe 作为外壳),如果设置正确保存,dotemacs 将包含如下所示的行:

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

自定义设置变量表单应尽可能靠近 dotmacs 的开头。

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.

出于某种原因,在我的系统中,这还不够,Mx grep 将坚持使用“grep -n”作为其模板而不是“grep -nH -e”,并且它总是附加“NUL”。在这种情况下,请在 dotmacs 文件中添加以下行。

(grep-compute-defaults)

回答by Terrence Brannon

回答by Drew

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

我使用 Cygwin(旧版本)。对于 Emacs,我使用库cygwin-mount.elsetup-cygwin.el,按顺序加载。工作得很好。