我需要在Emacs中使用$PATH
来运行一些命令。我该如何让Emacs使用它?我是从Ubuntu仓库安装的Emacs。
我需要在Emacs中使用$PATH
来运行一些命令,该怎么做呢?我是从Ubuntu仓库安装的Emacs。
我需要在Emacs中使用$PATH
来运行一些命令。我该如何让Emacs使用它?我是从Ubuntu仓库安装的Emacs。
我需要在Emacs中使用$PATH
来运行一些命令,该怎么做呢?我是从Ubuntu仓库安装的Emacs。
这里有一个技巧我使用,可以确保我的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))
$PATH
的定义,因此这个技巧对我在该平台上的使用很有帮助。给出了一种更通用的解决方案(不仅设置PATH
变量和别名)。请参见https://dev59.com/nGct5IYBdhLWcg3wQ7Qy#12229404,关键是要设置:
(setq shell-command-switch "-ic")
添加"-i"
参数将强制Bash shell进入交互模式,从而导致读取~/.bashrc
文件。此外,"-i"
会覆盖默认的"-c"
(执行以下命令)。
~/.bash_profile
中的所有内容,然后在~/.bashrc
中添加了一行source ~/.bash_profile
。就这么简单。最终,我的$PATH
和方便的别名可以在Emacs
的M-x shell-command
中顺利使用。 - Y. E. emacs
更改为 bash-c emacs
。您可以将路径设置添加到 /etc/profile.d 中,例如:
# /etc/profile.d/path.sh
export PATH="$PATH:/usr/local"
~/.xsessionrc
,所以你也可以在这个文件中设置GUI应用程序的路径。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
;;-------------------------------------------------------
$PATH
,则可以通过命令M-! <your command> RET
执行系统命令。有许多Emacs软件包可以更新$PATH环境变量和'exec-path'。这是因为Emacs不假设BASH相关文件(如“~/.bashrc”)中的定义。
在任何非从终端shell执行的程序中所需的所有定义都必须移动到“~/.profile”中,这些定义在系统启动时加载。
一些旧系统需要手动从“/etc/profile”中加载用户配置文件。
这应该是一条评论,但格式不正确。@Hueges的答案对我有用,但它会导致dired
模式出现问题。有更强的elisp能力的人可能会做得更好,但对我来说它有效。
;; temporarily set shell-command-switch to -ic until function completes
(defun wrap-shell-command (func &rest args)
(let ((current-shell-command-switch shell-command-switch))
(setq shell-command-switch "-ic")
(apply func args)
(setq shell-command-switch current-shell-command-switch)))
;; usage
(wrap-shell-command 'shell-command "func-define-in-bashrc" "other" "args")
;; set shell-command-switch to -ic only for async-shell-command
(defun pre-command-hook-fn ()
"."
(if (string= current-minibuffer-command "async-shell-command")
(setq shell-command-switch "-ic")
(setq shell-command-switch "-c")
)
)
(add-hook 'minibuffer-setup-hook 'pre-command-hook-fn)
待办事项:修改 'exec-path' 以便让迷你缓冲区完成使用交互式 shell 的 PATH。