While I was learning more about Fabric (open-source AI framework) I learned that macOS had two native commands to copy and paste the clipboard content into the terminal: pbcopy
and pbpaste
. Their names comes from “pasteboard”, which is the term used in macOS for clipboard.
Since these two commands don’t work on Linux and would impact directly the way that I used command line tools, I’ve decided to see how to implement them.
Solution: Create an alias using another Linux command
After doing some research, I found xsel to be the best solution for me using X11 (you can check this in: Settings > System > System Details > Windowing System
).
First you need to install it. Here is the command for Ubuntu/Debian distributions:
sudo apt install xsel
BashThen you need to edit your .bashrc
file and add the following lines at the bottom:
# Linux version of macOS pbcopy and pbpaste
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
BashA quick explanation of the commands arguments:
--clipboard
is to make it operate on the CLIPBOARD selection. As an alternative you leave out this argument to make it operate on the primary selection (so you don’t need to copy a content, just select it);--input
and--output
is to enabled it to read or write the standard input or output into the selection. This is required if we passing the content from one command to another.
Restart your terminal or run the command bellow:
source ~/.bashrc
BashNow you can use pbcopy
and pbpaste
in your terminal, here is an example:
kossmann@abracadabra:~$ pbpaste
This text was copied from a website
kossmann@abracadabra:~$ echo "This is a terminal output" | pbcopy
kossmann@abracadabra:~$ pbpaste
This is a terminal output
BashAlternative solution for Wayland
I found the project wl-clipboard that implements two command-line Wayland clipboard utilities, wl-copy
and wl-paste
, that do the same thing.
What are the reasons for choosing xsel over xclip?
During my research I found out that xclip does not close STDOUT after it has read from the tmux buffer, which can cause various issues (some examples here and here with Sublime Merge).
A fun fact is that xsel has fewer dependencies than xclip:
kossmann@abracadabra:~$ apt-cache depends xsel
xsel
Depends: libc6
Depends: libx11-6
kossmann@abracadabra:~$ apt-cache depends xclip
xclip
Depends: libc6
Depends: libx11-6
Depends: libxmu6
Bash
Leave a Reply