12

There has already been a post in stackoverflow for VI editor for copying all the text into the clipboard. (Copy all the lines to clipboard) I want to do the same thing with the less viewer. I tried to search online for the process called "yank" and I did not find anything for it.

How do I copy all lines in the less editor into the clip board.

And I cannot close less and reopen it in vi. It is because of the fact that I have managed to load this file into the editor and while I have loaded it, the file has already been moved in the back end. It is a long story. The easiest solution for me now is to copy the contents of the file into memory.

Community
  • 1
  • 1
xarzu
  • 7,579
  • 35
  • 100
  • 140

1 Answers1

23

less doesn't have a clipboard, but you may be able to get it to output what's stored in its buffers to a new file. This will only work if the entire contents of the file are buffered:

  1. Type g to go to the top of the file
  2. Type | (that's a pipe character, not an L or I) to indicate that you want to output to a pipe
  3. Type $ to indicate that you want the output content to go to the end of the file
  4. Type dd of=/path/to/new/file and press Enter

The dd command will take the piped data and save it to the file passed to the of= argument.

depquid
  • 646
  • 6
  • 20
  • 7
    Using `xclip` instead of `dd` send the output to the clipboard. – Clément Sep 23 '14 at 00:02
  • 10
    on Mac, instead of `dd`, use `pbcopy` to copy to clipboard – ryenus Feb 22 '17 at 04:33
  • Or, on a Mac, you can simply pipe the contents of the file to `pbcopy`, perhaps by using `cat`. That sidesteps the entire use of `less` and achieves the same result. – cjbarth Feb 27 '20 at 16:30