48

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
sastanin
  • 3,917

9 Answers9

43

You could use process substitution:

$ emacs --insert <(echo 123)
Andrew Wood
  • 1,309
17

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.

Trey Jackson
  • 4,071
14

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.

11

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))
    ))
cwitte
  • 111
7

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.

kasperd
  • 2,931
7

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.

7

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))'
    else
        _emacsfun "$@"
    fi
}

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.

dshepherd
  • 328
2

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.

jweede
  • 6,933
0

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}\"))" &
    fi;
else
    emacsclient --alternate-editor "emacs" --no-wait "$@" > /dev/null 2>&1 &
fi;
}