Tips&Tricks

How to copy from a terminal to the Linux or MacOS system clipboard

keyboard

When you use a clipboard of your desktop environment (DE), you may think, that it’s some global buffer. But it’s not it works inside DE. For example, come terminal utils use their own buffers – vim has its own buffers for working inside the editor.

But we have commands which you can use to copy and paste from / into desktop environment.

Working with MacOS clipboard

Open a terminal and do:

$ tail /var/log/system.log | pbcopy

Now the last 10 lines of the log file are in the clipboard.

To put it somewhere else, that is, to paste it, do:

$ pbpaste > ~/Documents/somefile.txt

Copy / Paste from Linux clipboard

You can do the same on Linux but with another commands:

pwd | xclip -selection clipboard
xclip -selection clipboard -o | cat

Or even create you own pbcopy and pbpaste aliases for GNU/Linux (as we did in “Bash aliases and startup commands“):

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'