Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emacsにシェルの環境変数を取り込む #18

Closed
zonuexe opened this issue Feb 17, 2013 · 10 comments
Closed

Emacsにシェルの環境変数を取り込む #18

zonuexe opened this issue Feb 17, 2013 · 10 comments

Comments

@zonuexe
Copy link
Member

zonuexe commented Feb 17, 2013

purcell/exec-path-from-shell: Make Emacs use the $PATH set up by the user's shellを使いましょう。


「こんな拡張書いたけど、見て欲しい」な宣伝なのですが ヾ(〃><)ノ゙☆

他の方は Emacs で、たとへば PATH 環境変数や exec-path などをどう管理してるのか、も気になります。

@syohex
Copy link
Member

syohex commented Feb 17, 2013

私は .zshrcに以下のようなコードを仕込んで、.zshrcをロードする度に環境変数を設定する
S式を生成し、init.elでそれを読んでいます。

## create emacs env file
perl -wle \
    'do { print qq/(setenv "$_" "$ENV{$_}")/ if exists $ENV{$_} } for @ARGV' \
    PATH  VIRTUAL_ENV > ~/.emacs.d/shellenv.el

そうすると、以下のようなものが出てくるので、

(setenv "PATH" "/home/syohei/.cabal/bin:/home/syohei/virtual_env/bin:/home/syohei/.rbenv/shims:/home/syohei/.rbenv/bin:/home/syohei/perl5/perlbrew/bin:/home/syohei/perl5/perlbrew/perls/perl-5.16.1/bin:/home/syohei/.cabal/bin:/home/syohei/virtual_env/bin:/home/syohei/.nvm/v0.8.3/bin:/home/syohei/.rbenv/shims:/home/syohei/.rbenv/bin:/home/syohei/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
(setenv "VIRTUAL_ENV" "/home/syohei/virtual_env")

init.elで以下のように読み込んでいます。

;; load environment value
(load-file (expand-file-name "~/.emacs.d/shellenv.el"))
(dolist (path (reverse (split-string (getenv "PATH") ":")))
  (add-to-list 'exec-path path))

以下は昔書いたブログエントリです。

シェルと Emacsの PATHを共有するComments

@h12o
Copy link

h12o commented Feb 18, 2013

�$B$O$8$a$^$7$F!":YLn1QJ~$H?=$7$^$9!#�(B

�$B;w$FHs$J$kG:$_$NOC$G$O$"$j$^$9$,!"�(B
bash�$B$H�(BEmacs�$B$G�(BPATH�$B$,0c$&$3$H$,$J$$$h$&$K$9$k$H$$$&0UL#$G!"�(B
�$B;d$O0J2<$N�(BURL�$B$K=q$$$?$h$&$J$3$H$r$7$F$$$^$9!#�(B

2013/2/18 Syohei YOSHIDA notifications@github.com

�$B;d$O�(B .zshrc�$B$K0J2<$N$h$&$J%3!<%I$r;E9~$s$G!"�(B.zshrc�$B$r%m!<%I$9$kEY$K4D6-JQ?t$r@_Dj$9$k�(B
S�$B<0$r@8@.$7!"�(Binit.el�$B$G$=$l$rFI$s$G$$$^$9!#�(B

create emacs env file

perl -wle
'do { print qq/(setenv "$" "$ENV{$}")/ if exists $ENV{$_} } for @argv'
PATH VIRTUAL_ENV > ~/.emacs.d/shellenv.el

�$B$=$&$9$k$H!"0J2<$N$h$&$J$b$N$,=P$F$/$k$N$G!"�(B

(setenv "PATH" "/home/syohei/.cabal/bin:/home/syohei/virtual_env/bin:/home/syohei/.rbenv/shims:/home/syohei/.rbenv/bin:/home/syohei/perl5/perlbrew/bin:/home/syohei/perl5/perlbrew/perls/perl-5.16.1/bin:/home/syohei/.cabal/bin:/home/syohei/virtual_env/bin:/home/syohei/.nvm/v0.8.3/bin:/home/syohei/.rbenv/shims:/home/syohei/.rbenv/bin:/home/syohei/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
(setenv "VIRTUAL_ENV" "/home/syohei/virtual_env")

init.el�$B$G0J2<$N$h$&$KFI$_9~$s$G$$$^$9!#�(B

;; load environment value
(load-file (expand-file-name "~/.emacs.d/shellenv.el"))
(dolist (path (reverse (split-string (getenv "PATH") ":")))
(add-to-list 'exec-path path))

�$B0J2<$O@N=q$$$?%V%m%0%(%s%H%j$G$9!#�(B

�$B%7%'%k$H�(B Emacs�$B$N�(B PATH�$B$r6&M-$9$k�(BCommentshttp://d.hatena.ne.jp/syohex/20111117/1321503477

�$B!=�(B
Reply to this email directly or view it on GitHubhttps://github.com//issues/18#issuecomment-13697644.


�$B:YLn�(B �$B1QJ~�(B (HOSONO Hidetomo), CISSP
URL: http://h12o.blessedgeeks.jp/
http://h12o.blessedgeeks.org/
---------------------------------------------------------<')))><

@syohex
Copy link
Member

syohex commented Feb 18, 2013

@h12o メールから返信する際は UTF-8で encodingしないと文字化けするそうです。

@h12o
Copy link

h12o commented Feb 18, 2013

なんと! 失礼しました。以下改めて。


はじめまして、細野英朋と申します。

似て非なる悩みの話ではありますが、
bashとEmacsでPATHが違うことがないようにするという意味で、
私は以下のURLに書いたようなことをしています。

2013/2/18 Syohei YOSHIDA notifications@github.com

私は .zshrcに以下のようなコードを仕込んで、.zshrcをロードする度に環境変数を設定する
S式を生成し、init.elでそれを読んでいます。

create emacs env file

perl -wle
'do { print qq/(setenv "$" "$ENV{$}")/ if exists $ENV{$_} } for @argv'
PATH VIRTUAL_ENV > ~/.emacs.d/shellenv.el

そうすると、以下のようなものが出てくるので、

(setenv "PATH" "/home/syohei/.cabal/bin:/home/syohei/virtual_env/bin:/home/syohei/.rbenv/shims:/home/syohei/.rbenv/bin:/home/syohei/perl5/perlbrew/bin:/home/syohei/perl5/perlbrew/perls/perl-5.16.1/bin:/home/syohei/.cabal/bin:/home/syohei/virtual_env/bin:/home/syohei/.nvm/v0.8.3/bin:/home/syohei/.rbenv/shims:/home/syohei/.rbenv/bin:/home/syohei/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
(setenv "VIRTUAL_ENV" "/home/syohei/virtual_env")

init.elで以下のように読み込んでいます。

;; load environment value
(load-file (expand-file-name "~/.emacs.d/shellenv.el"))
(dolist (path (reverse (split-string (getenv "PATH") ":")))
(add-to-list 'exec-path path))

以下は昔書いたブログエントリです。

シェルと Emacsの PATHを共有するCommentshttp://d.hatena.ne.jp/syohex/20111117/1321503477


Reply to this email directly or view it on GitHubhttps://github.com//issues/18#issuecomment-13697644.


細野 英朋 (HOSONO Hidetomo), CISSP
URL: http://h12o.blessedgeeks.jp/
http://h12o.blessedgeeks.org/
---------------------------------------------------------<')))><

@kawabata
Copy link
Member

環境変数のうち、non-interactive な環境でも必要なものは .environ に記述して
.zshenv でsourceし、それを

(defun sync-env ()
  "シェルの環境変数をEmacsの環境変数を同期させる。"
  (interactive)
  (let ((env (shell-command-to-string "source ~/.zshenv; env")))
    (parse-env env)))

と、

(defun parse-env (env)
  "'env'命令の実行結果ENVの内容をEmacsに反映させる。"
  (dolist (env (split-string env "\n"))
    (when (string-match "^\\([A-Z][_A-Za-z0-9]+\\)=\\(.+\\)" env)
      (let* ((env-name (match-string 1 env))
             (env-val (match-string 2 env))
             (old-val (getenv env-name)))
        (unless (or (equal env "SHLVL") ; 例外
                    (equal env "TERM")  ; 例外
                    (equal env-val old-val))
          (message "%s (%s) is updated to %s" env-name old-val env-val)
          (pcase env
            ("PATH" (dolist (path (reverse (split-string env-val ":")))
                      (add-to-list 'exec-path path)))
            ("INFOPATH" (dolist (path (reverse (split-string env-val ":")))
                          (add-to-list 'Info-directory-list path))))
          (setenv env-name env-val))))))

の組み合わせで取り込んでいます。

@tomoya
Copy link
Member

tomoya commented Feb 20, 2013

僕の場合は、少し @syohex さんに似てますが、単純に shell-command-to-string で echo $PATH を叩いているだけです。

;;; ターミナルコマンドの実行パスを追加
;; exec-pathリストにパスを追加する
(loop for x in (reverse
                (split-string (substring (shell-command-to-string "echo $PATH") 0 -1) ":"))
      do (add-to-list 'exec-path x))

@takaxp
Copy link
Member

takaxp commented Apr 23, 2018

この話題って,https://github.com/purcell/exec-path-from-shell 使えばOKでOKでしたっけ?

@zonuexe
Copy link
Member Author

zonuexe commented Apr 23, 2018

はい、そうです

@takaxp
Copy link
Member

takaxp commented Apr 23, 2018

通りがかっておいてなんですが,Close したい...w

@zonuexe zonuexe closed this as completed Apr 23, 2018
@kawabata
Copy link
Member

NTEmacs で、かつ shell-file-name が、msys/cygwin の "bash.exe" または "zsh.exe" の場合は、
":"で split したあと、cygpath -amp で変換しなければうまくいかなかったような。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants