Currently, I have following key binding defined in my init.el:

(global-set-key (kbd "C-c C-SPC")
                    (term (getenv "SHELL"))))

As it is easy to see, it executes the term command with my default shell.

The terminal is open with pwd equal to default-directory, which by default corresponds to the directory of the file I'm currently editing.

What I would like to do is be able to open it in command-line-default-directory. That's, in the directory from which I launched Emacs. However, I don't want to change default-directory variable, because it's behavior is ok for me for any other purpose.

  • 2
    Have you tried setting default-directory temporarily with let?
    – user12563
    Jul 10, 2018 at 21:30

1 Answer 1


as DoMiNeLa10 suggested, let is the way to go. With a prefix arg, you can have it both ways, a default to the command-line-default-directory, and with a prefix to the local directory.

(global-set-key (kbd "C-c C-SPC")
                (lambda (local)
                  (interactive "P")
                  (message "local: %S" local)
                  (let ((default-directory (if local
                    (term (getenv "SHELL")))))
  • 2
    I would recommend using a named function instead of a lambda.
    – phils
    Jul 11, 2018 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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