I'm something of an Emacs newbie. I would like to run a shell script I've written and insert the output into the buffer I'm working in. But the script requires some interaction, so ideally I'd like a workflow where I:

  • Call a command from the main buffer
  • Shell or Term mode opens, I interact with the script, the script gives final output and exits
  • Final output is inserted at the point in my main buffer

Is this feasible?

I've read some of the documentation on shell mode, the GNU documentation on running shell commands in Emacs and other material I've been able to find, but I can't find anything that addresses this issue specifically. I may have just missed it, of course.

  • I do not know how to do what it is that you seek, but the function shell-command-to-string can be used to return a value and that can be modified as needed; e.g., by modifying the string. E.g., (insert (car (split-string (shell-command-to-string "date") "\n"))). To enter commands underneath the shell hood, there are the functions comint-send-string and comint-send-input, the latter of which is required if the former does not contain a \n at the end of the string.
    – lawlist
    Commented Sep 23, 2019 at 8:01
  • @lawlist but these commands don't allow for interaction with the shell, do they?
    – ShankarG
    Commented Sep 23, 2019 at 8:34
  • Here is a link to an example where the shell buffer output is copied to the kill-ring: emacs.stackexchange.com/questions/30458/… Perhaps you can take that example and modify it to your needs. If you do not need much interaction with the shell script, perhaps you could write your own function that looks for specific input from the user, and then continues to its conclusion where it finally inserts the output into your working buffer. You may also want to try Googling things like: shell redirect output emacs
    – lawlist
    Commented Sep 23, 2019 at 18:25


Your Answer

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

Browse other questions tagged or ask your own question.