如何从 Emacs 中运行 Cygwin Bash Shell?

发布于 2024-07-08 15:47:55 字数 183 浏览 13 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

剩余の解释 2024-07-15 15:47:55

shell-file-name 是控制 Emacs 在运行 shell 命令时使用哪个 shell 的变量。

explicit-shell-file-name 是控制启动哪个 shell Mx shell 的变量。

Ken 的答案改变了这两个问题,您可能想要也可能不想要。

您还可以通过临时更改 explicit-shell-file-name 来启动不同的 shell 的函数:

(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来做到这一点。 (请注意,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 shell M-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:

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

情话墙 2024-07-15 15:47:55

我发现的最佳解决方案如下:

;; 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"))))))

将“--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:

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

梦回梦里 2024-07-15 15:47:55

我将 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"))

I use XEmacs with Cygwin, and can run bash from XEmacs relatively easily.

Here's the relevant section from 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"))
饮惑 2024-07-15 15:47:55

关于这个主题还有一个重要提示。

如果您使用 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.

下雨或天晴 2024-07-15 15:47:55

您可以直接从 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: 

解决方法是手动添加注释字符 ( #) 在每个命令的末尾,有效地注释掉 \r 文本:

ls #
myfile,txt
foo.bar
anotherfile.txt

这种整体方法远非理想,但如果您想从 Windows 的本机 shell 进入 bash 执行一些快速操作,然后退出到继续在 Windows 中工作。

ls\r': command not found

解决方法是手动添加注释字符 ( #) 在每个命令的末尾,有效地注释掉 \r 文本:

这种整体方法远非理想,但如果您想从 Windows 的本机 shell 进入 bash 执行一些快速操作,然后退出到继续在 Windows 中工作。

You can run bash directly from the default Windows command-line shell within your Emacs *shell* buffer:

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:

ls
bash: line 1: 

A workaround is to manually add a comment character (#) at the end of every command which effectively comments out the \r text:

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.

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:

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.

夏尔 2024-07-15 15:47:55

我正在使用 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.

空名 2024-07-15 15:47:55

由于这些方法对我不起作用,我通过以下方式得到它:(

我使用的是 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

寂寞陪衬 2024-07-15 15:47:55

除了 @Chris Jones 关于避免 bash 的 --login 参数的回答之外,我还设置了以下命令行参数:

 (setq explicit-bash-args '("--noediting" "-i"))

--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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文