Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何从Emacs中运行Cygwin Bash Shell?_Windows_Shell_Command Line_Emacs_Cygwin - Fatal编程技术网

Windows 如何从Emacs中运行Cygwin Bash Shell?

Windows 如何从Emacs中运行Cygwin Bash Shell?,windows,shell,command-line,emacs,cygwin,Windows,Shell,Command Line,Emacs,Cygwin,我正在Windows上运行GNU Emacs,因此输入: M-x shell 启动Windows命令行DOS shell。但是,我希望能够从Emacs中运行Cygwin Bash Shell(或任何其他非Windows Shell)。这怎么容易做到呢?我将XEMAC与Cygwin一起使用,并且可以相对容易地从XEMAC运行bash 以下是init.el ;; Let's use CYGWIN bash... ;; (setq binary-process-input t) (setq w32

我正在Windows上运行GNU Emacs,因此输入:

M-x shell

启动Windows命令行DOS shell。但是,我希望能够从Emacs中运行Cygwin Bash Shell(或任何其他非Windows Shell)。这怎么容易做到呢?

我将XEMAC与Cygwin一起使用,并且可以相对容易地从XEMAC运行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"))

shell文件名
是一个变量,用于控制shell Emacs在希望运行shell命令时使用的命令

explicit shell file name
是控制哪个shell
M-x shell
启动的变量

更改这两个选项,您可能需要也可能不需要

您还可以使用一个函数,通过临时更改显式shell文件名来启动不同的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会话。您可以通过设置
显式bash args
来实现这一点。(请注意,
M-x shell
使用
explicit-
PROGRAM
-args
,其中PROGRAM是shell路径名的文件名部分。这就是为什么在设置shell时不应包含
.exe

可以直接从Emacs*shell*缓冲区内的默认Windows命令行shell运行bash:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\temp>bash
bash     
但是,没有可见的命令提示,这可能会使您的命令和它们的输出结果混淆在一起

此外,由于某些未知原因,如果您输入命令并点击return,则返回行字符(\r)会附加到命令语句的末尾,从而导致bash错误:

ls
bash: line 1: $'ls\r': command not found
一种解决方法是在每个命令末尾手动添加注释字符(#),该命令可以有效地注释文本:

ls #
myfile,txt
foo.bar
anotherfile.txt

这种总体方法远不理想,但如果您想从Windows的本机shell进入bash进行一些快速操作,然后退出以继续在Windows中工作,则可能会很有用。

关于此主题的另一个重要提示

如果您使用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,它就可以正常工作。

因为这些方法对我不起作用,所以我通过以下方式得到了它:

(我使用的是默认情况下打开dos shell的NTEmacs,因此您的emacs可能也有相同的行为)

创建名为SHELL(“SHELL”而不是“$SHELL”)的windows环境变量,并为其提供cygwin安装的bash.exe路径(例如c:\programs\cygwin\bin\bash.exe)

现在,当执行M-xshell时,它会打开一个bash

问候,


Inno

我正在使用EmacsW32。
C-h外壳$
提供了一个外壳启动命令列表,命令cmd shell和cygwin shell看起来很有趣。这两个命令都需要EmacsW32。它们也可以在菜单中找到:工具>W&32外壳


如果您是第一次运行cygwin shell,并且没有在Emacs中设置cygwin路径,它将引导您进入自定义页面,在该页面中,您可以通过按“查找”按钮来设置cygwin路径。

我找到的最佳解决方案如下:

;; 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将始终在主目录中启动。但是,如果您正在编辑一个文件并点击“M-x shell”,则希望您的shell位于该文件的目录中。此外,我已使用“M-x grep”和“M-x compile”测试了此设置.我怀疑由于目录和路径问题,这里的其他示例无法使用这些示例


此elisp代码段属于~/.emacs文件。如果要使用Cygwin而不是MinGW,请将第一个字符串更改为C:/Cygwin/bin/bash。第二个字符串在Windows路径前面(将该路径转换为适当的unixy形式后);在Cygwin中,您可能需要“~/bin:/usr/local/bin:/usr/bin:”或者类似的东西。

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

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

--noediting选项可防止对GNU readline库的干扰,-i选项指定shell是交互式的。我还使用我的主目录中的.emacs_bash文件进行任何特定于emacs的bash自定义。

您的e-lisp比我的好-我在几年前就学会了让它以我想要的方式运行,并且从那时起就开始了忘记了它的大部分含义。我想我的代码最初是从某个常见问题中复制出来的…+1,用括号向尤达大师鞠躬。你的e-lisp将我的e-lisp从水中吹了出来。具体来说,你应该在某处添加(setq explicit bash args'(“--login”“-I”)。否则,我会收到消息“bash:ls:command not found”在使用您的函数后,您是否可以帮助解决我的bash问题:?另一种方法是从cygwin bash启动emacs,以便emacs具有与bash相同的路径。这有点麻烦,但可以通过vbs脚本来完成。我遇到了同样的问题,这对我也很有效。我将注意到,我必须关闭并重新打开Emacs,以使其生效。