0

I simply want to execute the whole file I am editing right now in a small shell buffer just like I can run my python script. How can I do this?

7
  • What have you tried so far?
    – Dan
    Jun 18 '19 at 13:07
  • 2
    What about Execute script... and Execute region in the Sh-script menu?
    – Tobias
    Jun 18 '19 at 13:10
  • 1
    Thanks! Did not know that there is a major mode menu! Maybe I should activate the menu-bar that got deactivated in spacemacs sometimes
    – Tillus
    Jun 18 '19 at 13:21
  • 2
    Yes, the menus are there to make features more discoverable! I would not recommend disabling it.
    – phils
    Jun 19 '19 at 11:08
  • what is the emacs command associated to this ?
    – Welgriv
    Jul 28 '20 at 11:02
2

Here you can find a function that will send current line to the shell buffer: https://stackoverflow.com/questions/6286579/emacs-shell-mode-how-to-send-region-to-shell/7053298#7053298

Here is an adapted version that uses comint-scroll-to-bottom-on-output, so the shell buffer always scrolls to the last output: https://nistara.net/post/emacs-send-line-or-region-to-shell/

1
  • Hi. I havn't used Emacs in a while (spent too much time configuring). But if someone can confirm this solution I wil accept it.
    – Tillus
    Nov 27 '20 at 16:18
1

A possible workaround would be to select the entire buffer (C-x h) and then use the function to run a shell command on a region as explained on https://stackoverflow.com/questions/1871970/emacs-execute-code-in-shell-script-mode.

If I have a split window with the shell script on top and a session in bash below, it would indeed be nice to have a command to send the content of the upper buffer to the bash session and see the output without having to change to the buffer below.

1
  • Hi. I haven't used Emacs in a while (spent too much time configuring and to view time working). But if someone can confirm this solution I will accept it.
    – Tillus
    Jun 9 at 13:42
1

You can execute the selected code snippet with C-M-x (sh-execute-region). (It's the same key binding as eval-defun when editing Lisp code, but in sh mode, it executes the region, not the defun around point.) This uses the shell in sh-shell-file, which is determined from the shebang line if there is one.

If the script is executable, you can execute it with C-c C-x (executable-interpret). You'll be given the opportunity to pass arguments. (This command works no matter what language the program is written in, by the way, as long as the file you're editing is executable.)

Neither command maintains a running interpreter in the background, like run-python and the associated eval commands do in Python mode. This functionality isn't built in for shell mode. If you want that, you can copy-paste into a *shell* buffer.

1
  • Hi. I havn't used Emacs in a while (spent too much time configuring). But if someone can confirm this solution I wil accept it.
    – Tillus
    Aug 24 at 19:44

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.