Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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? –  user815423426 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. –  user815423426 Oct 26 '11 at 19:49

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

A good workaround is to pipe the output from less to the 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 the command to pipe the range into (vim - in this case, where the "-" means to read the input from stdin - something similar will exist/work with emacs).

share|improve this answer

Your Answer

 
discard

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.