LoginSignup
116
110

More than 5 years have passed since last update.

EmacsでPATHの設定が引き継がれない問題をエレガントに解決する

Posted at

Mac や Ubuntu で Emacs を GUI で立ち上げると PATH の設定が引き継がれません

そのため Emacs から外部プログラムを呼び出す際にうまくいかないという問題が発生します

今までそれを シェルと Emacsの PATHを共有する - Life is very short を参考に .zshrc 側で設定をしていました

しかし,Emacs の設定に .zshrc から書き込むというのは気持ち悪かったので変えたいと思っていました

今回は Emacs で完結する設定方法を 2 つ見つけたので紹介します

init-loader.el と package.el を導入して快適 Emacs ライフ - Qiita [キータ]

この設定がしてあることを前提に話していきます

exec-path-from-shell を使う

exec-path-from-shell

これを使います

package.el の設定をしているならそこから簡単にインストール出来ます

そして

21-exec-path-from-shell.el
(exec-path-from-shell-initialize)

と書けば PATH が引き継がれます

簡単ですね

自分で書く

適当なファイルに以下の設定をします

11-path.el
(defun set-exec-path-from-shell-PATH ()
  "Set up Emacs' `exec-path' and PATH environment variable to match that used by the user's shell.

This is particularly useful under Mac OSX, where GUI apps are not started from a shell."
  (interactive)
  (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 exec-path (split-string path-from-shell path-separator))))

(set-exec-path-from-shell-PATH)

まあ最初の方の文言は何でもいいのですが,これだけで設定ができます

まとめ

年末emacs設定大掃除をして、これは捨てられないと思った設定書いてく - $shibayu36->blog;

emacsを使っていると一度はPATHなどの設定で死ぬ思いをする気がします

とあるように,私もこの前すごく悩みました

しかし Emacs のみで PATH の設定を完結できることが分かってよかったです

116
110
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
116
110