How to Manage Clipboard from Command Line in Linux

The clipboard is a fundamental tool in computing that allows users to easily copy and paste text and files between different applications. In Linux, the clipboard is managed through a command-line utility called xclip. xclip allows users to interact with and manipulate the clipboard without having to rely on graphical user interfaces.

In this article, we will explore how to manage the clipboard from the command line in Linux using xclip.

Installation
Before we can start using xclip, we need to install it on our Linux system. To install xclip, open a terminal and run the following command:

sudo apt-get install xclip

Alternatively, if you are using a different package manager, you can search for and install xclip using the appropriate command.

Copying Text to the Clipboard
To copy text to the clipboard using xclip, simply pipe the text to the xclip command. For example, to copy the text "Hello, World!" to the clipboard, run the following command:

echo "Hello, World!" | xclip

Now, you can paste the text "Hello, World!" into another application by using the standard paste shortcut (usually Ctrl+V).

Pasting Text from the Clipboard
To paste text from the clipboard using xclip, run the following command:

xclip -o

This will output the text currently stored in the clipboard. You can redirect the output to another command or a file as needed.

Clearing the Clipboard
To clear the contents of the clipboard using xclip, run the following command:

echo -n | xclip -selection clipboard

This command will clear the clipboard of any stored text.

Managing Multiple Clipboards
xclip supports multiple clipboards, which can be useful for managing different types of data. By default, xclip uses the "primary" clipboard. To copy text to a different clipboard, use the -selection option. For example, to copy text to the "clipboard" clipboard, run the following command:

echo "This is another clipboard" | xclip -selection clipboard

To paste text from a specific clipboard, use the -selection option as well. For example, to paste text from the "clipboard" clipboard, run the following command:

xclip -o -selection clipboard

Conclusion
Managing the clipboard from the command line in Linux using xclip provides a convenient and efficient way to copy, paste, and clear text. By mastering xclip, users can streamline their workflow and perform clipboard operations without relying on graphical interfaces. Try experimenting with the different options and features of xclip to discover its full potential for managing the clipboard in Linux.