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?
into a CygWin terminal type the command ls /bin/bash.exe : it list the executable for bash. open a windows CMD and type the command dir C:\cygwin\bin\bash.exe : it list the executable for bash.
Try editing /etc/nsswitch. The only catch is that you have to restart Cygwin. If you do use mkpasswd after this change, it will use your new default shell for all users that are allowed to log on.
description: Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003. 2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With