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.
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.
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.
- the simpliest way and no need of any toolsxetra11– xetra112019-10-24 08:01:22 +00:00Commented Oct 24, 2019 at 8:01
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.
- 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'– Gilles 'SO- stop being evil'2019-10-23 18:44:06 +00:00Commented Oct 23, 2019 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 waySimon– Simon2019-10-24 08:56:30 +00:00Commented Oct 24, 2019 at 8:56