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:
- Select the code using V
:w!/tmp/foobar
- Switch to the other vim
: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:
- Select the code using any of v, V, ^V
- Copy into the X clipboard:
"*y
- Switch to the other vim
"*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:
- (any command) | xclip
- Switch to vim
"*p
Of course that is just an example: a better way to do it directly in vim is:
:r!(any command)
But I'm happy, for two reasons:
- I can copy an paste arbitrary vim things between aribtrary vims using the X selection
- 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:
- it takes too much typing;
- it makes it awkward to select big quantities of text that don't fit in one screen;
- it can't copy and paste arbitrary vim objects (try pasting a ^V block selection with :paste);
- it doesn't always exactly paste what was copied (you copy tabs and you're likely to paste spaces).