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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6411121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:12:55  来源:igfitidea点击:

How to make Emacs use my .bashrc file?

bashemacspath

提问by S?awosz

I need to use my $PATHin Emacs to run some commands. How can I make Emacs use it? I installed Emacs from Ubuntu repositories.

我需要$PATH在 Emacs 中使用我的来运行一些命令。我怎样才能让 Emacs 使用它?我从 Ubuntu 存储库安装了 Emacs。

回答by sanityinc

Here's a trick I useto ensure my GUI Emacs always sees the same $PATHthat I get inside a shell:

这是用来确保我的 GUI Emacs 总是看到$PATH我在 shell 中看到的相同的一个技巧:

(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-shelland installable packages are available in MELPA.

更新:此代码现已发布为名为exec-path-from-shell的 elisp 库,并且可安装包在MELPA中可用。

回答by Hugues

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). The addition of "-i"forces the bash shell into interactive mode, which leads to the sourcing of ~/.bashrc.

它覆盖默认值"-c"(执行以下命令)。添加"-i"强制 bash shell 进入交互模式,这导致~/.bashrc.

回答by jaybee

If your env vars aren't picked up it may be due to the way emacs is started. Check the menuitem or whatever and try changing emacsto bash -c emacs.

如果您的 env vars 没有被选中,则可能是由于 emacs 的启动方式。检查菜单项或其他任何内容,然后尝试更改emacsbash -c emacs.

回答by Ian Yang

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 应用程序设置路径。

回答by cmore

I came up with something similar for sourcing my .bash_profile. If you only care about PATH, one of the other answers above is simpler.

我想出了类似的东西来采购我的 .bash_profile。如果您只关心 PATH,那么上面的其他答案之一会更简单。

It works by executing source ~/.bash_profile ; echo post-env; envthen 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 ##代码##nn

    (replace-regexp-in-string
     "\\\([ntrbav]\|\(\\\)\|\(0[0-7][0-7]\)\)"
     (lambda (str)
       ;; interpret octal expressions
       ;; of the form "##代码##nn"
       (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
;;-------------------------------------------------------

回答by aioobe

If the $PATHis set in the terminal you launch emacs from, you can execute system commands through the command M-! <your command> RET.

如果在$PATH启动 emacs 的终端中设置了 ,则可以通过命令执行系统命令M-! <your command> RET

回答by Medardo Rodriguez

There are many Emacs packages that update $PATH environment variable and the 'exec-path'. That's because Emacs don't assume definitions in BASH related files, such as '~/.bashrc'.

有许多 Emacs 软件包会更新 $PATH 环境变量和“exec-path”。这是因为 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”手动加载用户配置文件。