How can I run Cygwin Bash Shell from within Emacs?

后端 未结 8 1628
温柔的废话
温柔的废话 2020-12-01 01:34

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 a

相关标签:
8条回答
  • 2020-12-01 01:50

    I use XEmacs with Cygwin, and can run bash from XEmacs relatively easily.

    Here's the relevant section from init.el

    ;; Let's use CYGWIN bash...
    ;;
    (setq binary-process-input t) 
    (setq w32-quote-process-args ?\") 
    (setq shell-file-name "bash") ;; or sh if you rename your bash executable to sh. 
    (setenv "SHELL" shell-file-name) 
    (setq explicit-shell-file-name shell-file-name) 
    (setq explicit-sh-args '("-login" "-i"))
    
    0 讨论(0)
  • 2020-12-01 01:54

    In addition to @Chris Jones' answer about avoiding the --login argument to bash, I set the following command line arguments:

     (setq explicit-bash-args '("--noediting" "-i"))
    

    The --noediting option prevents interference with the GNU readline library and the -i option specifies that the shell is interactive. I also use the .emacs_bash file in my home directory for any emacs specific bash customizations.

    0 讨论(0)
  • 2020-12-01 02:02

    One more important hint on this subject.

    If you use Emacs shell mode and want both bash and cmd sometimes, set it up to use bash by default, because you can type cmd at bash and the resulting dos shell works just fine.

    If you setup Emacs to use cmd as the shell (which is the way NT emacs installs), then dos shell works fine, but if you type bash or bash -i to run a bash shell, the resulting shell doesn't work right - see answer 0.

    But it works fine if bash is the "first" shell emacs invokes.

    0 讨论(0)
  • 2020-12-01 02:03

    Since these approaches didn't work for me I got it the following way:

    (I'm using NTEmacs which opens a dos shell by default, so perhaps your emacs behaves the same)

    Create a windows environment variable named SHELL ('SHELL' not '$SHELL') and give it the path to bash.exe of your cygwin installation (for example c:\programs\cygwin\bin\bash.exe)

    Now when doing M-x shell it opens a bash.

    Regards,

    Inno

    0 讨论(0)
  • 2020-12-01 02:04

    You can run bash directly from the default Windows command-line shell within your Emacs *shell* buffer:

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\temp>bash
    bash     
    

    However, no command prompt is visible which can be disorienting resulting in your commands and their output results all blending together.

    In addition, for some unknown reason, if you do enter a command and hit return, a return line character (\r) is appended to the end of your command statement causing a bash error:

    ls
    bash: line 1: $'ls\r': command not found
    

    A workaround is to manually add a comment character (#) at the end of every command which effectively comments out the \r text:

    ls #
    myfile,txt
    foo.bar
    anotherfile.txt
    

    This overall approach is far from ideal but might be useful if you want to drop into bash from Windows' native shell to do some quick operations and then exit out to continue working in Windows.

    0 讨论(0)
  • 2020-12-01 02:06

    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.

    0 讨论(0)
提交回复
热议问题