How to Use the Command 'xclip' (with Examples)
- Linux
- December 17, 2024
‘xclip’ is a versatile command-line utility for interacting with the X11 clipboard system on Unix-like operating systems. It is analogous to tools like ‘xsel’, and allows users to copy and paste text and other types of data with ease. This command supports manipulation of the X primary and secondary selections, as well as the system clipboard (associated with ‘Ctrl + C’ / ‘Ctrl + V’). Such capabilities make ‘xclip’ exceptionally useful in various scripting scenarios and workflows where clipboard operations are required.
Below, we explore different use cases of the ‘xclip’ command, demonstrating its flexibility and power.
Copy the Output from a Command to the X11 Primary Selection Area (Clipboard)
Code:
echo 123 | xclip
Motivation: This use case allows you to directly capture the output of a command and store it in the X11 primary selection area. This is particularly useful for quickly sharing command output without needing to manually copy it.
Explanation:
echo 123
: This command outputs the text ‘123’.|
: This pipe operator passes the output ofecho 123
to the next command,xclip
.xclip
: By default,xclip
will copy the input to the X11 primary clipboard.
Example Output: When you middle-click in a text field, ‘123’ will be pasted.
Copy the Output from a Command to a Given X11 Selection Area
Code:
echo 123 | xclip -selection clipboard
Motivation: This example demonstrates how to specify which X11 selection area the content should be copied to. It is useful for users who require specific control over the clipboard interaction, such as when preparing data to be used across different applications.
Explanation:
-selection clipboard
: This option specifies that the data should be copied to the system clipboard, making it accessible with ‘Ctrl + V’.
Example Output: Using ‘Ctrl + V’ in a text field will paste ‘123’.
Copy the Output from a Command to the System Clipboard Using Short Notation
Code:
echo 123 | xclip -sel clip
Motivation: This short notation provides a more concise way to copy data to the system clipboard, appealing to users who prefer brevity in their command lines.
Explanation:
-sel clip
: This abbreviated form of-selection clipboard
indicates the usage of the system clipboard.
Example Output: Pasting using ‘Ctrl + V’ will insert ‘123’ into your target application.
Copy the Contents of a File into the System Clipboard
Code:
xclip -sel clip input_file.txt
Motivation: This is particularly useful when you need to copy all the content of a file directly into the clipboard for use in other applications or systems without needing a text editor.
Explanation:
input_file.txt
: The name of the file whose contents are being copied.-sel clip
: Specifies the use of the system clipboard.
Example Output: The entire content of input_file.txt
will be pasted when ‘Ctrl + V’ is used.
Copy the Contents of a PNG into the System Clipboard
Code:
xclip -sel clip -t image/png input_file.png
Motivation: This use case demonstrates how to handle non-textual data, like images, allowing users to copy and paste images seamlessly between applications.
Explanation:
-t image/png
: This specifies the content type (MIME type) being handled, indicating toxclip
that the file is an image.input_file.png
: The image file to be copied.
Example Output: Pasting will insert the image from input_file.png
into a compatible application, like an image editor or email client.
Copy the User Input in the Console into the System Clipboard
Code:
xclip -i
Motivation: This usage is beneficial when you want to capture dynamic input directly from the terminal to the clipboard, enabling quick transfer of text data inputted by the user.
Explanation:
-i
: This tellsxclip
to take data from the standard input.
Example Output: After typing text into the terminal, it can be pasted into another application.
Paste the Contents of the X11 Primary Selection Area to the Console
Code:
xclip -o
Motivation: This use case allows users to retrieve and view the content stored in the primary selection directly in the terminal, facilitating quick verification or reuse of clipped data.
Explanation:
-o
: This option outputs the current selection to the standard output (console).
Example Output: The text currently in the X11 primary selection will appear in the terminal.
Paste the Contents of the System Clipboard to the Console
Code:
xclip -o -sel clip
Motivation: When you need to verify or repurpose clipboard content from the system clipboard directly in the terminal, this use case is ideal.
Explanation:
-o
: Outputs the current clipboard selection.-sel clip
: Specifies the use of the system clipboard.
Example Output: The content from the system clipboard is displayed in the terminal.
Conclusion:
‘xclip’ offers a robust and flexible way to manage clipboard data within X11 environments, demonstrating usefulness across a wide spectrum of scenarios, from scripting to daily productivity tasks. Whether handling text or binary data, ‘xclip’ provides an efficient means of clipboard interaction, enhancing workflows and saving time.