less than 1 minute read

It is possible to control the clipboard (copy and paste) from the command line in linux and OSX. In linux, the command is xsel, and in OSX, pbcopy/pbpaste.

For example, someone IMed me and asked for a subversion URL. Normally, I would type “svn info” in a terminal, use the mouse to select the URL, and press Ctrl-Insert to copy. Instead, I can just run this command:

svn info | grep URL | xsel --clipboard

Then, I can Alt-Tab back to the IM window and press Ctrl-v. There is no wasted time reaching for the mouse.

The—clipboard argument is required. Without it, xsel acts on the currently selected text in the terminal rather then the clipboard.

In OSX, the equivalent command is:

svn info | grep URL | pbcopy

pbcopy/pbpaste come with OSX. In debian flavors of linux, the following command will install xsel:

sudo apt-get install xsel

xsel can also be used to paste the clipboard. For example:

% echo ls | xsel --clipboard

% xsel --clipboard

% `xsel --clipboard`
bin    dev   initrd      lost+found    mnt   root  sys  var