The clipboard is a temporary storage area for data that you copy and paste. When you copy text or other data, it goes to the clipboard. You can then paste this data somewhere else. The clipboard only holds the most recent item you copied. Managing the clipboard efficiently can help you avoid mistakes and keep your work organized. This detailed guide explains how to manage the clipboard from the command line in Linux.
Table of Contents
Tools for Clipboard Management
Two popular tools for managing the clipboard in Linux are xclip
and xsel
. Both tools allow you to view, clear, and manipulate clipboard contents.
Installing Clipboard Management Tools
Install xclip
Xclip and Xsel are available in the default repositories of many Linux distributions. So you can install them using the default package manager.
For instance, yo can install xclip
on Debian and Ubuntu systems using the following command:
sudo apt install xclip
Install xsel
To install xsel
, use this command:
sudo apt install xsel
Manage Clipboard from Command line in Linux
1. Viewing Clipboard Contents
Using xclip
:
To view the contents of the clipboard with xclip
, use:
xclip -selection clipboard -o
This command outputs the current content of the clipboard to the terminal.
Using xsel
:
To view the clipboard content with xsel
, run:
xsel --clipboard --output
Or, just:
xsel --clipboard
This command displays the clipboard's current contents.
2. Clearing the Clipboard
Using xclip
:
To clear the clipboard with xclip
, use:
xclip -selection clipboard /dev/null
This command empties the clipboard.
Using xsel
:
To clear the clipboard with xsel
, run:
xsel --clipboard --clear
This command clears the clipboard content effectively.
3. Copying Data to the Clipboard
Using xclip
:
To copy data to the clipboard using xclip
, you can echo the data and pipe it to xclip
. For example:
echo "Hello, OSTechNix Users!" | xclip -selection clipboard
This command copies "Hello, OSTechNix Users!" to the clipboard.
Using xsel
:
To copy data to the clipboard using xsel
, you can use a similar approach:
echo "Hello, OSTechNix Users!" | xsel --clipboard
This command also copies "Hello, OSTechNix Users!" to the clipboard.
4. Pasting Data from the Clipboard
Pasting data is same as viewing. I have included this for the sake of understanding.
Using xclip
:
To paste the clipboard contents using xclip
, you can run:
xclip -selection clipboard -o
This command outputs the clipboard content, which you can redirect or use in a script.
Using xsel
:
To paste the clipboard contents using xsel
, run:
xsel --clipboard --output
This command displays the clipboard content.
Clipboard in Different Selections
Unlike Windows, which typically has a single clipboard, Linux supports multiple clipboard selections. They are:
- PRIMARY (selected text)
- SECONDARY (rarely used)
- CLIPBOARD (Ctrl+C, Ctrl+V operations)
By default, xclip
and xsel
use the CLIPBOARD
selection. You can specify different selections as needed.
Using xclip
:
- Primary selection:
xclip -selection primary
- Secondary selection:
xclip -selection secondary
- Clipboard selection:
xclip -selection clipboard
Using xsel
:
- Primary selection:
xsel --primary
- Secondary selection:
xsel --secondary
- Clipboard selection:
xsel --clipboard
Advanced Usage
1. Copying File Contents to Clipboard
To copy the contents of a file to the clipboard, you can use:
Using xclip
:
xclip -selection clipboard < filename.txt
Using xsel
:
xsel --clipboard < filename.txt
2. Redirecting Clipboard Content to a File
To save the clipboard content to a file, use:
Using xclip
:
xclip -selection clipboard -o > output.txt
Using xsel
:
xsel --clipboard --output > output.txt
3. Working with Multiple Clipboards
Copy to PRIMARY selection:
echo "Primary Selection" | xclip -selection primary
Copy to SECONDARY selection:
echo "Secondary Selection" | xclip -selection secondary
4. Clipboard Persistence
By default, xclip exits immediately, which can cause the clipboard content to be lost. To keep it running:
echo "Persistent clipboard" | xclip -selection clipboard -loops 0
This will keep xclip running until you manually terminate it.
5. Clipboard Monitoring
To continuously monitor clipboard changes:
watch -n 1 "xclip -selection clipboard -o"
This command will display the clipboard contents every second.
Practical Examples
1. Creating a Quick Paste Bin
alias pb="xclip -selection clipboard" cat file.txt | pb
Now you can quickly copy file contents to clipboard using cat file.txt | pb
.
2. Clipboard to File with Timestamp
xclip -selection clipboard -o > clipboard_$(date +%Y%m%d_%H%M%S).txt
This saves the current clipboard content to a file with the current timestamp.
3. Clipboard History
To maintain a simple clipboard history:
echo "$(xclip -selection clipboard -o)" >> ~/.clipboard_history
Add this to your .bashrc
or create a cron job to periodically save clipboard contents.
Troubleshooting
- If xclip or xsel commands fail, ensure you're running them in an X11 session.
- For headless servers, you might need to use a virtual framebuffer like Xvfb.
- If clipboard contents are lost unexpectedly, check if any clipboard manager is interfering.
Summary
Managing the clipboard from the command line in Linux is straightforward with tools like xclip
and xsel
. You can view, clear, copy, and paste clipboard contents easily. Additionally, you can handle different clipboard selections and work with file contents.
Mastering xclip
and xsel
commands will help you manage your clipboard effectively and prevent accidental pastes. Additionally, mastering clipboard management in the Linux command line enhances productivity and enables powerful scripting capabilities.
While xclip and xsel are the primary tools, many desktop environments also offer their own clipboard managers with additional features.
Remember that clipboard operations in Linux are typically tied to the X server session. For system-wide or cross-session clipboard management, consider using dedicated clipboard manager applications.
Related Read:
- How To Use Pbcopy And Pbpaste Commands On Linux
- How To Manage Clipboard Contents With CopyQ In Linux
- Access Clipboard Contents Using Xclip and Xsel In Linux
- Copy File Contents Into Clipboard Without Displaying Them In Linux
Featured image by Mohamed Hassan from Pixabay.