2

I would like to use the command line to put text in the GUI clipboard so that it can, for example, be pasted into a graphical web browser's text input field. I am using Kubuntu 20.04.

I tried as an example uptime | xclip, and when I press the middle mouse button, the uptime output is pasted. However, when I press Ctrl-Shift-V on the command line, or Ctrl-V in a GUI application, the uptime text is not pasted; instead, the previously copied text is pasted.

I read that there is a distinction between the selection and the clipboard. I presume xclip is copying into the selection. How can I copy into the clipboard?

2 Answers2

3

Personally, I prefer XSel to xclip, for pretty much the same reason: the manual makes it easier to see how to toggle which selection to copy to.

After installing it, try this:

uptime | xsel -ib

By the way, once you get used to there being both a primary selection and a clipboard, you'll find that it's really handy having both. The primary selection is often faster to work with, and it's also nice to be able to store two different items of information simultaneously. Furthermore, if you have a gaming mouse, you can use ratbagd/piper to bind the spare keys to manipulating the selections, eg swap the primary and clipboard using mouse button 4 with the thumb.

cryptarch
  • 1,270
3

There are two sets of commands that can do this, xclip and xsel, and they can be used interchangeably. In order to use the clipboard used by graphical applications (rather than the terminal selection buffer), an option must be specified.

To copy into the clipboard:

uptime | xclip -selection clipboard
# or
uptime | xclip -sel clip
# or
uptime | xsel -ib

To paste from the clipboard on the command line:

xclip -o -selection clipboard
# or
xclip -o -sel clip
# or
xsel -ob

If typing on the command line, xsel is faster to type; if assigning to an alias, or including in a script, then the verbose xclip form is more intention-revealing.