Introduction to xclip

xclip is a command-line utility that allows you to interact with the X11 clipboard directly from the terminal. This tool is particularly useful in Linux environments that use the X Window System, such as Ubuntu. With xclip, you can easily copy output from the terminal to the clipboard, paste text from the clipboard to the terminal, and even automate tasks to improve your overall productivity.

Since you're new to the command line (CLI), this guide will provide a detailed tutorial on xclip to help you understand its functionality and how you can use it to boost your workflow in Ubuntu.


Table of Contents

  1. Introduction to the Clipboard in Linux

  2. Installing xclip on Ubuntu

  3. Basic Usage of xclip

    • Copying Text to the Clipboard

    • Pasting Text from the Clipboard

  4. Understanding the Selection Types in X11

  5. Practical Examples

    • Copying Command Output

    • Using xclip with Files

  6. Advanced Usage

    • Automating Tasks with xclip

    • Piping Input and Output

  7. Improving Productivity with xclip

    • Automating Workflows

    • Integrating with Scripts

  8. Summary and Key Takeaways


1. Introduction to the Clipboard in Linux

Linux systems, particularly those using the X Window System (X11), have multiple clipboards. The most common ones are:

  • Primary Selection: Text selected by highlighting with the mouse.

  • Clipboard: Explicitly copied text, similar to how Ctrl + C works in most systems.

  • Secondary Selection: Rarely used.

These selections allow for different ways to copy and paste content between applications. xclip lets you control which selection you interact with, making it versatile for various use cases.


2. Installing xclip on Ubuntu

Step 1: Open the Terminal

Press Ctrl + Alt + T to open the terminal on Ubuntu.

Step 2: Install xclip

You can install xclip via the Ubuntu package manager apt. Run the following command:

Enter your password when prompted, and Ubuntu will install xclip on your system.

Step 3: Verify Installation

After installation, you can verify if xclip is installed successfully by typing:

You should see output similar to:

Now that you have xclip installed, let's explore how to use it.


3. Basic Usage of xclip

3.1 Copying Text to the Clipboard

To copy text or output from a command to the clipboard, you can use xclip in conjunction with the echo command or any command that generates output.

For example, to copy the string "Hello, World!" to the clipboard:

  • The -selection clipboard option specifies that the text should be copied to the clipboard selection (the same one used by Ctrl + C).

Now, if you open a text editor or any other application, you can paste the text using Ctrl + V as you would normally.

3.2 Pasting Text from the Clipboard

To paste the clipboard contents back into the terminal, use the -o (output) option:

This command will print whatever is currently stored in the clipboard to the terminal.


4. Understanding the Selection Types in X11

As mentioned earlier, the X Window System has different types of selections:

  • Primary Selection (-selection primary): This is used for text that is highlighted by the mouse. You can paste it by middle-clicking the mouse.

  • Clipboard Selection (-selection clipboard): This is used for text copied explicitly using keyboard shortcuts like Ctrl + C or via xclip.

  • Secondary Selection (-selection secondary): Rarely used but available.

By default, if you don't specify the selection, xclip uses the primary selection. To interact with the standard clipboard, always use -selection clipboard.

Example: Copying the output of the date command to the clipboard:

You can now paste the date anywhere by pressing Ctrl + V.


5. Practical Examples

5.1 Copying Command Output

Imagine you want to quickly copy the output of a command like ls (which lists files in a directory) to the clipboard:

This command copies the list of files and directories to the clipboard, which can be pasted into an email, document, or other applications.

5.2 Using xclip with Files

You can also copy the contents of an entire file to the clipboard. For example, to copy the contents of a text file named example.txt:

Similarly, to paste the clipboard contents into a file:

These examples show how xclip can be used for file handling and can save time by avoiding manual copy-paste actions.


6. Advanced Usage

6.1 Automating Tasks with xclip

One of the major productivity boosters with xclip is the ability to automate repetitive tasks. For instance, if you regularly need to copy a specific system log, you can create a shell script to do it for you:

You can save this as a script and run it whenever you need to copy the system log. Automating such tasks reduces the time spent on manual actions and improves efficiency.

6.2 Piping Input and Output

xclip can be used with other command-line tools that accept input/output from pipes. This allows for more complex workflows. For example, you can filter command output with grep and copy only the relevant lines:

This copies only the lines containing the word "error" from the system log to the clipboard.


7. Improving Productivity with xclip

Here are some ways to boost your productivity using xclip:

7.1 Automating Workflows

You can create shell scripts that use xclip to automate routine tasks. For example, if you frequently need to copy system statistics, you can write a script that copies the output of multiple commands into the clipboard at once:

This script gathers disk usage, memory usage, and running processes, then copies the information to the clipboard in one go. You can run this script whenever you need to send system information to someone.

7.2 Integrating with Scripts

You can use xclip in any script where you want to automate clipboard interaction. For instance, suppose you're writing a script to back up a directory, and after the backup, you want to copy the backup file path to the clipboard:

This script backs up the /mydirectory folder and copies the path of the backup file to the clipboard so that you can paste it into other tools or documents.


8. Summary and Key Takeaways

xclip is a powerful tool that allows you to interact with the clipboard directly from the terminal in Ubuntu. It supports copying and pasting text, handling files, automating tasks, and integrating clipboard operations into scripts.

Key points:

  • Clipboard Selections: Learn how X11 selections work and use the correct one (-selection clipboard) for copying and pasting with keyboard shortcuts.

  • Automating Workflows: Save time by creating scripts that automate repetitive tasks involving clipboard operations.

  • Piping Input/Output: Use xclip with other command-line tools to handle more complex tasks efficiently.

By mastering xclip, you can significantly improve your productivity in Ubuntu's CLI environment, making it easier to move between terminal tasks and GUI applications without manual intervention.


This guide covers the essentials and advanced uses of xclip, which should help you get started and progressively integrate it into your workflow.

Last updated