0

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

(global-set-key (kbd "C-c C-SPC")
                (lambda()
                    (interactive)
                    (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? – DoMiNeLa10 Jul 10 '18 at 21:30
2

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
                                               default-directory
                                             command-line-default-directory)))
                    (term (getenv "SHELL")))))
  • 2
    I would recommend using a named function instead of a lambda. – phils Jul 11 '18 at 1:29

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.