2011-06-20 9 views
11

私はいくつかのコマンドを実行するために私の$PATHをEmacsで使う必要があります。 Emacsにはどのように使用させることができますか?私はUbuntuのリポジトリからEmacsをインストールしました。Emacsにmy .bashrcファイルを使用させる方法は?

+0

http://stackoverflow.com/questions/1393400/commands-from-bashrc-not-available-in-emacs –

+0

[この回答](https://emacs.stackexchange.com/a/29120/8849)完璧な解決策のようです。 –

答えて

29

はここに私のGUI Emacsはいつも私がシェルの中に入る同じ$PATH見ていることを確認するためにトリックI useです:OS X上で、

(defun set-exec-path-from-shell-PATH() 
    (let ((path-from-shell (replace-regexp-in-string 
          "[ \t\n]*$" 
          "" 
          (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'")))) 
    (setenv "PATH" path-from-shell) 
    (setq eshell-path-env path-from-shell) ; for eshell users 
    (setq exec-path (split-string path-from-shell path-separator)))) 

(when window-system (set-exec-path-from-shell-PATH)) 

具体的には、グラフィカルEmacsは、ユーザーのシェルの定義をピックアップしません。 $PATHなので、このトリックはそのプラットフォームに役立ちます。

更新:このコードはexec-path-from-shellと呼ばれるelispライブラリとして公開され、インストール可能なパッケージはMELPAで利用できます。あなたはこのようなUbuntuので

# /etc/profile.d/path.sh 
export PATH="$PATH:/usr/local" 

とを/etc/profile.dするパス設定を追加することができます

+0

これは、ubuntu 13.04の同じ問題を修正しました(私はパッケージマネージャのインストールを使用しました) – Anake

+0

エイリアスを使用していないようです。名前のせいでそれを期待しないだろう。通常のbashのエイリアスも取得するための何かがありますか? – Zelphir

+0

Emacsはあなたのbashエイリアスを決して使用しません、ごめんなさい:これらはbashセッションでのみ利用できます。このコードとシェルからのexec-pathパッケージは、Emacsがbashセッションで見えるのと同じ実行可能ファイルとスクリプトを確実に見つけられるようにするだけです。 – sanityinc

0

$PATHがemacsを起動する端末に設定されている場合は、コマンドM-! <your command> RETでシステムコマンドを実行できます。

1

あなたの環境変数が取得されていない場合は、emacsの起動方法が原因である可能性があります。メニュー項目などを確認してemacsbash -c emacsに変更してみてください。

1

、私はすべてのセッションのソースあなたの~/.xsessionrcを覚えているので、あなたはまた、GUIアプリケーションのために、このファイルにパスを設定することができます。

1

私は自分の.bash_profileを調達するのに似たようなものを思いついた。あなたがPATHだけを気にしているなら、上記の答えの一つはより簡単です。

source ~/.bash_profile ; echo post-env; envを実行してから "post-env"の前にすべてをスローし、 "env"コマンドが出力する "key = value"形式の値を解析します。

おそらくすべてのケースを完全には処理しませんが、十分に機能します。 -

;;------------------------------------------------------- 
;; begin sourcing of .bash_profile 

;; only do this on Mac OS X 
(when (string= system-type "darwin") 
    ;; require common lisp extensions, for search 
    (require 'cl) 


    (defun src-shell-unescape (string) 
    ;; replace \n \t \r \b \a \v \\ 
    ;; and octal escapes of the form \0nn 

    (replace-regexp-in-string 
    "\\\\\\([ntrbav]\\|\\(\\\\\\)\\|\\(0[0-7][0-7]\\)\\)" 
    (lambda (str) 
     ;; interpret octal expressions 
     ;; of the form "\0nn" 
     (let ((char1 (aref str 1))) 
    (cond ((= ?0 (aref str 1)) 
     (byte-to-string 
     (+ (* (- (aref str 2) ?0) 8) 
      (- (aref str 3) ?0)))) 
      ((eq char1 ?n) "\n") 
      ((eq char1 ?t) "\t") 
      ((eq char1 ?r) "\r") 
      ((eq char1 ?b) "\b") 
      ((eq char1 ?a) "\a") 
      ((eq char1 ?v) "\v") 
      ((eq char1 ?\\) "\\\\") 
      (t "")))) string)) 

    (defun src-set-environment-from-env-output(env-output) 
    ;; set the environment from shell's "env" output 
    (let ((lines (split-string env-output "\n" t))) 
     (dolist (line lines) 
    (let ((idx-equals (search "=" line))) 
     (when (and (not (eq idx-equals nil)) 
      (> idx-equals 1)) 
     (let ((key (substring line 0 idx-equals)) 
      (value (substring line (+ idx-equals 1)))) 
      (setenv key (src-shell-unescape value)) 
      ;; (message "%s = %s" key value) 
     )))))) 

    (defun src-source-shell-file (file-name) 
    ;; if your shell is sh rather than bash, the "source " may need 
    ;; to be ". " instead 
    (let* ((command (concat "source '" file-name "'; echo 'post-env'; env")) 
     (output (shell-command-to-string command)) 
     (idx-post-env (search "post-env" output))) 
     (if (eq nil idx-post-env) 
     (message "Didn't find expected output after sourcing %s. Found: %s" file-name output) 
    (let ((trimmed-output (substring output idx-post-env))) 
     ;; (message "trimmed-output: %s" trimmed-output) 
     (src-set-environment-from-env-output trimmed-output))))) 



    (src-source-shell-file (expand-file-name "~/.bash_profile"))) 


;; end sourcing of .bash_profile 
;;------------------------------------------------------- 
1

Aより一般的な解決策https://stackoverflow.com/a/12229404/1190077に与えられている(だけでなく、PATHは、すべての変数とエイリアスを設定するには)キーを設定することです:デフォルト"-c"(実行を無効にします

(setq shell-command-switch "-ic") 

以下のコマンド)。 "-i"を追加すると、bashシェルは対話モードになり、ソースは~/.bashrcになります。

関連する問題