Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If I've piped the results of a command to less and then decided that I want to save the contents to a file, is this possible?

I've tried setting a mark a at the end of the buffer, and then returning to the top and using |avi to send the whole contents to vi, but that doesn't work.

share|improve this question
    
Your original technique would work if you instructed vi to load from stdin, by doing |avi -. –  Joe Shaw Apr 18 at 17:07
add comment

4 Answers

up vote 35 down vote accepted

On my system, man less says

       s filename
              Save the input to a file.  This only works if  the  input  is  a
              pipe, not an ordinary file.

Works for me!

share|improve this answer
    
nice, thanks! Works a treat. –  Jonathan Day May 31 '11 at 11:43
1  
Somehow, this doesn't work for me - typing 's' moves the window by one line. I'm on a Mac. –  benroth Feb 19 at 0:47
    
@benroth: You probably have a lesskey file that changes the normal commands. See man lesskey –  RedGrittyBrick Feb 19 at 9:44
add comment

Use the > operator. For example: less foo.bar > output.txt.

share|improve this answer
    
Thanks @Dror, but I'm already in the less application, not at the bash prompt any more –  Jonathan Day May 31 '11 at 9:53
add comment

No if you have started less, but if you know before yu want to send it to less and a file then you can use the tee command

command | tee out_file | less
share|improve this answer
    
Thanks Mark, but I'm specifically looking how to do it if I'm already in less –  Jonathan Day May 31 '11 at 9:53
add comment

The accepted answer doesn't work on the Mac -- as @benroth says, pressing s just moves down a line -- but you can use a different method.

In less --help:

|Xcommand            Pipe file between current pos & mark X to shell command.

and

A mark is any upper-case or lower-case letter.
Certain marks are predefined:
     ^  means  beginning of the file
     $  means  end of the file

So if you go to the top of the buffer (<) and then:

|$cat > /tmp/foo.txt

the contents of the buffer will be written out to /tmp/foo.txt.

share|improve this answer
add comment

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.