简体   繁体   English

如何让Emacs使用我的.bashrc文件?

[英]How to make Emacs use my .bashrc file?

I need to use my $PATH in Emacs to run some commands. 我需要在Emacs中使用我的$PATH来运行一些命令。 How can I make Emacs use it? 如何让Emacs使用它? I installed Emacs from Ubuntu repositories. 我从Ubuntu存储库安装了Emacs。

Here's a trick I use to ensure my GUI Emacs always sees the same $PATH that I get inside a shell: 这是用来确保我的GUI Emacs始终看到我在shell中获得的相同$PATH的技巧:

(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))

Specifically, on OS X, a graphical Emacs will not pick up the user's shell's definition of $PATH , so this trick helps me on that platform. 具体来说,在OS X上,图形化的Emacs不会获取用户shell的$PATH定义,所以这个技巧可以帮助我在这个平台上。

Update: this code has now been published as an elisp library called exec-path-from-shell and installable packages are available in MELPA . 更新:此代码现已发布为名为exec-path-from-shell的elisp库, MELPA中提供了可安装的软件包。

A more general solution (to set all variables and aliases, not just PATH ) is given in https://stackoverflow.com/a/12229404/1190077 -- the key is to set: https://stackoverflow.com/a/12229404/1190077中给出了一个更通用的解决方案(设置所有变量和别名,而不仅仅是PATH ) - 关键是设置:

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

which overrides the default "-c" (execute the following command). 它会覆盖默认的"-c" (执行以下命令)。 The addition of "-i" forces the bash shell into interactive mode, which leads to the sourcing of ~/.bashrc . 添加"-i"强制bash shell进入交互模式,从而导致~/.bashrc的来源。

I came up with something similar for sourcing my .bash_profile. 我想出了类似的东西来寻找我的.bash_profile。 If you only care about PATH, one of the other answers above is simpler. 如果你只关心PATH,那么上面的其他答案之一就更简单了。

It works by executing source ~/.bash_profile ; echo post-env; env 它的工作原理是执行source ~/.bash_profile ; echo post-env; env source ~/.bash_profile ; echo post-env; env source ~/.bash_profile ; echo post-env; env then throwing away everything before "post-env", and then parsing out the values in the "key=value" format that the "env" command prints. source ~/.bash_profile ; echo post-env; env然后在“post-env”之前丢弃所有内容,然后解析出“env”命令打印的“key = value”格式的值。

It probably doesn't handle every case perfectly, but works well enough for me. 它可能无法完美地处理每个案例,但对我来说效果很好。

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

If your env vars aren't picked up it may be due to the way emacs is started. 如果你的env vars没有被选中,可能是因为emacs的启动方式。 Check the menuitem or whatever and try changing emacs to bash -c emacs . 检查menuitem或其他任何内容并尝试将emacs更改为bash -c emacs

You can add path settings to /etc/profile.d such as 您可以将路径设置添加到/etc/profile.d,例如

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

In Ubuntu, I remember all sessions source your ~/.xsessionrc , so you also can set path in this file for GUI apps. 在Ubuntu中,我记得所有会话来源~/.xsessionrc ,因此您也可以在此文件中为GUI应用程序设置路径。

There are many Emacs packages that update $PATH environment variable and the 'exec-path'. 有许多Emacs包可以更新$ PATH环境变量和'exec-path'。 That's because Emacs don't assume definitions in BASH related files, such as '~/.bashrc'. 这是因为Emacs不假设BASH相关文件中的定义,例如'〜/ .bashrc'。

All definitions you need to have in any program not executed from a terminal shell, must be moved to '~/.profile', these are loaded in the system startup. 您在任何未从终端shell执行的程序中需要的所有定义必须移动到'〜/ .profile',这些定义在系统启动时加载。

Some old systems need to manually load user profile from '/etc/profile'. 一些旧系统需要从'/ etc / profile'手动加载用户配置文件。

If the $PATH is set in the terminal you launch emacs from, you can execute system commands through the command M-! <your command> RET 如果在您启动emacs的终端中设置了$PATH则可以通过命令M-! <your command> RET执行系统命令M-! <your command> RET M-! <your command> RET . M-! <your command> RET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM