8

When I run M-x term on the Windows version of Emacs, I am prompted to run C:/Program Files/emacs/libexec/emacs/25.1/x86_64-w64-mingw32/cmdproxy.exe

New versions of Windows 10 have the Linux subsystem and its shell (which is installed at C:\Windows\System32\bash.exe ~).

Is it possible to use this executable as the shell in Emacs?

I tried just pasting that path into the Run Program: path but I get the: Spawning child process error.

Is it possible to use this shell instead on Windows Emacs?

Startec
  • 1,354
  • 1
  • 13
  • 30
  • Are you able to use the Windows bash subsystem independently from Emacs? I think that's the first step as there is some configuration required (I believe, may have changed in more recent Win10 versions). – glucas Nov 15 '17 at 16:04
  • Yes. Ubuntu, on Windows 10 runs as a separate program. In fact, I can't run it from *within* emacs – Startec Nov 16 '17 at 08:06
  • Related to https://stackoverflow.com/questions/42346758/is-it-not-possible-to-use-windows-bash-as-shell-in-emacs-for-ssh-etc – jmlorenzi Aug 17 '18 at 09:44

5 Answers5

8

In my .emacs I first define a function like so

(defun my-bash-on-windows-shell ()
  (let ((explicit-shell-file-name "C:/Windows/System32/bash.exe"))
    (shell)))

and I call at the bottom using:

(my-bash-on-windows-shell)

Note that Emacs shell mode does not handle the default colored prompt very well, however changing my PS1 to:

PS1='\u@\h:\w\$ '

in my ~/.bashrc gave me a nice readable prompt.

Bas Bossink
  • 181
  • 1
  • 3
  • I like this answer. However, I have noticed that after I kill the shell running the instance of Bash with `exit` then `Ctl-x-k` I can't open Bash again. The shell is empty and any input is just echoed back to the screen. Do have you this issue? – Startec Apr 15 '18 at 23:22
  • I've not noticed this, although I did do something slightly different - "(global-set-key [f9] 'my-bash-on-windows-shell)". I can type "exit" and the bash shall and restart it via F9 whenever needed. – SomeoneElse Mar 04 '19 at 17:35
  • One other caveat to get this working - on a 64-bit version of Windows 10, the 64-bit version of Emacs must be installed (if you're using the Chocolatey package manager then "choco install emacs" will work just fine). Some other emacs Windows builds out there are 32-bit, which won't allow this to work. – SomeoneElse Mar 07 '19 at 18:01
  • The .emacs configuration can be simplified to a single line (no function needed): "(setq explicit-shell-file-name "C:/Windows/System32/bash.exe")". M-x shell will then open bash instead of cmd.exe. I also found I had to update the prompt (PS1) for reliability. – Digicrat Feb 22 '20 at 04:16
  • That works for me. I recommend adding `(interactive)` as the second line of the `defun` to make the function a command (invokable from `M-x`). – Hugues Mar 26 '22 at 05:51
  • One final problem I have is that `C-c C-c` (which invokes the Emacs command `comint-interrupt-subjob`) and should send a `SIGINT` interrupt signal to the currently running shell command instead terminates the shell with the error message `Process shell<..> unknown signal`. – Hugues May 13 '22 at 06:47
2

Windows Emacs cannot pass pseudo terminal device file to inferior bash/zsh, which is required for such shells to work interactively. There is a software called "fakecygpty" that provides pseudo terminal to Emacs inferior shell. (https://github.com/d5884/fakecygpty/) You can use that for Windows Emacs to work with MSYS2 shells.

dualism
  • 304
  • 1
  • 6
1

I can get it to run M-x term using /windows/system32/bash.exe in Cygwin emacs-w32 (c: is my Cygwin root), but my issue is that the prompts are not displayed. Note that the environment looks almost exactly the same except extra directories /mnt/c/Windows/System32:/mnt/c/usr/local/bin:/mnt/c/bin:/mnt/c/lib/lapack in $PATH.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Does this answer the question? You don't really say *how* you "*can get it to run...*". Is the lack of prompts the only problem you see? A little more info/clarification could help. – Drew Dec 26 '17 at 21:42
  • Assuming that the question is about running bash.exe from the Windows version of emacs as opposed to a Cygwin version running on Windows, this doesn't seem to work - it just gives a "spawning child process invalid argument" error. – SomeoneElse Mar 04 '19 at 17:38
0

Windows 10 is supposed to have Bash built into it. If you are using Windows 10, I am sure that it is possible. https://msdn.microsoft.com/en-us/commandline/wsl/install_guide . It seems that there is also this if you are using other versions of Windows. https://sourceforge.net/projects/win-bash/?source=navbar . I have never used either of these, because I only use Windows at work. Good luck.

CigEmacs
  • 15
  • 5
  • If you have an older version of Windows (pre 10) and Cygwin installed, then you may be able to adapt Bas Bossink's answer to invoke Cygwin instead of the WSL Bash prompt. I suspect the "win-bash" route would be less supported going forwards. – SomeoneElse Mar 04 '19 at 17:42
0

This doesn't answer the your exact question, but instead of that, I run Emacs in WSL+X11. Then you get everything you expect in Linux, including bash, and easy access to both your Windows and Linux drives.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://emacs.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](https://emacs.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/23540) – Drew Jan 15 '23 at 18:47