18

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
  • 8,657
  • 40
  • 108
  • 160

3 Answers3

31

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
  • 726
  • 7
  • 20
  • 10
    Using `xclip` instead of `dd` send the output to the clipboard. – Clément Sep 23 '14 at 00:02
  • 11
    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
2

as an workaround you can set terminal's font size to 1, then select with mouse and copy (works for big , but not huge files).

noonex
  • 1,975
  • 1
  • 16
  • 18
0

If the file is not too big and if it fits in your terminal number of lines configured, then do the following:

  1. Terminal > Edit > Clear to start
  2. cat <file_name>
  3. Terminal > Edit > Select all
  4. Terminal > Edit > Copy