I kept pasting
export FOO=bar
into the *scratch* buffer and editing it into (setenv "FOO" "bar")
and eval'ing that. After the 1000th time of doing that, I decided to automate it. Turns out emacs already has an interactive function for copying an environment variable from the shell, but you have to type in the variable name. I decided to write a little function to look for the last export, or the last echo $FOO
, and copy that variable:(defun engisneering-shell-copy-env-var () (interactive) (let* ((expat "\\(export +\\([^=\n]+\\)=\\(.+\\)\\)") (echpat "\\(echo +\\$\\(.+\\)\n\\(.+\\)\\)") (cshpat "\\(setenv +\\([^ \n]+\\) +\\(.+\\)\\)") (patt (concat shell-prompt-pattern "\\(" expat "\\|" echpat "\\|" cshpat "\\)"))) (save-excursion (if (re-search-backward patt) (let* ((m (or (and (match-beginning 2) 3) (and (match-beginning 5) 6) 9)) (var (buffer-substring (match-beginning m) (match-end m))) (oldval (or (getenv var) " "))) (shell-copy-environment-variable var) (setq val (getenv var)) (message "Old %s=%s; New %s=%s" var oldval var val))))))
Run this function inside your shell buffer, and it will search backwards for the last environment variable action and bring that variable setting into the emacs session. It's also nice because it gives you a message in the minibuffer showing the change. Add a
local-set-key
-- I like C-c C-v
-- in your shell-mode-hook, and you're good to go.