1

I copied some text and want to search through it using less. I wonder how I can directly use less on my clipboard content without having it saved first into a file or being echod etc.

xetra11
  • 546

3 Answers3

5

Use xclip or xsel (which should available on any Linux distribution and in BSD ports).

For the X11 selection that is automatically copied when you select something with the mouse:

xsel | less
xclip -o | less

For the X11 clipboard that is copied explicitly (typically with Ctrl+C):

xsel -b | less
xclip -o -selection c | less

On macOS, use pbpaste.

pbpaste | less

See Copy the contents of a file into the clipboard without displaying its contents for more information.

1

Under X11 this won't work because of the way the clipboards are integrated.

First of all, there are two clipboards in use:

  1. Something you selected
  2. Something you copied with a hotkey (like ctrl + c)

Programs can use both or either of those clipboards.

The whole process works a bit like this:

Client A                    X Server                    Client B
----------------------------------------------------------------

(1) |  I own selection FOO!    |
    |  ------------------->    |


                               |  Write sel. FOO to BAR!  | (2)
                               |  <---------------------  |


    | Write sel. FOO to BAR!   |
    | <---------------------   |


    |     Here is FOO.
    | -------------------------:----------------------->  |


                                     Okay, got it.        |
    | <------------------------:------------------------  |

(source)

If you want to use clipboard content in your terminal workflow, you could use something like xclip and alias them to a command of your choice.

Simon
  • 309
1

Here's a more low-tech way:

You've copied something, right? So if you hit Shift-Insert or whatever, it will paste, right?

$ cat | less

Now press Shift+Insert then Ctrl+C

Voila! You can now scroll up and down and search and do whatever you want with less, operating on the clipboard text you pasted in.

Note that Ctrl+D will not work to terminate the input to cat in this case. I'm not sure why, but it didn't on FreeBSD nor on Ubuntu.

Jim L.
  • 7,997
  • 1
  • 13
  • 27