如何从 Emacs 中运行 Cygwin Bash Shell?
我在 Windows 上运行 GNU Emacs,因此输入:
M-x shell
启动 Windows 命令行 DOS shell。 但是,我希望能够从 Emacs 中运行 Cygwin Bash Shell(或任何其他非 Windows shell)。 这怎么能轻易做到呢?
I am running GNU Emacs on Windows so entering:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
shell-file-name
是控制 Emacs 在运行 shell 命令时使用哪个 shell 的变量。explicit-shell-file-name
是控制启动哪个 shellMx shell
的变量。Ken 的答案改变了这两个问题,您可能想要也可能不想要。
您还可以通过临时更改
explicit-shell-file-name
来启动不同的 shell 的函数:您可能还希望将
--login
参数传递给 bash ,因为您正在开始一个新的 Cygwin 会话。 您可以通过设置explicit-bash-args
来做到这一点。 (请注意,Mx shell
使用explicit-
PROGRAM-args
,其中 PROGRAM 是 shell 路径名的文件名部分。这就是为什么您不应该这样做设置 shell 时包含.exe
。shell-file-name
is the variable that controls which shell Emacs uses when it wants to run a shell command.explicit-shell-file-name
is the variable that controls which shellM-x shell
starts up.Ken's answer changes 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
: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 settingexplicit-bash-args
. (Note thatM-x shell
usesexplicit-
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”作为 cjm 建议 传递的问题是你的 shell 总是在你的主目录中启动。 但是,如果您正在编辑文件并点击“Mx shell”,则您希望 shell 位于该文件的目录中。 此外,我还使用“Mx grep”和“Mxcompile”测试了此设置。 我怀疑由于目录和路径问题,这里的其他示例不适用于这些示例。
此 elisp 片段属于您的 ~/.emacs 文件。 如果您想使用 Cygwin 而不是 MinGW,请将第一个字符串更改为 C:/cygwin/bin/bash。 第二个字符串被添加到您的 Windows PATH 之前(将该 PATH 转换为适当的 unixy 形式之后); 在 Cygwin 中,您可能需要“~/bin:/usr/local/bin:/usr/bin:”或类似的内容。
The best solution I've found to this is the following:
The problem with passing "--login" as cjm suggests is 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.
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.
我将 XEmacs 与 Cygwin 一起使用,并且可以相对轻松地从 XEmacs 运行 bash。
这是
init.el
中的相关部分I use XEmacs with Cygwin, and can run bash from XEmacs relatively easily.
Here's the relevant section from
init.el
关于这个主题还有一个重要提示。
如果您使用 Emacs shell 模式并且有时需要 bash 和 cmd,请将其设置为默认使用 bash,因为您可以在 bash 中键入 cmd,并且生成的 dos shell 可以正常工作。
如果您将 Emacs 设置为使用 cmd 作为 shell(这是 NT emacs 的安装方式),则 dos shell 可以正常工作,但如果您键入 bash 或 bash -i 来运行 bash shell,则生成的 shell 无法正常工作 -请参阅答案 0。
但如果 bash 是 emacs 调用的“第一个”shell,则它可以正常工作。
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.
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.
But it works fine if bash is the "first" shell emacs invokes.
您可以直接从 Emacs *shell* 缓冲区中的默认 Windows 命令行 shell 运行 bash:
但是,没有可见的命令提示符,这可能会导致您的命令及其输出结果混合在一起。
此外,由于某种未知的原因,如果您输入命令并按回车键,则回车行字符 (\r) 会附加到命令语句的末尾,从而导致 bash 错误:
解决方法是手动添加注释字符 ( #) 在每个命令的末尾,有效地注释掉 \r 文本:
这种整体方法远非理想,但如果您想从 Windows 的本机 shell 进入 bash 执行一些快速操作,然后退出到继续在 Windows 中工作。
You can run bash directly from the default Windows command-line shell within your Emacs *shell* buffer:
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:
A workaround is to manually add a comment character (#) at the end of every command which effectively comments out the \r text:
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.
我正在使用 EmacsW32。 Ch a shell$ 给出了 shell 启动命令的列表,命令 cmd-shell 和 cygwin-shell 看起来很有趣。 这两个命令都需要 EmacsW32。 它们也可以在菜单中找到:工具> W&32 炮弹。
如果您是第一次运行 cygwin-shell,并且尚未在 Emacs 中设置 cygwin 路径,它会引导您进入“自定义”页面,您可以在其中按“查找”按钮设置 cygwin 路径。
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.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.
由于这些方法对我不起作用,我通过以下方式得到它:(
我使用的是 NTEmacs,它默认打开 dos shell,所以也许你的 emacs 的行为相同)
创建一个名为 SHELL 的 Windows 环境变量(“SHELL”不是'$SHELL')并为其提供 cygwin 安装的 bash.exe 的路径(例如 c:\programs\cygwin\bin\bash.exe)
现在,在执行 Mx shell 时,它会打开 bash。
问候,
伊诺
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)
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)
Now when doing M-x shell it opens a bash.
Regards,
Inno
除了 @Chris Jones 关于避免 bash 的 --login 参数的回答之外,我还设置了以下命令行参数:
--noediting 选项可防止干扰 GNU readline 库,而 -i 选项指定 shell 是交互式的。 我还使用主目录中的 .emacs_bash 文件进行任何 emacs 特定的 bash 自定义。
In addition to @Chris Jones' answer about avoiding the --login argument to bash, I set the following command line arguments:
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.