Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

With Vim I can easily do

$ echo 123 | vim -

Is it possible to do with Emacs?

$ echo 123 | emacs23
... Emacs starts with a Welcome message

$ echo 123 | emacs23 -
... Emacs starts with an empty *scratch* buffer and “Unknown option”

$ echo 123 | emacs23 --insert -
... “No such file or directory”, empty *scratch* buffer

Is it really impossible to read a buffer from a unix pipe?

Edit: As a solution, I wrote a shell wrapper named emacspipe:

#!/bin/sh
TMP=$(mktemp) && cat > $TMP && emacs23 $TMP ; rm $TMP
share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

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 and C-c continue to have their normal effect. The functions prin1, princ and print output to stdout instead of the echo area, while message and error messages output to stderr. Functions that would normally read from the minibuffer take their input from stdin 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.

share|improve this answer
    
Thank you Trey for detailed explanations. –  sastanin Aug 31 '09 at 9:10
add comment

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.

share|improve this answer
    
Yes, I know there is emacsy way, but I hoped it may be used unixy way. Creating a temporary file is not a very nice option (I have to remember to delete it later). –  sastanin Aug 31 '09 at 9:08
    
tacking on ';rm temp' should delete the file once emacs closes –  jweede Aug 31 '09 at 12:06
1  
such as: echo 123 > temp; emacs temp; rm temp –  jweede Aug 31 '09 at 12:10
1  
In general, there is a high impedance between Emacs and Unix. Or at least between Emacs and the traditional Unix work flow. –  Richard Hoskins Aug 31 '09 at 15:28
1  
@jweede If you want to add M-! part of my answer to yours, then I could delete my answer. There is a large overlap in our answers, but I think meta-bang is important to future readers. –  Richard Hoskins Aug 31 '09 at 16:03
show 1 more comment

It is possible, see http://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))
    ))
share|improve this answer
add comment

offhand, something like:

$ echo 123 > tmp.txt; emacs tmp.txt

or

$ echo 123 > tmp.txt; emacs tmp.txt; rm tmp.txt

is an option. Emacs just doesn't integrate with UNIX the way vim does.

share|improve this answer
    
It is surprising that Emacs doesn't integrate with UNIX better, given its history and that one of the key tenets of UNIX is that "everything is a file". It feels intuitive to pipe output directly into Emacs. –  SabreWolfy Sep 11 '11 at 11:11
1  
@SabreWolfy While GNU Emacs is most commonly hosted on Unix, it isn't "a Unix program" the way Vim is, but rather a Lisp machine that implements a largely platform-independent text editor. (See Richard Hoskins' answer; the "Emacs way" of doing this isn't to pipe shell command into Emacs, but to have invoke the shell command from within Emacs via M-!, which automatically captures the resulting output into a temporary buffer.) Holy wars aside, neither editor is "better" than the other; they just have very different perspectives on pretty much everything. –  Aaron Miller Aug 8 '13 at 15:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.