Skip to content

How to use pbcopy on Ubuntu

pbcopy on a mac enables you to copy the standard input from terminal window to your clipboard enabling you to paste it to other applications.

This functionality is available by default on Ubuntu/Linux but can be easily replicated using available Linux functionality.

You can replicate pbcopy on ubuntu by utilising a similar tool called xclip which does exactly the same.  However its syntax is a little too verbose and I prefer to use OS X pbcopy command.

Fortunately due to the fact that OS X and Linux *nix based we can make use of the The alias Command to replicate the pbcopy functionality in ubuntu.

how to configure pbcopy on ubuntu

In this tutorial we will be making use of the Terminal window and you may want to learn How To Use The Linux Terminal Window On Ubuntu

If you haven't previously installed xclip simply run the following command in your terminal window:

sudo apt-get install xclip -y

Edit your BASH settings file using your favourite text editor.  I'll be using nano, but feel free to use Gedit or Vim etc.

nano ~/.bashrc

Then create an alias for pbcopy and pbpaste:

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

Advice

Ideally you should place all custom aliases in the ~/.bash_aliases file so not to pollute your ~/.bashrc too much.

However, for the sake of this tutorial we will place them in bashrc.

Close and save the file then just refresh your bash to import your new settings

source ~/.bashrc

Script for pbcopy on ubuntu

I have created a quick script to help automate the configuration of pbcopy on ubuntu.

#!/bin/sh
# Copyright (C) 2009-2017 Three Nine Consulting
#  Always good practice to update packages. However ask user if they would like to do so
#  For explanation on how this works and why check out https://garywoodfine.com/use-pbcopy-on-ubuntu/
read -p "Do you want to update your package repositories before proceeding ? " -n 1 -r
echo  #adding new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
   sudo apt update
   sudo apt upgrade -y
   sudo apt autoremove -y
fi



#  Check to see if Xclip is installed if not install it
if [ $(dpkg-query -W -f='${Status}' xclip 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  echo 'xclip not installed .... installing now!'
  sudo apt install xclip -y;
fi
#  Add the aliases to the .bashrc
echo 'updating bash profile'
echo "#pbcopy & pbpaste aliases" >> ~/.bash_aliases
echo "alias pbcopy='xclip -selection clipboard'" >> ~/.bash_aliases
echo "alias pbpaste='xclip -selection clipboard -o'"  >> ~/.bash_aliases

Please bear in mind, that due to the fact the script will be installing new software it will require elevated permissions to run. Typically I execute the script

sudo bash pbcopyfy

# Then after the script has  completed. I refresh the terminal with
source  ~/.bashrc

For further information on How to execute a BASH script

The Linux Programming Interface

A Linux and UNIX System Programming Handbook

Definitive guide to the Linux and UNIX programming interface the interface employed by nearly every application that runs on a Linux or UNIX system.

how to use pbcopy on ubuntu

After you have finished the configuration steps above you can simply use pbcopy on your ubuntu machine in much the same way you'll utilize it on the mac.

pbcopy < ~/.ssh/id_rsa.pub 

This will copy your ssh public key and you can test if this works by pasting it into a Gedit text file.
This functionality also comes in very handy in situations when you need to copy the contents of your php info. For instance, when you need to configure xdebug to enable debugging for PHP.

You could use xclip to copy the contents

php -i | xclip -selection clipboard

Or if you have taken the time to configure the short cut above you can make the command a lot easier to remember by removing some of the verbosity.

php -i | pbcopy

Summary

This is an example of how flexible, adaptable and customizable the Linux Operating system is. Simply utilising the The alias Command you're easily able to replicate functionality found on the MacOSX directly on your Linux system.

This provides you with the capability to customise your system to how you prefer to work with it. Taking time to learn and understand the Aliases in Linux will help you to tailor your system and decrease the amount of typing you need to do.

Gary Woodfine
Latest posts by Gary Woodfine (see all)