A better copy/paste flow for tmux on macOS

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:

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:

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.