How to copy text from less

How might one copy the entire buffer being displayed by less?
No need to select specific text, I want it all.
Copying to the clipboard is preferable but if we can output it to a file that works too.

I've tried using generic vim commands like :w and the ones listed here but less doesn't seem to accept commands like that.

EDIT
I must be able to do this from within less. Let's say that less is pipeed to from an alias, I have no control over how less is actually called. I am just presented with the result.
I found a way to save the buffer (see my answer below) so now I just need to see if there is any way to select and copy. There probably isn't, given the restrictions.


Solution 1:

Dump less buffer into clipboard using xsel as follows:

  1. Open file $ less file.txt

  2. In less press |(pipe) then $

  3. Write xsel -i and press Enter

less buffer content should be copied.

Solution 2:

Type the command :s from within less to write the buffer to a 'log' file.

Source: The "s" command is equivalent to specifying -o from within less

Solution 3:

  1. install xsel first
  2. copy to clipboard using xsel: less filename.txt | xsel -i
  3. paste it: xsel -o

Solution 4:

You can repurpose less's 'v' command for this.

Look at the manpage for less, specifically about LESSEDIT. You can use @rwxrwxrwx's suggestion, if you do a little set-up ahead of time (maybe in your .bashrc):

bash$ export LESSEDIT="%E < %f"
bash$ export EDITOR="xsel -ib"

When less runs, press 'v' to open the present file in the $EDITOR; in this case, open it with xsel -ib < {the file's name}.

Using xsel -ib puts the data on the clipboard, so you can ctrl-V to paste the data where you want it.