Last updated

Tmux and Vim: Copy and Paste on macOS Sierra

As a developer, more than anything, you copy and paste things. Making copy and paste work in properly with Vim and Tmux is, unfortunately, not trivial.

There’s a lot of information to be found on the internet about setting up copy and paste with Tmux. There’s also lots of information on how to setup Vim. But then you run Vim 8. On macOS Sierra. And things break down quickly.

This short guide helps you setup Tmux and Vim on macOS Sierra for proper copy pasting glory!

Dependencies

First, you’ll have to install one dependency. I also recommend you install the latest and greatest Vim instead of using the (old) version bundled with macOS, and of course, tmux.

1brew install reattach-to-user-namespace
2brew install vim tmux 

Configure Tmux

Tmux is a weird beast. Copy and pasting can be done in different ways, but I prefer the Vim style navigation.

 1# Configure your default shell, Zsh in my case.
 2set -g default-shell $SHELL 
 3
 4# Override the default command to use `reattach-to-user-namespace` for everything.
 5set -g default-command "reattach-to-user-namespace -l ${SHELL}"
 6
 7# Remap prefix to ctrl-a (or caps-a for my mac)
 8set -g prefix C-a
 9
10# Vim style navigation in copy mode
11setw -g mode-keys vi
12
13# Setup 'v' to begin selection, just like Vim
14bind-key -t vi-copy v begin-selection
15
16# Setup 'y' to yank (copy), just like Vim
17bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
18
19# Update default binding of `Enter` to also use copy-pipe
20unbind -t vi-copy Enter
21bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
22
23# Bind ']' to use pbbaste
24bind ] run "reattach-to-user-namespace pbpaste | tmux load-buffer - && tmux paste-buffer"

This is how copy pasting will working after you’ve made the proper changes to your .tmux.conf. Note that I have remapped ctrl + b to ctrl + a, because that works pretty fast when you’ve already remapped Caps-Lock to Control.

  • ctrl + a, [ to enter copy mode. This allows you to use normal Vim motion keys to move around.
  • v to start selection, again use vim motion keys to move around.
  • y to yank the selection. enter also works.

Pasting works just as before:

  • ctrl + a, ]

Congratulation, you can now copy and paste with Tmux.

Configure Vim

The changes needed for Vim are minimal. All you need to do is unset the name of the default clipboard, so it will pass through to Tmux. In ~/.vimrc:

1" Clipboard 
2set clipboard=unnamed

Bonus features

So, copy and paste is now working from Vim, Tmux and Vim-in-Tmux. Pasting is also working as expected.

As a bonus, when you have two tmux panes or windows with different instances of Vim running, you can now easily copy and paste between them by using normal Vim commands!

Conclusion

Copy and paste with Vim and Tmux does not work out of the box, but once setup works like a charm.

Here are links to my ~/.vimrc and ~/.tmux.conf for your perusal.

I highly recommend tmux 2: Productive Mouse-Free Development, but feel free to check all my book recommendations.