How to make Emacs read buffer from stdin on start?
Solution 1:
Correct, it is impossible to read a buffer from stdin.
The only mention of stdin in the Emacs info pages is this, which says:
In batch mode, Emacs does not display the text being edited, and the standard terminal interrupt characters such as
C-z
andC-c
continue to have their normal effect. The functionsprin1
,princ
andstdout
instead of the echo area, whilemessage
and error messages output tostderr
. Functions that would normally read from the minibuffer take their input fromstdin
instead.
And the read
function can read from stdin
, but only in batch mode.
So, you can't even work around this by writing custom elisp.
Solution 2:
You could use process substitution:
$ emacs --insert <(echo 123)
Solution 3:
You can redirect to a file, then open the file. e.g.
echo 123 > temp; emacs temp
jweede notes that if you want the temp file to automatically be removed, you can:
echo 123 > temp; emacs temp; rm temp
The Emacsy way to do this is to run the shell command in Emacs.
M-! echo 123 RET
That gives you a buffer named *Shell Command Output* with the results of the command.
Solution 4:
It is possible, see https://stackoverflow.com/questions/2879746/idomatic-batch-processing-of-text-in-emacs
Here is echo in an emacs script (copied from the above link):
#!/usr/bin/emacs --script
(condition-case nil
(let (line)
(while (setq line (read-from-minibuffer ""))
(princ line)
(princ "\n")))
(error nil))
or to read it into a buffer and then print it out all in one go
#!/usr/bin/emacs --script
(with-temp-buffer
(progn
(condition-case nil
(let (line)
(while (setq line (read-from-minibuffer ""))
(insert line)
(insert "\n")))
(error nil))
(princ (buffer-string))
))