Copy / Yanking that always works with vim and neovim

shell script vimtmux

Are you on macOS and having trouble copying in vim when using tmux?

You may try to yank and paste, and be greeted with an error:

Terminal window
E353: Nothing in register *

Before we go ahead, let’s check our tmux version:

Terminal window
$ tmux -V

tmux 2.6 and upLink to section

If you can upgrade to 2.6 on macOS, you should be OK:

Do a dance on OS X 10.10 and above to return tmux to the user namespace, allowing access to the clipboard.

If you’re a macOS user using Homebrew and have Mac OS X 10.10 or greater, you can upgrade your tmux via homebrew: $ brew upgrade tmux.

tmux 2.5 and lower or Mac OS X < 10.10Link to section

In order to get the clipboard working with vim in tmux panes, you’ll need tmux-MacOSX-pasteboard. To grab it via Homebrew:

Terminal window
$ brew install reattach-to-user-namespace

Add this to your ~/.zshrc or ~/.bashrc:

Terminal window
if command -v reattach-to-user-namespace > /dev/null; then
alias vim="reattach-to-user-namespace vim"
alias nvim="reattach-to-user-namespace nvim"
fi

Another more automatic away, in your ~/.tmux.conf:

Terminal window
set-option -g default-command "reattach-to-user-namespace -l zsh"

It’s required that you start tmux in a fresh server and a new terminal. save all your work, and then exit tmux with $ tmux kill-server.