How can I save the current contents of less to a file?

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!


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.


When your less is opened, you can save the complete output to a file. Like vim, less supports commands.

Just type the key s, then less will ask you the name of the file where you wish to save the content, just type the file name and then type Enter.

Cheers

Tags:

Bash

Pipe

Less