Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

TMP=$(mktemp) && cat > $TMP && emacs23 $TMP ; rm $TMP
share|improve this question
up vote 6 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
I mean no disrespect to anyone, but this is abhorrent. This is a very basic editor feature and GNU EMACS has been around for decades. It should be built in. – user787832 Feb 6 '15 at 15:46

You could use process substitution:

$ emacs --insert <(echo 123)
share|improve this answer
This is definitely the answer that gets closest to the Vim functionality. Pretty much just moving the piped part into a subprocess substitution. – dbmikus Jan 29 '15 at 15:42
@dbmikus I can't decide which I prefer between mine and Tomasz Obrębski's.. – Andrew Wood Jan 29 '15 at 17:02
Tomasz's results in me getting the following error for some reason: emacs: standard input is not a tty – dbmikus Jan 29 '15 at 18:49
Oh indeed! I assumed he'd tested before posting. – Andrew Wood Jan 29 '15 at 19:56
Hmm.. I just tried Tomasz Obrębski's again, and it works for me this time :/ – Andrew Wood Feb 6 '15 at 20:11

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
such as: echo 123 > temp; emacs temp; rm temp – jweede Aug 31 '09 at 12:10
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
@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
temp may already exist in the current directory, it's not safe; as a solution, I wrote a wrapper: TMP=$(mktemp) && cat > $TMP && emacs23 $TMP ; rm $TMP. Thanks everybody! – sastanin Sep 1 '09 at 12:36

It is possible, see

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
    (condition-case nil
    (let (line)
      (while (setq line (read-from-minibuffer ""))
        (insert line)
        (insert "\n")))
      (error nil))
    (princ (buffer-string))
share|improve this answer

It's possible to create a simple shell function which works as it is reading from stdin (although in fact it is writing to a temporary file then reading that). Here's the code I'm using:

# The emacs or emacsclient command to use
function _emacsfun
    # Replace with `emacs` to not run as server/client
    emacsclient -c -n $@

# An emacs 'alias' with the ability to read from stdin
function e
    # If the argument is - then write stdin to a tempfile and open the
    # tempfile.
    if [[ $# -ge 1 ]] && [[ "$1" == - ]]; then
        tempfile="$(mktemp emacs-stdin-$USER.XXXXXXX --tmpdir)"
        cat - > "$tempfile"
        _emacsfun --eval "(find-file \"$tempfile\")" \
            --eval '(set-visited-file-name nil)' \
            --eval '(rename-buffer "*stdin*" t))'
        _emacsfun "$@"

You just use the function as an alias for emacs, e.g.

echo "hello world" | e -

or as normal from files

e hello_world.txt

Replacing emacs by emacsclient in the function works as well.

share|improve this answer
This works well for me, but _emacsfun should be emacsclient -c -t $@, or at the very least drop the -n option. man pages with emacsclient -t --eval "(man \"$1\")" --eval "(delete-window)" (and now you can helm-swoop your way to Man Glory!) – Alejandro Mar 31 at 19:14

This works:

echo 123 | emacs --insert <(cat)

but, for some reason, only with graphical-mode emacs (Gnome,Konsole,GNU Emacs 23.4.1). The command:

echo 123 | emacs -nw --insert <(cat)

generates an error 'emacs: standard input is not a tty'. The same error appears when tried in raw text console.

share|improve this answer
echo 123 | exec emacs -nw --insert <(cat) </dev/tty should work. – pyrocrasty May 16 '15 at 17:15
That works; why the exec? This also works: emacs -nw --insert <(echo 123) </dev/tty – RoyM Oct 28 '15 at 16:32
Go figure: works beautifully on Emacs (w32) on Cygwin, where lots of other settings of mine do not – Charles Roberto Canato Jan 5 at 23:37

offhand, something like:

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


$ 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
@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

Another possibility not mentioned in any of the previous answers is to use /dev/stdin if your chosen Unix variant has it.

Simply trying to open /dev/stdin directly doesn't work, because Emacs does a few checks and then reports Symbolic link that points to nonexistent file. (And if Emacs would have allowed you to load the file, then trying to save it again as /dev/stdin would rarely do what the user expected.)

However combining /dev/stdin with the --insert argument does work:

echo 123 | emacs --insert /dev/stdin
share|improve this answer

Your Answer


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.