9

I recently learned that if I page my files with less, I can press v and open the buffer in Emacs. This works as long as I have the environment variable $EDITOR set up correctly, which in my case I have it as:

export EDITOR='env TERM=xterm-256color emacs -nw'

The above gives me the ability to load emacs within the terminal that runs less.

However, the trick above does not work when piping commands to less. When doing this, less complains with the following message:

Cannot edit standard input (press RETURN)

My question is, why does this happen? But most importantly, is there any workaround to it?

  • 2
    You should learn how to use emacsclient, which will let you edit files in an already-running copy of Emacs. Emacs isn't the fastest-starting editor. – cjm Oct 26 '11 at 9:04
  • Why did you rollback my edit? Isn't the title supposed to be a question? – John Oct 26 '11 at 19:38
  • 1
    Sorry @John, I didn't see your edit. I edited the title to clarify that I am mostly interested in workarounds to this problem. I think the title as it is: Piping into less and editing: “cannot edit standard input” error. Workarounds? is clear. Why do think otherwise? – Amelio Vazquez-Reina Oct 26 '11 at 19:44
  • That's understandable. Although I would assume that if you're asking why you got the error, it should be obvious that you would want a workaround. – John Oct 26 '11 at 19:47
  • I agree, although the most voted answer so far addresses the why only, and not the workarounds, so I added the emphasis. – Amelio Vazquez-Reina Oct 26 '11 at 19:49
5

As it was already said, you cannot edit an input stream.

The workaround is quite obvious: save the stream to a file from within less and open the saved file. To do that while viewing, press s and give a name of a file. Then either exit less and open the file by hand, or, still from within less, type :e and give the name of the file you just saved.

|improve this answer|||||
15

A good workaround is to pipe the output from less to your editor, e.g. after "echo foo | less" you can do:

g|$vim -

g will jump to the first line of the file/input, | will invoke the "pipe" action, $ is the mark ("the end"), followed by the command to pipe the range into (vim - in this case, where the "-" means to read the input from stdin).

|improve this answer|||||
5

It means exactly what it said - you can only edit a file that is on the disk in this way (from less). If you piped the output of a process, its STDOUT is going into STDIN for less; there is no file to edit.

|improve this answer|||||

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.