Tmux – How to copy to system clipboard

Welcome Tmux users! Tmux is a terminal multiplexer loved by some, hated by others, and used by many. Tmux users know that it has some flaws, yet we choose this multiplexer among others, but there is one fatal flaw amongst them all. If you ended up here, that means that you are struggling with the same issue as most of us. The million dollar question:

HOW DO I USE MY CLIPBOARD WITH TMUX?!

This is something that most of us try to figure out. After hours of researching, struggling, and playing with trial and error, I ended up with a solution on how to use the beloved clipboard. This solution that I will share with you all. For this series of instructions I will be using Linux Mint, but this solution should apply to most flavors of Linux. This tutorial also assumes that you already have Tmux installed on your machine and that you are running these commands on Tmux.

Instructions to allow clipboard on Tmux

1. First install Tmux Plugin Manager

TPM is an essential tool that allows us to install plugins for Tmux. This is the first tool that we will need to succeed. You can find the repository on the following Github Link. You will clone the repo to the machine with the command. In my case, I will save the repository on the following location of my home folder: ~/.tmux/plugins/tpm .

The command I used is

git clone https://github.com/tmux-plugins-tpm ~/.tmux/plugins/tpm

After cloning the repository, we need to add some lines to our .tmux.conf file. Select your editor of choice and add the following lines at the end of your file:

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

run '~/.tmux/plugins/tpm/tpm'

The last line should be ~./Your/location/tpm/tpm. After adding these lines to your .tmux.conf file, run the following command on Tmux to reload the .tmux.conf file:

tmux source ~/.tmux.conf

2. Install Tmux-Yank plugin

The second magic tool we need is called Tmux-Yank. You can find it in the following Github Link. Since we already installed TPM, we can do the following to install Tmux-Yank.

On Tmux, open your .tmux.conf file, add the following line after the lines we previously added:

set -g @plugin 'tmux-plugins/tmux-yank'

Once you added that line. Close and open Tmux again. Once tmux is open, on your keyboard press the following:

prefix + I 

That is capital i. This command will install the plugin. Now you should be able to copy from Tmux to your clipboard.

3. Copying to Clipboard from Tmux

After following the previous steps, you should be able to copy from Tmux to your clipboard. Now the way Tmux-Yank works is the following. First, you enter selection mode. Quick reminder, to enter selection mode we use:

prefix + [

After entering selection mode, navigate to the text you want to select. Then using the command:

prefix + space

Select the text you want to copy. In my case, I’m going to select the text that says .python_history.

Once you selected the text, you can copy text by pressing the key y. This will send the selected text to your clipboard. Then paste using regular paste mode where you want to paste the selected text.

Possible errors!

Error: Tmux-yank dependencies not installed

If you got this error, that means that your computer doesn’t have a program that allows to store data in the system clipboard. You can fix this by installing xsel.

After installing xsel, Tmux should stop presenting this error.

Nothing is copying to the clipboard

If nothing is copying to the clipboard after following all the steps, you can manually install Tmux-yank.

1. Clone the repository

You can clone the repository to your machine using the command:

git clone https://github.com/tmux-plugins/tmux-yank /location/to/save

The path /location/to/save is where you want to clone the repo.

2. Adding the line to the bottom of .tmux.conf

The following line has to be added to the file:

run-shell /location/to/save/yank.tmux
3. Reloading tmux.

After doing the previous steps, reload Tmux using the command:

tmux source-file ~/.tmux.conf

Wrap up

I hope this tutorial helps you out to maximize the potential of Tmux. Good luck and happy multiplexing!