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?

salotz
  • 1,370
  • 10
  • 21

2 Answers2

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.

Harald Hanche-Olsen
  • 2,401
  • 12
  • 15
  • 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 '16 at 20:09
  • @Tyler Use a prefix argument to avoid that. No different from the original `shell` command. – Harald Hanche-Olsen Apr 10 '16 at 20:10
  • To use a prefix when you have `helm-M-x` bound see this asnwer http://emacs.stackexchange.com/questions/20299/how-to-pass-a-prefix-argument-to-helm-m-x – salotz Apr 12 '16 at 01: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 05 '20 at 00: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.

salotz
  • 1,370
  • 10
  • 21
  • FYI: Also for this version of `other-shell` lexical scoping does not matter since `(special-variable-p 'shell-file-name)` gives t. – Tobias Mar 05 '20 at 00:54