Accessing the OS X clipbord in VIM

I’m a vi kind of guy and uses various flavors of it a lot. One thing that took me all too long before I got around to set up was proper access to the OS X clipboard from vim.

There are all kinds of howtos on this out there, so I will just cut to the chase but one thing to point out is that depending on compile options you might have good access to the clipboard already. One way to check is by running :echo has('clipboard') in vim. If it returns 0, you don’t have it compiled with the clipboard. This is the case for the all the vim’s I have checked that ships with OS X. If yours is different please leave a comment with some details about OS X and vim versions.

In your .vimrc add the following lines
" paste on new line (after current) from OS X clipboard
map pbp o<Esc>:.!pbpaste<CR><CR>

" copy visual selection to OS X clipboard
vmap pbc :w !pbcopy<CR>

It’s probably obvious to anyone who has used vi what these lines do, but they map pbp to run the external commands pbpaste (paste from OS X clipboard) and the visual selection alias pbc to run pbcopy.

Pbpaste and pbcopy are available by default as a part of OS X.


Leave a Reply

Your email address will not be published. Required fields are marked *