From 0489d73a0a25beb0cd8914322ed3ae2665f90cd3 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 4 Nov 2019 10:02:58 -0500 Subject: [PATCH] Add function to load env vars from a shell script --- emacs/init.org | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/emacs/init.org b/emacs/init.org index cd9fb0c..89a46a8 100755 --- a/emacs/init.org +++ b/emacs/init.org @@ -286,6 +286,19 @@ Pretty-print JSON: (buffer-substring (point-min) (point-max)))) #+END_SRC +Load environment variables into Emacs from a shell script: +#+BEGIN_SRC emacs-lisp + (defun source-env-file (file) + (interactive "fFile: \n") + (let ((var-re "\\(.+?\\)=\\(.+\\)$")) + (with-temp-buffer + (shell-command (concat "source " file " > /dev/null && env") + (current-buffer)) + (save-match-data + (while (re-search-forward var-re nil t) + (setenv (match-string 1) (match-string 2))))))) +#+END_SRC + ** Persisting variables between session The idea behind this is pretty simple - variables get persisted in ~/.emacs.d/ as a plist of (variable-name variable-value).