17

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?

5
  • 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. Commented Oct 26, 2011 at 9:04
  • Why did you rollback my edit? Isn't the title supposed to be a question? Commented Oct 26, 2011 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? Commented Oct 26, 2011 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. Commented Oct 26, 2011 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. Commented Oct 26, 2011 at 19:49

3 Answers 3

27

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

You can also use this to remap the default binding in your lesskey with:

v noaction g|$vim -\n
1
  • That's awesome! Is there a way to bind that to a key in less? Commented Sep 1, 2021 at 22:10
9

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.

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.