Piping into less and editing: “cannot edit standard input” error. Workarounds?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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).

Method 2

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.

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x