Home > bash > Copy string to X clipboard in shell

Copy string to X clipboard in shell

This entry is based on the post Copy Shell Prompt Output To Linux / UNIX X Clipboard Directly.

Problem

Under Linux, you want to copy a given text to the X clipboard. How to do that?

Three different clipboards

First of all, we should know that under X there are three clipboards :)

  • “primary”: The primary one’s paste operation is usually accessed with the middle mouse button.
  • “secondary”: This one didn’t really work for me…
  • “clipboard”: Its paste operation is accessed with Shift + Insert (or right mouse click and Paste in popup menus).

The xclip command

Reads from standard in, or from one or more files, and makes the data available as an X selection for pasting into X applications. Prints current X selection to standard out.

Installation:

sudo apt-get install xclip

Copy a string to the clipboard. By default, xclip copies to the “primary” clipboard.

echo "this is a test" | xclip
echo "this is a test" | xclip -selection primary
echo "this is a test" | xclip -selection p

The three variations are equivalent.

Copy a string to the “clipboard” (3rd clipboard).

echo "another test" | xclip -selection clipboard
echo "another test" | xclip -selection c

The two variations are equivalent.

Copy the contents of a file to the clipboard.

cat file.txt | xclip

Print the contents of the clipboard to the standard output.

xclip -o

Of course, you can use redirection.

xclip -o > out.txt

The xsel command

Retrieve and set the X selection.

xsel is similar to xclip, but it offers some additional features like append, clear, etc.

Installation:

sudo apt-get install xsel

Copy a string to the primary clipboard (default).

echo "1" | xsel
echo "1" | xsel -p

The two variations are equivalent.

Copy a string to “clipboard” (3rd clipboard).

echo "1" | xsel -b

Copy the contents of a file to the clipboard.

cat file.txt | xsel

Print the contents of the clipboard to the standard output.

xsel -o

Homework

Find a solution to copy a string to the “primary” and “clipboard” clipboards, i.e. on both. Hint: “echo homework | xsel -p -b” and “echo homework | xclip -selection p -selection c” won’t work. Post your solution in the comments.

Advertisements
Categories: bash Tags: , , ,
  1. March 5, 2011 at 20:08
  2. guri
    January 9, 2012 at 09:54

    hi
    i’m using radhat environment but both command don’t work for me .
    how can I do it ?

    • January 9, 2012 at 11:18

      Can you provide some more info? What do you mean by “they don’t work”? Do they exist under Redhat? If they exist, they don’t copy anything to the clipboard? Explain.

  3. guri
    January 9, 2012 at 12:40

    yes ; it’s seem the commands don’t exist …

    • January 9, 2012 at 13:46

      I’m not an expert of RPM-based distros. I’m sure Redhat has its own package manager where you can select these packages for installation. I also googled it and I found the following: “sudo yum install xsel“. Give it a try.

  1. March 5, 2011 at 20:04

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: