2

I know you can customize settings or set environmental variables to get emacs to use other shells in shell-mode (1, 2). Is it possible to simply start a shell specifying which shell you want to use interactively?

Like: M-x other-shell RET /path/to/csh RET

Or set up something in your init file to let you use other shells fairly easily without changing the default?

2 Answers 2

3

This appears to do the job:

(defun other-shell (explicit-shell-file-name)
  (interactive "f")
  (if (called-interactively-p)
      (call-interactively #'shell)
    (shell)))

This relies on shell using the variable explicit-shell-file-name as a first choice of the shell to be used.

It also relies on not using lexical scoping.

4
  • This works if you don't already have a shell running. But if you do, it switches you to the existing shell.
    – Tyler
    Apr 10, 2016 at 20:09
  • @Tyler Use a prefix argument to avoid that. No different from the original shell command. Apr 10, 2016 at 20:10
  • To use a prefix when you have helm-M-x bound see this asnwer emacs.stackexchange.com/questions/20299/…
    – salotz
    Apr 12, 2016 at 1:32
  • AFAIK lexical scoping does not matter here since explicit-shell-file-name is defined through defcustom in shell.el and that marks the variable as special.
    – Tobias
    Mar 5, 2020 at 0:51
0

Building on Harald's answer. If you want to specify the shell by name (interactive "s" option) rather than path and use the default shell-file-name instead of the explicit path:

(defun other-shell (shell-file-name)
  (interactive "s")
  (if (called-interactively-p)
      (call-interactively #'shell)
    (shell)))

I found this to be more useful when using a shell like xonsh where I just want to use the one in the virtualenv that started emacs, rather than specify long explicit paths.

1
  • FYI: Also for this version of other-shell lexical scoping does not matter since (special-variable-p 'shell-file-name) gives t.
    – Tobias
    Mar 5, 2020 at 0:54

Your Answer

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

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