I'd like to run a shell command that asks a few interactive questions before proceeding, from within Emacs. The functionality I'm looking for is like xterm -e <command>, only with an Emacs window opening up instead. It wouldn't need too much terminal emulation capability (it's basically pressing "y" a few times), but it still needs keyboard input. Is it possible to do this somehow?

I've tried using term and ansi-term already; when given a command that's different from /bin/bash, both close immediately, without leaving any time to read what the problem was.

  • 1
    If you know the answers are "y", then you probably do not need to interact with what is happening. By providing more specifics, someone can probably write up a solution that obviates the need for interaction. – lawlist May 1 '15 at 6:34

Short answer:

M-x terminal-emulator

sounds exactly like what you're asking for.

What you need might be different - if you really just need to give the program a series of y lines on stdin, consider yes | program. If the program really wants its stdin to be a tty, consider using expect.


You can make an emacs editing macro ("editing macros" are what the name "emacs" is derived from) and in the keyboard macro put commands that run Gnu Bash scripts or other shell programs and create scripts that query the user for Y or N answers.

So to create the macro, you'd do something like this:

Cx (
Mx new-frame return
Mx shell return (or just Mx eshell return--to use the emacs built-in shell)
bash <bash-program-that-queries-end-user-for-Y-or-N-answers> &
Mx delete-frame
Cx )

Save the macro and put it into an emacs lisp program that you load for reuse.

The macro would of course be usable from then on within emacs and/or you could run the whole thing as a batch file, from the command line, like any other program.

Other tools and suggestions: Look into emacs recursive-editing (which stops and re-starts macros and gives an end-user time to answer questions or even edit things) and the emacs built-in function message-or-box or just the message function. You could use tkquery (an easy to setup Tcl/Tk program that queries users for input data) and/or use zenity to make simple graphical interaction boxes that are easy to program and easy to use by end users.

You could create a question and answer routine in Gnu Make and run it inside Gnu Emacs. To do this and interact with a running Make session you'd could use process-send-string like this excerption from one of my makefiles:

echo "(process-send-string \"*compilation*\" \" \\n \") "

I mean, you could use the emacs process-send-string function to communicate and answer Y-or-N questions with a running Gnu Make program that is running inside a Gnu Emacs buffer.

Lastly, getting back to your question and your wondering why xterm -e <your-program> is terminating quickly: You could just do things like emacs -l <emacs-lisp-file-with-question-and-answer-macro-in-it> & disown and/or xterm -e <your-program-which-queries-the-end-users> & disown. But this depends on how you want to run the program, as a synchronous or an asynchronous process; I mean, do you want to block the program from running while the end user answers the question(s) or not?

With these tools in hand I think you can get what you want with Gnu Emacs; and, since Gnu Emacs is extensible you can certainly get it to do what you want somehow--if you extend it by programming it yourself.

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.