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?
3 Answers
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.

- 22,318
- 4
- 56
- 92
-
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. – tbrodbeck Aug 24 '21 at 19:44
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/

- 185
- 1
- 10
-
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. – tbrodbeck Nov 27 '20 at 16:18
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.

- 77,472
- 10
- 114
- 243

- 111
- 3
-
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. – tbrodbeck Jun 09 '21 at 13:42
Execute script...
andExecute region
in theSh-script
menu? – Tobias Jun 18 '19 at 13:10M-x menu-bar-mode
to toggle it. – tbrodbeck Jul 29 '20 at 09:42f10
(menu-bar-open
). – Gilles 'SO- stop being evil' Aug 23 '21 at 19:59