I help teams use home ambitiously!

Working with the MacOS system clipboard in vim inside Tmux

Let's be honest. Working with vim inside Tmux is the way everyone should work when coding on a unix platform ;)

No seriously if you happen to use this combination (vim + tmux) on MacOS you will know that working with the clipboard does not work as expected. Yanking code in vim won't be safed to the system clipboard and visual selection mode with tmux simply ships in a broken state out of the box on MacOS.

Fixing Tmux

Fixing Tmux is pretty easy (assuming you are using Homebrew):

brew install reattach-to-user-namespace  

You then have add some configuration to your tmux.conf to tell Tmux to use the system clipboard. Just follow the instructions in this blogpost on the Thoughtbot blog. I won't repeat the steps because I did not come up with it but rest assured after that Tmux will happily paste to the clipboard. Yay!

Fixing Vim

When running Vim inside of Tmux and expecting that yanking will copy to the clipboard you are out of luck too with the default settings and the default installed vim version on MacOS. But as with Tmux this is fixable when using homebrew.

First you have to install MacVim:

brew install macvim --override-system-vim  

The override-system-vim-option tells homebrew that you want to replace the default vim installation on your system with macvim. Running vim inside your terminal will then run MacVim instead of normal cmd-line vim.

Make sure you have /usr/local/bin in your $PATH before /usr/bin otherwise the override-system-vim-option will have no effect. To make sure your $PATH-Variable is set correctly you can run brew doctor which will complain if /usr/local/bin is not in first place.

After that you still have to add some lines to your .vimrc:

" make pasting from clipboard work (no indents etc.)  
" toggle paste and then toggle back  
set pastetoggle=<F10>  
" yanking will save to clipboard (macvim only)  
set clipboard=unnamed

This is taken directly from my .vimrc-file and the comments explain what the commands are doing, especially the pastetoggle-command will save you some time when working with vim and MaxOS.

Now when pasting into vim you have to toggle paste (with F10) first and after having pasted you turn it off again. Only setting set paste is not enough because you will loose automatic indentation when doing so.

This is still kind of annoying but you will not paste into vim that often anyway.

Vim inside of Tmux

After following the steps above Tmux and Vim will behave in a sane way when run independent of each other. This is not what we want to do and I got an error when yanking and pasting inside vim within tmux:

E353: Nothing in register *  

As it turns out you have to to tell tmux to reattach-to-user-namespace when starting up to get this to work. Simply add the following line to your .tmux.conf:

set -g default-command 'reattach-to-user-namespace $SHELL --login'  

After that everything should be fine.

Hope this was helpful to some of you! Happy coding!

comments powered by Disqus