Universal copy-paste for Linux, Windows, macOS

· forensicks's blog


Are you used to macOS but now you're fancying Linux?

First, ensure you have xclip installed on your machine.

If youre are on a Debain/Ubunto-based Linux distro, run this command:

1sudo apt install xclip -y

If your machine is an Arch Linux-based operating system, instead run this command:

1sudo pacman -S xsel

Now, add these aliases to your ~/.bashrc, ~/.zshrc, ~/.aliasrc, etc. …

1alias pbcopy="xclip -selection clipboard"
2alias pbpaste="xclip -selection clipboard -o"

Now, open a new tab in your terminal.

Let's try copying some text:

1echo 'xyz' | pbcopy

Now let's try pasting from your clipboard:

1pbpaste

You should see this output:

1xyz

¡Voilà!


Or, for a more cross-platform implementation that works on macOS, Linux, and Windows, add this to your profile script:

 1#!/bin/bash
 2
 3if grep -q -i microsoft /proc/version; then
 4  # on WSL: version contains the string "microsoft"
 5  alias copy="clip.exe"
 6  alias paste="powershell.exe Get-Clipboard"
 7elif grep -q -i cygwin $(uname -a); then
 8  # on CYGWIN: uname contains the string "cygwin"
 9  alias copy="/dev/clipboard"
10  alias paste="cat /dev/clipboard"
11elif [[ ! -r /proc/version ]]; then
12  # on MAC: version is not readable at all
13  alias copy="pbcopy"
14  alias paste="pbpaste"
15else
16  # on "normal" linux
17  alias copy="xclip -sel clip"
18  alias paste="xclip -sel clip -o"
19fi

Copying #

1# pipe
2echo "hello world" | copy
3
4# or for direct file input
5copy < file

Pasting #

1paste > file

Credits: This Stack Overflow answer, by winklerrr.