Ben Woodall

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

Copying and Pasting Between OS X and Vim

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.

1
2
3
4
5
window.setTimeout((function() {
    $(".alert").slideUp(500, function() {
        $(this).remove();
            });
                }), 4000);

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

1
: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:

1
:set nopaste

Another option is to set your pastetoggle key.

1
: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:

1
brew install reattach-to-user-namespace

and in our .vimrc, add the following line:

1
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!

Comments