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 echo
d etc.

- 546
3 Answers
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.

- 829,060
Under X11 this won't work because of the way the clipboards are integrated.
First of all, there are two clipboards in use:
- Something you selected
- 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.

- 309
-
I'm not sure what you're referring to in the first sentence, but the existence of xclip shows that it's possible. – Gilles 'SO- stop being evil' Oct 23 '19 at 18:44
-
it works if there is a program acting as someone pasting the stuff and providing it in a file or some other way – Simon Oct 24 '19 at 08:56
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.

- 7,997
- 1
- 13
- 27