13

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.

4 Answers 4

2

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.

3
  • this must have been what @Neil was trying to tell me
    – Hashbrown
    Nov 24, 2019 at 2:57
  • 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
    Nov 24, 2019 at 3:01
  • 1
    For macOS users you can replace xsel -i with pbcopy
    – Will Ayd
    Nov 9, 2020 at 22:28
5

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

2
  • 7
    Still seeking answer for save to clipboard
    – Hashbrown
    Aug 1, 2013 at 0:36
  • it seems possible to create a clipboard file device and then use :s /dev/clipboard (well, ~/.dev/clipboard, maybe)
    – Hashbrown
    Sep 13, 2019 at 0:17
3
  1. install xsel first
  2. copy to clipboard using xsel: less filename.txt | xsel -i
  3. paste it: xsel -o
3
  • 1
    Maybe add the -b option to use the clipboard selection.
    – ahilsend
    Jul 30, 2013 at 7:41
  • Thanks @ahilsend, you are right. By the way I use clipit to sync clipboards and it works well.
    – rwxrwxrwx
    Jul 30, 2013 at 7:59
  • 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
    Jul 30, 2013 at 10:32
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.

5
  • 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
    Aug 1, 2013 at 4:58
  • 3
    You can pipe any buffer's contents to the clipboard with g|$xsel -ib.
    – Neil
    Aug 1, 2013 at 16:27
  • You can also modify your PATH to replace less with something that redirects to vim.
    – Neil
    Aug 1, 2013 at 16:31
  • That would work, actually. Very intrusive, but will work, @Neil
    – Hashbrown
    Aug 1, 2013 at 22:46
  • When I try it I get less /etc/php.ini | xsel -ib xsel: Can't open display: (null) what am I doing wrong?
    – vfbsilva
    Sep 22, 2015 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.