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

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.

  • 16
    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)
  • 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
  • 1
    Maybe it depends on the month. – dbmikus Feb 10 '15 at 18:41

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.

  • 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
  • 1
    such as: echo 123 > temp; emacs temp; rm temp – jweede Aug 31 '09 at 12:10
  • 2
    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
  • 2
    @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
  • 1
    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 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
    (condition-case nil
    (let (line)
      (while (setq line (read-from-minibuffer ""))
        (insert line)
        (insert "\n")))
      (error nil))
    (princ (buffer-string))
  • Why not do (let (line) (while (setq line (ignore-errors (read-from-minibuffer ""))) (insert line) (insert "\n"))) instead of using condition-case? – Flux Feb 18 at 12:52

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.

  • 1
    echo 123 | exec emacs -nw --insert <(cat) </dev/tty should work. – pyrocrasty May 16 '15 at 17:15
  • 1
    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 '16 at 23:37

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.

  • 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 '16 at 19:14
  • 1
    This is the only answer that I could get to work with emacsclient. I made a shell script out of the basic idea so I can call it from i3 config. +1 – ergosys Mar 8 '19 at 22:39

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

It should be noted that this version only works when using X. If you need a solution which works in a terminal I suggest you look at another answer.

  • i got emacs: standard input is not a tty message testing it on linux and bsd – user373230 Feb 9 '18 at 15:28
  • 1
    @Chinggis6 Looks like my suggestion only works when using X11. If I first type unset DISPLAY, I get the same error message as you. – kasperd Feb 9 '18 at 21:59
  • 1
    @Chinggis6 I updated my answer to point out that it needs X to work and pointed to an answer which works without X. – kasperd Feb 9 '18 at 22:05

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.

  • 1
    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
  • 5
    @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
  • except when you're in a shell and want to do something like curl foo.bar | vim - .. I'm sorry. I meant curl foo.bar | emacs except you can't do that – dylnmc Oct 19 '18 at 1:02

The solution which works for me in bash is shown on EmacsWiki:

Add the following function into your .bashrc

e() {
local TMP;
if [[ "$1" == "-" ]]; then
    TMP="$(mktemp /tmp/emacsstdinXXX)";
    cat >"$TMP";
    if ! emacsclient --alternate-editor /usr/bin/false --eval "(let ((b (create-file-buffer \"*stdin*\"))) (switch-to-buffer b) (insert-file-contents \"${TMP}\") (delete-file \"${TMP}\"))"  > /dev/null 2>&1; then
        emacs --eval "(let ((b (create-file-buffer \"*stdin*\"))) (switch-to-buffer b) (insert-file-contents \"${TMP}\") (delete-file \"${TMP}\"))" &
    emacsclient --alternate-editor "emacs" --no-wait "$@" > /dev/null 2>&1 &

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.