How to copy text from less

12

3

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.

Hashbrown

Posted 2013-07-30T04:04:41.763

Reputation: 1 720

Answers

0

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.

Petr Javorik

Posted 2013-07-30T04:04:41.763

Reputation: 156

this must have been what @Neil was trying to tell me

– Hashbrown – 2019-11-24T02:57:51.663

1

for other people, as @ahilsend noted xsel -i is good for the middle-click buffer, but use xsel -ib if you wanted the paste buffer instead

– Hashbrown – 2019-11-24T03:01:25.900

4

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

Hashbrown

Posted 2013-07-30T04:04:41.763

Reputation: 1 720

it seems possible to create a clipboard file device and then use :s /dev/clipboard (well, ~/.dev/clipboard, maybe)

– Hashbrown – 2019-09-13T00:17:33.230

5Still seeking answer for save to clipboard – Hashbrown – 2013-08-01T00:36:32.860

3

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

rwxrwxrwx

Posted 2013-07-30T04:04:41.763

Reputation: 821

1Maybe add the -b option to use the clipboard selection. – ahilsend – 2013-07-30T07:41:43.877

Thanks @ahilsend, you are right. By the way I use clipit to sync clipboards and it works well. – rwxrwxrwx – 2013-07-30T07:59:28.770

I should be more specific, I do not start the less command. The less view is up, and you must copy its contents then. So using some sort of inbuilt functionality I think is necessary. Piping can't be used – Hashbrown – 2013-07-30T10:32:06.197

3

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.

Neil

Posted 2013-07-30T04:04:41.763

Reputation: 412

When I try it I get less /etc/php.ini | xsel -ib xsel: Can't open display: (null) what am I doing wrong? – vfbsilva – 2015-09-22T18:01:24.200

as far as I know, you cannot use :v when the input comes from std-in, which, in my case, it is. Will this method somehow mediate this? – Hashbrown – 2013-08-01T04:58:48.623

3You can pipe any buffer's contents to the clipboard with g|$xsel -ib. – Neil – 2013-08-01T16:27:59.687

You can also modify your PATH to replace less with something that redirects to vim. – Neil – 2013-08-01T16:31:32.117

That would work, actually. Very intrusive, but will work, @Neil – Hashbrown – 2013-08-01T22:46:38.033