Ben Woodall

Last of the freelance hackers - Greatest sword fighter in the world

Copying and Pasting Between OS X and Vim


Copying and pasting between the clipboard and vim has always been a nightmare for me. You find a big chunk of code you want to copy over to your project, tab over to vim, get into insert mode, and hit Command + v. What do you get? A big wall of text that has every line tabbed over. Definitely not the format you were trying to copy over.

window.setTimeout((function() {
    $(".alert").slideUp(500, function() {
                }), 4000);

The easiest fix for this is to set the paste mode before you head into insert the code:

:set paste

You’ll notice the statusline shows you in PASTE mode and the tooltip shows -- INSERT (paste) --. Paste in your code and things should be good. When you’re done, revert back with:

:set nopaste

Another option is to set your pastetoggle key.

:set pastetoggle=<F2>

The above code will set the F2 key to toggle between paste and nopaste.

What I REALLY want is a way to yy and pp from my system clipboard and have it work in vim. One large problem with this is that the methods that OS X uses to go to and from the clipboard (pbcopy and pbpaste) break when you work in tmux sessions.

To fix this, we need to install reattach-to-user-namespace from ChrisJohnsen/tmux-MacOSX-pasteboard. We can install this threw brew:

brew install reattach-to-user-namespace

and in our .vimrc, add the following line:

set clipboard=unnamed

Now, anything you yank from vim is available in the clipboard, and anything in the clipboard is available to paste in vim!