windows 如何从 Emacs 中运行 Cygwin Bash Shell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/235254/
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
How can I run Cygwin Bash Shell from within Emacs?
提问by Ray
I am running GNU Emacs on Windows so entering:
我在 Windows 上运行 GNU Emacs,所以输入:
M-x shell
launches the Windows command-line DOS shell. However, I would like to instead be able to run the Cygwin Bash Shell (or any other non-Windows shell) from within Emacs. How can this be easily done?
启动 Windows 命令行 DOS shell。但是,我希望能够从 Emacs 中运行 Cygwin Bash Shell(或任何其他非 Windows shell)。如何轻松做到这一点?
回答by cjm
shell-file-name
is the variable that controls which shell Emacs uses when it wants to run a shell command.
shell-file-name
是控制 Emacs 在运行 shell 命令时使用哪个 shell 的变量。
explicit-shell-file-name
is the variable that controls which shell M-x shell
starts up.
explicit-shell-file-name
是控制哪个 shellM-x shell
启动的变量。
Ken's answerchanges both of those, which you may or may not want.
肯的回答改变了这两者,你可能想要也可能不想要。
You can also have a function that starts a different shell by temporarily changing explicit-shell-file-name
:
您还可以通过临时更改来启动不同的 shell 的函数explicit-shell-file-name
:
(defun cygwin-shell ()
"Run cygwin bash in shell mode."
(interactive)
(let ((explicit-shell-file-name "C:/cygwin/bin/bash"))
(call-interactively 'shell)))
You will probably also want to pass the --login
argument to bash, because you're starting a new Cygwin session. You can do that by setting explicit-bash-args
. (Note that M-x shell
uses explicit-
PROGRAM-args
, where PROGRAM is the filename part of the shell's pathname. This is why you should not include the .exe
when setting the shell.
您可能还想将--login
参数传递给 bash,因为您正在启动一个新的 Cygwin 会话。您可以通过设置来做到这一点explicit-bash-args
。(请注意,M-x shell
使用explicit-
PROGRAM -args
,其中 PROGRAM 是 shell 路径名的文件名部分。这就是为什么.exe
在设置 shell 时不应包含。
回答by Chris Jones
The best solution I've found to this is the following:
我发现的最佳解决方案如下:
;; When running in Windows, we want to use an alternate shell so we
;; can be more unixy.
(setq shell-file-name "C:/MinGW/msys/1.0/bin/bash")
(setq explicit-shell-file-name shell-file-name)
(setenv "PATH"
(concat ".:/usr/local/bin:/mingw/bin:/bin:"
(replace-regexp-in-string " " "\\ "
(replace-regexp-in-string "\\" "/"
(replace-regexp-in-string "\([A-Za-z]\):" "/\1"
(getenv "PATH"))))))
The problem with passing "--login" as cjm suggestsis your shell will always start in your home directory. But if you're editing a file and you hit "M-x shell", you want your shell in that file's directory. Furthermore, I've tested this setup with "M-x grep" and "M-x compile". I'm suspicious that other examples here wouldn't work with those due to directory and PATH problems.
cjm建议传递“--login”的问题是您的 shell 将始终在您的主目录中启动。但是,如果您正在编辑一个文件并点击“Mx shell”,则您希望您的 shell 位于该文件的目录中。此外,我已经用“Mx grep”和“Mx compile”测试了这个设置。我怀疑这里的其他示例由于目录和 PATH 问题而无法与这些示例一起使用。
This elisp snippet belongs in your ~/.emacs file. If you want to use Cygwin instead of MinGW, change the first string to C:/cygwin/bin/bash. The second string is prepended to your Windows PATH (after converting that PATH to an appropriately unixy form); in Cygwin you probably want "~/bin:/usr/local/bin:/usr/bin:" or something similar.
这个 elisp 片段属于你的 ~/.emacs 文件。如果要使用 Cygwin 而不是 MinGW,请将第一个字符串更改为 C:/cygwin/bin/bash。第二个字符串被添加到您的 Windows PATH(在将该 PATH 转换为适当的 unixy 形式之后);在 Cygwin 中,您可能想要“~/bin:/usr/local/bin:/usr/bin:”或类似的东西。
回答by Ken Gentle
I use XEmacs with Cygwin, and can run bash from XEmacs relatively easily.
我将 XEmacs 与 Cygwin 一起使用,并且可以相对轻松地从 XEmacs 运行 bash。
Here's the relevant section from init.el
这是来自的相关部分 init.el
;; Let's use CYGWIN bash...
;;
(setq binary-process-input t)
(setq w32-quote-process-args ?\")
(setq shell-file-name "bash") ;; or sh if you rename your bash executable to sh.
(setenv "SHELL" shell-file-name)
(setq explicit-shell-file-name shell-file-name)
(setq explicit-sh-args '("-login" "-i"))
回答by Ken Gentle
One more important hint on this subject.
关于这个主题的另一个重要提示。
If you use Emacs shell mode and want both bash and cmd sometimes, set it up to use bash by default, because you can type cmd at bash and the resulting dos shell works just fine.
如果您使用 Emacs shell 模式并且有时需要 bash 和 cmd,请将其设置为默认使用 bash,因为您可以在 bash 中键入 cmd 并且生成的 dos shell 工作正常。
If you setup Emacs to use cmd as the shell (which is the way NT emacs installs), then dos shell works fine, but if you type bash or bash -i to run a bash shell, the resulting shell doesn't work right - see answer 0.
如果您将 Emacs 设置为使用 cmd 作为 shell(这是 NT emacs 安装的方式),那么 dos shell 可以正常工作,但是如果您键入 bash 或 bash -i 来运行 bash shell,则生成的 shell 无法正常工作 -见答案 0。
But it works fine if bash is the "first" shell emacs invokes.
但是如果 bash 是 emacs 调用的“第一个”shell,它就可以正常工作。
回答by Ray
You can run bash directly from the default Windows command-line shell within your Emacs *shell* buffer:
您可以直接从 Emacs *shell* 缓冲区内的默认 Windows 命令行 shell 运行 bash:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\temp>bash
bash
However, no command prompt is visible which can be disorienting resulting in your commands and their output results all blending together.
但是,没有命令提示符是可见的,这可能会导致您的命令及其输出结果混合在一起,这可能会使您迷失方向。
In addition, for some unknown reason, if you do enter a command and hit return, a return line character (\r) is appended to the end of your command statement causing a bash error:
此外,由于某些未知原因,如果您确实输入命令并按回车键,则会在命令语句的末尾附加一个回车行字符 (\r),从而导致 bash 错误:
ls
bash: line 1: $'ls\r': command not found
A workaround is to manually add a comment character (#) at the end of every command which effectively comments out the \r text:
一种解决方法是在每个命令的末尾手动添加一个注释字符 (#),从而有效地注释掉 \r 文本:
ls #
myfile,txt
foo.bar
anotherfile.txt
This overall approach is far from ideal but might be useful if you want to drop into bash from Windows' native shell to do some quick operations and then exit out to continue working in Windows.
这种整体方法远非理想,但如果您想从 Windows 的本机 shell 进入 bash 以执行一些快速操作,然后退出以继续在 Windows 中工作,可能会很有用。
回答by Yoo
I'm using EmacsW32. C-h a shell$
gives a list of shell launching commands and the commands cmd-shell and cygwin-shell look interesting. Both commands need EmacsW32. They are also found in the menu: Tools > W&32 Shells.
我正在使用 EmacsW32。C-h a shell$
给出了一个 shell 启动命令列表,命令 cmd-shell 和 cygwin-shell 看起来很有趣。这两个命令都需要 EmacsW32。它们也可以在菜单中找到:工具 > W&32 壳。
If you run cygwin-shell for the first time, and if you have not setup cygwin path in Emacs, it leads you to the Customization page where you can setup the cygwin path by pressing Find button.
如果您是第一次运行 cygwin-shell,并且您还没有在 Emacs 中设置 cygwin 路径,它会引导您进入自定义页面,您可以在其中按 Find 按钮设置 cygwin 路径。
回答by Inno
Since these approaches didn't work for me I got it the following way:
由于这些方法对我不起作用,我通过以下方式得到它:
(I'm using NTEmacs which opens a dos shell by default, so perhaps your emacs behaves the same)
(我使用的是默认情况下打开 dos shell 的 NTEmacs,所以也许你的 emacs 表现相同)
Create a windows environment variable named SHELL ('SHELL' not '$SHELL') and give it the path to bash.exe of your cygwin installation (for example c:\programs\cygwin\bin\bash.exe)
创建一个名为 SHELL('SHELL' 而不是 '$SHELL')的 Windows 环境变量,并为其指定 cygwin 安装的 bash.exe 的路径(例如 c:\programs\cygwin\bin\bash.exe)
Now when doing M-x shell it opens a bash.
现在,在执行 Mx shell 时,它会打开一个 bash。
Regards,
问候,
Inno
英诺
回答by mcheema
In addition to @Chris Jones' answer about avoiding the --login argument to bash, I set the following command line arguments:
除了@Chris Jones 关于避免将 --login 参数传递给 bash 的回答之外,我还设置了以下命令行参数:
(setq explicit-bash-args '("--noediting" "-i"))
The --noediting option prevents interference with the GNU readline library and the -i option specifies that the shell is interactive. I also use the .emacs_bash file in my home directory for any emacs specific bash customizations.
--noediting 选项可防止干扰 GNU readline 库,-i 选项指定 shell 是交互式的。我还将主目录中的 .emacs_bash 文件用于任何 emacs 特定的 bash 自定义。