Windows, Emacs, Git Bash, and shell-command

2020-06-04 12:23发布

问题:

Windows 7. Emacs 24.3.1. Git 1.8.1.msysgit.1. I have the following in my equivalent .emacs file:

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

This works great when I want to do M-x shell: I can pop open a shell and type "ls".

However, M-x shell-command is failing. When I try to run "ls" via shell-command (which should print its output in the *Shell Command Output* buffer, according to C-h f shell-command), I get a single error message:

"Searching for program: permission denied, bash"

There are some very old suggestions on the Google about call-process and many questions on StackOverflow about getting the shell to work in Emacs. Please note that M-x shell works great, and what I'd like to work is shell-command.

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

回答1:

Try setting both variables to point to the same executable and make sure the path is in 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")


回答2:

I know this is an older question, but I found it while I was searching for help on the same issue, so here's the solution I use for my particular use case, in case it helps someone in the future.

I sync up my .emacs.d between all computers I use emacs on, which includes both Linux & Windows machines. I inserted this into my init.el file to automatically deal with this issue appropriately in a Windows environment:

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

Please note that the variable git-shell-path will need to be defined based on where you have git installed on your system. I install it in %USERPROFILE%\LocalAppInfo\apps\Git on the one Windows machine where I use it.



回答3:

Using GitBash you can use the lein script instead of the lein.bat script. Put the lein script into a directory called bin in your user account on windows. GitBash should have that bin directory on its executable PATH, so you can run lein anywhere.

With this approach you do not need any configuration in Emacs Lisp.

See my full answer