If you recently switched from Mac to Linux, you might miss few commands that used to work on your Mac terminal. One such command pair is pbcopy and pbpaste, which allow you to copy and paste text from and to the clipboard.
In this article, we will show how you can use similar commands on Linux by installing some utilities and creating aliases for them.
pbcopy and pbpaste command
The pbcopy command simply copy the standard input (stdin) to clipboard buffer. You can use pbpaste command to output clipboard contents.
For example, to copy the output of ls
command into the clipboard, you can simply pipe it to pbcopy
as shown:
ls | pbcopy
Then, you can view clipboard contents using pbpaste
like this:
pbpaste
These commands are very useful to increase productivity in command line and makes mouse use less frequent.
How to install pbcopy (xclip) and pbpaste (xsel) in Linux?
Unfortunately, pbcopy and pbpaste tools are not available for Linux. However, we can replicate their functionality using some popular opensource utilities that can interact with the clipboard (daemon).
Two such utilities are xclip
and xsel
, which are available in the default repositories of most Linux distributions.
Here is a helpful table with commands to install xclip and xsel:
Distribution |
Command |
Debian/Ubuntu |
sudo apt install xclip xsel |
Fedora/CentOS/Red Hat |
sudo dnf install xclip xsel |
Arch/Manjaro/Antergos |
sudo pacman -S xclip xsel |
You don’t need to install both utilities, just choose one that suits your preference.
How to use with xclip and xsel command?
Both xclip and xsel command have many options and flags to alter their functionality.
We can use the below commands to copy and paste using xclip
:
`
xclip -selection clipboard # copy
xclip -selection clipboard -o # paste
Similarly, to copy using xsel
utility, we use:
xsel --clipboard --input # copy
xsel --clipboard --output # paste
Now, let's see a more productive way to do the same.
Alias for xclip and xsel commands in Linux
In Linux, we use shell aliases for productivity. If you use a long commands with multiple flags multiple times then it is wise to use an alias for the original command.
To do this, we need to edit ~/.bashrc
file, which is a script that runs every time we open a new shell as that respective user. You can use any text editor of your choice, but we will use nano for simplicity.
nano ~/.bashrc
Then, scroll down to the end of the file and add these lines depending on which tool you installed.
If you installed xclip, add these lines:
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
If you installed xsel, add these lines:
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
Save and close the file by pressing Ctrl+O followed by Enter, then Ctrl+X.
Finally, reload your ~/.bashrc
file by running:
source ~/.bashrc
Alternatively, you can close and reopen your terminal.
Use pbcopy and pbpaste commands on Linux?
Now that we have created pbcopy and pbpaste alias, we can use them exactly as we would on Mac.
For example, we can copy the output of a command to clipboard by piping it to pbcopy
:
date | pbcopy
Then, we can paste the clipboard contents using pbpaste as follows:
pbpaste
Also, to copy contents of a file, we can redirect the file file.txt
to pbcopy
as shown:
pbcopy < file.txt
We can also combine these commands with other commands to perform various tasks.
For example, you can copy a remote file from a server using ssh into our local clipboard as shown:
ssh user@server 'cat /path/to/file' | pbcopy
Then, we can paste it into a local file using redirection like this:
pbpaste > file.txt
Q. Copy and paste image with xclip command in Linux?
Yes, we can copy and paste binary data with xclip
(more powerful than xsel
).
xclip -selection -clipboard -t image/jpeg 1.jpg
Basically, we use -t
flag with "image/jpeg
" to specify the type of binary data.
FAQs
Here are common questions you might have.
Q. How to clear clipboard contents using Linux command line?
To clear the clipboard contents, you can simply copy an empty string into.
echo -n "" | pbcopy
This will overwrite the clipboard contents with nothing.
Q. How to append text to the clipboard contents?
You can use the -append
option with xsel
commands.
echo "Hello" | pbcopy -append
The above command will add “Hello” to the end of the existing clipboard contents.
Conclusion
These commands are very handy for copying and pasting text between different applications without using the mouse or keyboard shortcuts. We hope you found this article useful and informative. Thank you for reading!