Skip to main content

Tmux and Vim: Copy and Paste on macOS Sierra

·509 words·3 mins· ·
Vim Tmux Terminal Macos
Ariejan de Vroom
Author
Ariejan de Vroom
Jack of all Trades, Professional Software Craftsman
Table of Contents

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.

brew install reattach-to-user-namespace
brew 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.

# Configure your default shell, Zsh in my case.
set -g default-shell $SHELL 

# Override the default command to use `reattach-to-user-namespace` for everything.
set -g default-command "reattach-to-user-namespace -l ${SHELL}"

# Remap prefix to ctrl-a (or caps-a for my mac)
set -g prefix C-a

# Vim style navigation in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection, just like Vim
bind-key -t vi-copy v begin-selection

# Setup 'y' to yank (copy), just like Vim
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

# Bind ']' to use pbbaste
bind ] 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:

" Clipboard 
set 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.

Related

Search and Replace in multiple files with Vim
·269 words·2 mins
Rails Vim
From 11.34s to 0.625s for opening a .rb file in Vim
·313 words·2 mins
Ruby Vim
Fixing a slow starting Terminal or iTerm2 on Mac OS X
·171 words·1 min
Bash Shell MacOSX Devops Terminal Terminal.app Iterm Iterm2 Zsh