How to best integrate Emacs and Cygwin?

Viewed 37999

There are a ton of ways to integrate Cygwin with Emacs on Windows. EmacsWiki shows a few ideas. Here are the options that I've found:

  1. Use the Emacs that comes with Cygwin. (Then find a way to get to cmd.exe if you want it.)
  2. Use NTEmacs and Cygwin as a "sometimes" shell. (A special command to launch Cygwin)
  3. Use NTEmacs and Cygwin as the "always" shell. (M-x shell launches cygwin)

To give some clarity as to "why". Here are a couple of examples where I wish I had Cygwin with Emacs:

  1. M-x whois doesn't work on NTEmacs.
  2. Packages like Wanderlust include a Makefile that isn't Windows friendly.
  3. Sometimes I just need a bash shell for something.

So which option of Emacs/Cygwin works well for others? Also, has anyone tried MSYS integration successfully?

12 Answers

A fourth choice is to run emacs in one of Cygwin's alternative terminals: (u)rxvt, mintty, xterm. These all offer much better terminal emulation than the console, which means a much improved emacs experience.

I tend to use the native version of emacs on windows in conjunction with the (also native) ports of gnu utils, which are much faster, though less complete, than the Cygwin ones.

Then I just use a cygwin window for the bash shell and the things that are missing.

I installed cygwin on the root. It explicitly warns you against it, but I saw no ill effects. (I found that piece of advice from Steve Yegge).

I also installed cygwin-mount, which helps a bunch. Also, I had to switch from ispell to aspell. Finally, a little bit of .emacs tweaking, and I was good to go:

;;;;;;;;;;;;;;;;;;;;;;
;;CygForTheWin
;;*cygwin
(when (equal system-type 'windows-nt) 
(message "Setting up Cygwin...")
(let* ((cygwin-root "c:")
       (cygwin-bin (concat cygwin-root "/bin"))
       (gambit-bin "/usr/local/Gambit-C/4.0b22/bin/")
       (snow-bin "/usr/local/snow/current/bin")
       (mysql-bin "/wamp/bin/mysql/mysql5.0.51a/bin/"))
   (setenv "PATH" (concat cygwin-bin ";" ;
                          snow-bin ";" 
                          gambit-bin ";"
                          mysql-bin ";"
                          "c:/usr/local/jdk1.60_03/bin/"
                          ".;")  
           (getenv "PATH"))
   (setq exec-path (cons cygwin-bin exec-path)))

(require 'cygwin-mount)
(cygwin-mount-activate)

(setq shell-file-name "bash")
(setq explicit-shell-file-name "bash")

(defun jonnay-cygwin-shell ()
  "Wrapper around cygwin-shell so that it doesn't throw an error"
  (interactive)
  (condition-case e
   (cygwin-shell)
   (message "There was an error trying to launch the shell: %s" e)))

(message "Setting up Cygwin...Done")


;; found from the manual, check, use and make go?
 (defun my-shell-setup ()
   "For Cygwin bash under Emacs 20"
   (setq comint-scroll-show-maximum-output 'this)
   (setq comint-completion-addsuffix t)
   (setq comint-eol-on-send t)
   (setq w32-quote-process-args ?\")
   (make-variable-buffer-local 'comint-completion-addsuffix))

(setq shell-mode-hook 'my-shell-setup)
(add-hook 'emacs-startup-hook 'jonnay-cygwin-shell)
)

I'm using wsl2 with the win32 27.1 distribution. I followed the instructions here: https://www.emacswiki.org/emacs/NTEmacsWithCygwin The only change I made was this:

(let* ((cygwin-root "//wsl$/Ubuntu/")

Where Ubuntu is your distribution.

I don't have the proper /mnt/c working yet.

I like the xemacs version that you get from the Cygwin setup program. Works out of the box, the Alt key maps to Meta just fine, and the bash shell buffer is there whenever you need it.

The third option. I am using NTEmacs + EmacsW32 + Cygwin. This way I have a native Windows application with Unix utilities and bash shell. The makefiles work, I have never used M-x whois though. I cannot think of any immediate drawbacks of this setup.

Here are interesting bits of my setup, basically very similar to http://www.emacswiki.org/emacs/NTEmacsWithCygwin:

(setq cygwin-root "c:/cygwin/")
(setq private-bin (concat home-dir "/usr/bin"))
(setq exec-path (cons private-bin exec-path))
(setenv "PATH" (concat private-bin ";" (getenv "PATH")))
; Add Cygwin Emacs stuff
(add-to-load-path "/usr/share/emacs/site-lisp")
; Add Cygwin Info pages
(add-to-list 'Info-default-directory-list
             (concat cygwin-root "usr/share/info/"))

(setq shell-file-name "bash")
(setenv "SHELL" shell-file-name)
(setq explicit-shell-file-name shell-file-name)

(setq w32shell-cygwin-bin "c:\\cygwin\\bin")
(require 'w32shell)
(w32shell-add-emacs)
(w32shell-set-shell "cygwin")
Related