emacs: standard input is not a tty

Did you ever tried something like :

$ find -name '*.c' | xargs emacs
$ grep -rl snmp . | xargs emacs

and got the error “emacs: standard input is not a tty” ?

That’s normal, as the stdin for emacs is here the pipe, not your tty, you need a workaround to leave the normal stdin to your emacs.

There are different approches, the one I used for a long time was a command substitution :

$ emacs $(find -name '*.c')

But other approches exists, redirecting /dev/tty to emacs’ stdin :

$ find -name '*.c' | xargs sh -c 'emacs "[email protected]" < /dev/tty' emacs

And if you are searching for a specific pattern with grep, you should want to jump directly to the right line using the ‘+line file’ syntax of emacs, and the shell substitution :

$ emacs $(grep -rn snmp services/ | sed -r 's/([^:]*):([0-9]*):.*/+\2 \1/g')

It’s good while having only one occurence of the pattern in each file, but if many occurences exists, the file is oppened only once, with a while you can open one emacs for each occurence of the searched pattern and the /dev/tty redirection :

$ grep -rn snmp services/ | sed -r 's/([^:]*):([0-9]*):.*/+\2 \1/g' | while read line file; do emacs $line $file < /dev/tty; done
This entry was posted in Code. Bookmark the permalink.

2 Responses to emacs: standard input is not a tty

  1. R.U.R. says:

    Thanks! This enabled me to have a function:

    function emforeach () {
    while read i
    do emacs $i < /dev/tty

    which I can use for nice things like "well now I created five files and found out that I have to edit each of them a little":

    ls -tr | tail -5 | emforeach

    (I need this quite often so I have one more alias, so actually I do write only: lastt 5 | emforeach)

Leave a Reply to Julien Palard Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>