Vim Tips Wiki
Advertisement


Tip: In line copy and paste to system clipboard

Created: July 23, 2007 Complexity: basic Author: Robert Iannucci Version: tested with 7.0


I know that this has been covered in other places, but I was dissatisfied with the ability to only copy and paste line-wise. I found it particularly annoying when I just wanted to yank a single word out to do a web search or similar. After a bit of poking around with VIM's builtin functions (on version 7 at least), I discovered the following workaround:

vmap <C-c> y:call system("pbcopy", getreg("\""))<CR>
vmap <C-v> :call setreg("\"",system("pbpaste"))<CR>p

For my non-OS X systems (ubuntu running vim in the gnome terminal, particularly), I use something like this:

vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>
vmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p

Now I know that "+ and "* are supposed to 'do the right thing', but there isn't any such integration with OS X, and ubuntu uses the 'clipboard' rather than the default register for xclip... it is quite annoying.

In addition to the integration issues, these have the advantage that they can copy any arbitrary visual block of text (including whole lines with the Visual Lines mode). I do realize that mapping <C-v> blows away the whole visual block mode, but I never use it. Feel free to map these to something else (maybe using ':vnoremap y') :-).

Also, to get more groovyness in Insert mode (lets you paste and keep on typing):

imap <C-v> <Esc><C-v>a

Comments

Advertisement