我在 Windows 上运行 GNU Emacs,所以输入:
M-x shell
启动 Windows 命令行 DOS shell。但是,我希望能够从 Emacs 中运行 Cygwin Bash Shell(或任何其他非 Windows shell)。这怎么能很容易做到呢?
我在 Windows 上运行 GNU Emacs,所以输入:
M-x shell
启动 Windows 命令行 DOS shell。但是,我希望能够从 Emacs 中运行 Cygwin Bash Shell(或任何其他非 Windows shell)。这怎么能很容易做到呢?
shell-file-name
是控制 Emacs 在要运行 shell 命令时使用哪个 shell 的变量。
explicit-shell-file-name
是控制哪个 shellM-x shell
启动的变量。
肯的回答改变了这两个,你可能想要也可能不想要。
您还可以通过临时更改来拥有一个启动不同 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)))
您可能还希望将--login
参数传递给 bash,因为您正在开始一个新的 Cygwin 会话。你可以通过设置来做到这一点explicit-bash-args
。(请注意,M-x shell
使用explicit-
PROGRAM ,其中 PROGRAM 是 shell 路径名的文件名部分。这就是为什么在设置 shell 时-args
不应包含 的原因。.exe
我发现的最佳解决方案如下:
;; 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"))))))
像 cjm建议的那样传递“--login”的问题是您的 shell 将始终在您的主目录中启动。但是如果你正在编辑一个文件并且你点击了“Mx shell”,你希望你的shell在那个文件的目录中。此外,我用“Mx grep”和“Mx compile”测试了这个设置。我怀疑这里的其他示例由于目录和 PATH 问题而无法与这些示例一起使用。
这个 elisp 片段属于您的 ~/.emacs 文件。如果您想使用 Cygwin 而不是 MinGW,请将第一个字符串更改为 C:/cygwin/bin/bash。第二个字符串被添加到您的 Windows PATH 之前(在将该 PATH 转换为适当的 unixy 形式之后);在 Cygwin 中,您可能想要 "~/bin:/usr/local/bin:/usr/bin:" 或类似的东西。
我将 XEmacs 与 Cygwin 一起使用,并且可以相对容易地从 XEmacs 运行 bash。
这是来自的相关部分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"))
关于这个主题的另一个重要提示。
如果您使用 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,它就可以正常工作。
您可以直接从 Emacs *shell* 缓冲区中的默认 Windows 命令行 shell 运行 bash:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\temp>bash
bash
但是,没有命令提示符可见,这可能会导致您的命令及其输出结果混合在一起而迷失方向。
此外,由于某些未知原因,如果您确实输入了命令并按回车键,则会在命令语句的末尾附加一个回车符 (\r),从而导致 bash 错误:
ls
bash: line 1: $'ls\r': command not found
一种解决方法是在每个命令的末尾手动添加一个注释字符 (#),从而有效地注释掉 \r 文本:
ls #
myfile,txt
foo.bar
anotherfile.txt
这种整体方法远非理想,但如果您想从 Windows 的本机 shell 进入 bash 以执行一些快速操作,然后退出以继续在 Windows 中工作,则可能会很有用。
我正在使用 EmacsW32。C-h a shell$
给出了一个 shell 启动命令列表,命令 cmd-shell 和 cygwin-shell 看起来很有趣。这两个命令都需要 EmacsW32。它们也可以在菜单中找到:Tools > W&32 Shells。
如果你是第一次运行 cygwin-shell,并且你没有在 Emacs 中设置 cygwin 路径,它会引导你到 Customization 页面,你可以通过按 Find 按钮设置 cygwin 路径。
由于这些方法对我不起作用,我通过以下方式得到它:
(我使用的是 NTEmacs,它默认打开一个 dos shell,所以也许你的 emacs 行为相同)
创建一个名为 SHELL('SHELL' 而不是 '$SHELL')的 Windows 环境变量,并为其提供 cygwin 安装的 bash.exe 的路径(例如 c:\programs\cygwin\bin\bash.exe)
现在在做 Mx shell 时,它会打开一个 bash。
问候,
英诺
除了@Chris Jones 关于避免 bash 的 --login 参数的回答之外,我还设置了以下命令行参数:
(setq explicit-bash-args '("--noediting" "-i"))
--noediting 选项可防止干扰 GNU readline 库,而 -i 选项指定 shell 是交互式的。我还将主目录中的 .emacs_bash 文件用于任何 emacs 特定的 bash 自定义。