A better copy/paste flow for tmux on macOS

Sidharth Shanker
Jan 28, 2018 · 2 min read

I’m a huge fan of tmux, and spend my most of my development time inside a tmux session. Unfortunately, one of the most frustrating things about using tmux is getting copy and paste to work, but thankfully, there are a lot of good solutions. This guide by Carbon Five, is a pretty solid way to get set up on tmux 2.4.

I use the vi-copy mode to copy & paste using tmux, and after upgrading to to tmux 2.4, my current setup broke due to the `vi-copy` directive changing to be `copy-mode-vi`. I decided to use the opportunity to make some improvements to my copy/paste setup.

My setup uses this command to manage copying text:

bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'

What this means is that when I have some text selected in copy mode and hit y, the text is copied, and I am exited from scroll mode. The flow looks like this:

Image for post
Image for post

While this works well, if I want to copy multiple snippets from my tmux pane, I have to keep going back into scroll mode, and scroll back to where I was after every selection.

To solve this, I introduced a second method for copying text:

bind-key -T copy-mode-vi Enter send-keys -X copy-pipe 'reattach-to-user-namespace pbcopy' \; send -X clear-selection

What this does is when I hit Enter with some text selected, it copies the text to my clipboard, and then clears the selection, without dropping me out of scroll mode. This copy flow looks like this:

Image for post
Image for post

What I’ve done here is chain together the two commands copy-pipe and clear-selection. The escaped “\;” is required in order to chain the commands, since an unescaped semicolon would terminate the tmux configuration line.

Let me know if you have other suggestions, and check out the rest of my tmux configuration here.

If you’re new to tmux, I’d highly recommend reading this guide.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store