Passing data through vims using the X clipboard

So you have this nicely formatted piece of code in one vim and you'd like to paste it into another vim, but that usually messes up things.

What I used to do was this:

  1. Select the code using V
  2. :w!/tmp/foobar
  3. Switch to the other vim
  4. :r/tmp/foobar

Then with vim7 I could just load files in different tabs and do everything inside vim. But sometimes I have more than one vim running anyway. Today I figured out a better way:

  1. Select the code using any of v, V, ^V
  2. Copy into the X clipboard: "*y
  3. Switch to the other vim
  4. "*p

It SO works. It'll even allow you to copy and paste block selections accross different vim instances.

Of course this can also be used to copy and paste pieces of text to/from any other program that can access the X clipbpard, like X applications or the nice xclip commandline tool:

  1. (any command) | xclip
  2. Switch to vim
  3. "*p

Of course that is just an example: a better way to do it directly in vim is:

  1. :r!(any command)

But I'm happy, for two reasons:

  1. I can copy an paste arbitrary vim things between aribtrary vims using the X selection
  2. Within 10 minutes I'll receive 5 e-mails telling me of even better ways of doing this.

Update: some kind people suggested to use the :set paste and :set nopaste trick and then do normal copy&pasting using the X terminal.

That is what most people do, and what I used to do sometimes, but: