History always repeats itself and sometimes that's good. Here's a second chance to be part of it: tmdtc.com

Friday 23 January 2009

Using the clipboard in vim

How many vim users know that you can copy from and paste to the clipboard?
It's even very easy: use the register +
To use a register with a copy or paste command, you specify it by typing " followed by the register name.
So, to copy to the x register and the clipboard:
"+y
and to paste from the x register:
"+p

I think this would be best illustrated by a screencast. Anyone interested in vim screencasts? Should I take the time to do some vim screencasts? Let me know.

3 comments:

Paul Cobbaut said...

The clipboard ? Isn't that a window thingie ?

Anyway, when I do a dd or yy, then those lines can be pasted with p in another vim (fired up after the first vim).

Screencasts are welcome.

raphinou said...

Hi Paul,

I can't speak for windows, i don't use it, but there's an X clipboard.

When you want to copy file from a vim terminal to, eg, a form in a web page, you use this clipboard (I think ;). Under X you can select the text with your mouse cursor, and paste it with a middle click. This method has drawbacks though, as some newlines might be added, or if display line numbers in vim, these are copied too. It's even worse if you use the vsplit command from vim.

The easiest way is to use this + register, then you can use CTRL-V in your browser to paste the text.

This doesn't work with a vim running on a remote server, as it doesn't have access to your local X clipboard.

As for your remark, when you exit vim, it writes in ~/.viminfo the state of your last sessions, including the states of the registers. Just take a look in that file and you should find the text you copied in the first vim session and pasted in the subsequently started vim (unless you have put something else in the register in the meantime of course ;-).

Paul Cobbaut said...

thanks for your informative reply!

paul