I want to set PAGER to cat in shell-mode. For eshell I just add a hook with setenv. Unfortunately it doesn't seem to work for shell-mode, setenv doesn't set env variable. Is there other way to redefine shell-defined variables (which may be set in ~/.zshrc, so it needs to be redefined after shell is launched and rc file is sourced). I'm using following code:

(defun my--shell-mode-hook ()
  (setenv "PAGER" "cat")
  (setenv "EDITOR" "emacsclient")
  (setenv "BAR" "bar")
  (message "%s" (getenv "BAR"))

(add-hook 'shell-mode-hook 'my--shell-mode-hook)

When I run M-x shell string "bar" is displayed in the messages buffer, so var is being set in emacs. But in shell itself it is not set, neither existing variables are reset:

ineu@rei ~ $ env | grep -i -e pager -e editor -e bar
env | grep -i -e pager -e editor -e bar
EDITOR=subl3 -w
ineu@rei ~ $
  • It doesn't seem to work for shell-mode you said. How did you use setenv? And, how did you verify?
    – Nsukami _
    Jan 6 '15 at 13:06
  • @Nsukami_ updated question with code snippets.
    – Ineu
    Jan 6 '15 at 13:42
  • It doesn't work indeed, let me dig.
    – Nsukami _
    Jan 6 '15 at 15:12

Do it like you would do it in xterm:


If you always want to set the same variables in shell-mode but not xterm then add this to your shells init file:

if test -n "$INSIDE_EMACS"

Or in Emacs

(add-hook 'shell-mode-hook 'my-shell-mode-hook)
(defun my-shell-mode-hook ()
  (process-send-string (get-buffer-process (current-buffer))
                       "export VARIABLE=VALUE\n"))

Note that when using Zsh then I am having problems with this, with Dash or Bash it works without any problems.

  • You should test for $INSIDE_EMACS. $EMACS is obsolete, iirc.
    – user227
    Jan 18 '15 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.