2008-10-24 62 views
37

我在Windows上運行GNU Emacs的這樣輸入:如何從Emacs中運行Cygwin Bash Shell?

M-x shell 

啓動Windows命令行DOS外殼。不過,我希望能夠在Emacs中運行Cygwin Bash Shell(或任何其他非Windows shell)。這如何輕鬆完成?

回答

42

shell-file-name相關的部分是控制,當它想運行shell命令,外殼Emacs使用的變量。

explicit-shell-file-name是控制該殼M-x shell啓動該變量。

Ken's answer改變這兩個,您可能會或可能不希望的。

您也可以通過臨時改變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))) 

你可能也想通過--login參數bash的,因爲你開始一個新的Cygwin的會議。你可以通過設置explicit-bash-args來實現。 (請注意,M-x shell使用explicit-程序-args,其中program是shell的路徑名的文件名部分,這就是爲什麼設置外殼時,你應該不包括.exe

+2

您的e-lisp比我的要好 - 我學會了足夠讓它按照我以前想要的方式運行,並且忘記了它的大部分含義。我認爲我的代碼最初是從某個常見問題解答中複製出來的...... – 2008-10-24 23:07:57

+0

+1,弓向Yoda大師揮動括號。你的e-lisp會把我從水裏吹出來。 – 2009-05-14 12:15:48

6

我用XEmacs的使用Cygwin,並可以相對容易地從運行bash的XEmacs的。

下面是​​

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

您可以從默認的Windows命令行直接運行的bash您的Emacs內殼*殼*緩衝液:

Microsoft Windows XP [Version 5.1.2600] 
(C) Copyright 1985-2001 Microsoft Corp. 

C:\temp>bash 
bash  

然而,沒有命令提示是可見的,其可以是迷失方向導致的命令及其輸出結果中的所有混合在一起

此外,對於一些unkno。 WN原因,如果你輸入一個命令並回車,返回行字符(\ r)附加到您的命令語句的結束導致一個bash錯誤:

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

一種解決方法是手動添加評論字符(#)在每個有效地註釋掉\ r文本命令的末尾:

ls # 
myfile,txt 
foo.bar 
anotherfile.txt 

這一總的做法是遠遠不夠理想,但如果你想放入的bash從Windows的本機外殼可能是有用的做一些快速操作,然後退出以繼續在Windows中工作。

5

關於這個問題的一個更重要的提示。

如果您使用Emacs shell模式,並希望雙方的bash,有時在cmd,將它設置爲默認使用bash,因爲你可以在鍵入CMD慶典,所得DOS外殼工作得很好。

如果您設置Emacs使用cmd作爲shell(這是NT emacs安裝的方式),那麼dos shell可以正常工作,但是如果您鍵入bash或bash -i來運行bash shell,則生成的shell不會「 t工作正確 - 請參閱答案0.

但它工作正常,如果bash是「第一個」殼emacs調用。

1

由於這些方法並沒有爲我工作,我得到它通過以下方式:

(我使用的默認情況下打開一個DOS外殼NTEmacs,所以也許你的emacs的行爲相同)

創建一個名爲SHELL('SHELL''''''SHELL')的Windows環境變量併爲其指定cygwin安裝的bash.exe路徑(例如c:\ programs \ cygwin \ bin \ bash.exe)

現在當做Mx shell時,它會打開一個bash。

問候,

的Inno

2

我使用EmacsW32。 C-h a shell$給出了一個shell啓動命令列表,命令cmd-shell和cygwin-shell看起來很有趣。這兩個命令都需要EmacsW32。它們也在菜單中找到:工具> W & 32殼體

如果您是第一次運行cygwin-shell,並且您沒有在Emacs中設置cygwin路徑,它會引導您進入定製頁面,您可以通過按下查找按鈕來設置cygwin路徑。

7

我發現這個最好的解決辦法是:

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

與傳遞「--login」爲CJM suggests是你的shell總是會在你的home目錄中啓動的問題。但是如果你正在編輯一個文件並且你點擊了「M-x shell」,你需要在你的文件目錄下安裝你的shell。此外,我用「M-x grep」和「M-x compile」測試了這個設置。我懷疑其他例子在這裏不適用於那些由於目錄和PATH問題。

這個elisp片段屬於你的〜/ .emacs文件。如果要使用Cygwin而不是MinGW,請將第一個字符串更改爲C:/ cygwin/bin/bash。第二個字符串預先添加到您的Windows PATH(將該PATH轉換爲適當的unixy形式之後);在Cygwin中,你可能想要「〜/ bin:/ usr/local/bin:/ usr/bin:」或類似的東西。

0

除了@克里斯·瓊斯關於避免--login參數bash的答案,我設置以下命令行參數:

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

的--noediting選項防止與GNU readline庫干擾和-i選項指定shell是交互式的。我還在我的主目錄中使用.emacs_bash文件進行任何特定於emacs的bash自定義。