Windows、Emacs、Git Bash和shell-command

6

我的操作系统是Windows 7,编辑器是Emacs 24.3.1,版本控制工具是Git 1.8.1.msysgit.1。在我的.emacs文件中,我有以下内容:

(if (equal system-type 'windows-nt)
    (progn (setq explicit-shell-file-name
                 "C:/Program Files (x86)/Git/bin/sh.exe")
           (setq shell-file-name "bash")
           (setq explicit-sh.exe-args '("--login" "-i"))
           (setenv "SHELL" shell-file-name)
           (add-hook 'comint-output-filter-functions 'comint-strip-ctrl-m)))

当我想要执行M-x shell时,这个方法非常有效:我可以打开一个shell并输入"ls"。

然而,M-x shell-command却失败了。当我尝试通过shell-command运行"ls"(根据C-h f shell-command的说法,它应该在*Shell Command Output*缓冲区中打印其输出)时,我会收到以下错误信息:

"正在查找程序:权限被拒绝,bash"

关于call-process有一些非常古老的建议,以及许多关于如何使shell在Emacs中工作的StackOverflow问题。请注意,M-x shell功能正常,我想让的是shell-command。

(原因:https://github.com/donkirkby/live-py-plugin#installing-the-emacs-mode

3个回答

13

尝试将两个变量都设置为指向相同的可执行文件,并确保该路径在 exec-path 中:

(setq explicit-shell-file-name
      "C:/Program Files (x86)/Git/bin/bash.exe")
(setq shell-file-name explicit-shell-file-name)
(add-to-list 'exec-path "C:/Program Files (x86)/Git/bin")

在这之间,将"C:/Program Files (x86)/Git/bin"添加到Windows路径中,shell-command现在可以工作了。谢谢! - Ahmed Fasih

2

我知道这是一个比较早的问题,但我在寻找帮助解决同样问题时找到了它,所以这里给出我针对我的特定情况所使用的解决方案,以备将来有人需要。

我在使用 Emacs 的所有计算机上同步我的 .emacs.d 配置文件,包括 Linux 和 Windows 机器。为了在 Windows 环境下自动处理此问题,我在我的 init.el 文件中插入了以下内容:

;; Set Windows-specific preferences if running in a Windows environment.
(defun udf-windows-setup () (interactive)
  ;; The variable `git-shell-path' contains the path to the `Git\bin'
  ;; file on my system. I install this in
  ;; `%USERPROFILE%\LocalAppInfo\apps\Git\bin'.
  (setq git-shell-path
        (concat (getenv "USERPROFILE") "\\LocalAppInfo\\apps\\Git\\bin"))
  (setq git-shell-executable
        (concat git-shell-path "\\bash.exe"))
  (add-to-list 'exec-path git-shell-path)
  (setenv "PATH"
          (concat git-shell-path ";"
                  (getenv "PATH")))
  (message "Windows preferences set."))

(if (eq system-type 'windows-nt)
    (udf-windows-setup))

请注意,变量git-shell-path需要根据您在系统上安装git的位置进行定义。我在使用它的Windows计算机上将其安装在%USERPROFILE%\LocalAppInfo\apps\Git中。

0

使用GitBash,您可以使用lein脚本而不是lein.bat脚本。将lein脚本放入Windows用户帐户中名为bin的目录中。GitBash应该在其可执行PATH上具有该bin目录,因此您可以在任何地方运行lein

通过这种方法,您无需在Emacs Lisp中进行任何配置。

查看我的完整答案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接