How can I copy and paste text out of a remote vim to a local vim?

I'm editing a file in vim on a remote machine via ssh and I want to copy text out of the remote file and paste it into a vim instance running on my local machine. Is there an easy way to do this? I can use my terminal emulator's selection features to select text that is currently on the screen, but if my selection is larger than can fit on the screen, I'm out of luck.


Open the remote file in the local vim session:

  :e scp://user@host/relative/path/from/home.txt

If you have sufficient permissions on the remote machine, you can add

AcceptEnv WINDOWID

to /etc/ssh/sshd_config and restart sshd with

sudo /etc/init.d/ssh restart

Then on your local machine add

ForwardX11 yes
SendEnv WINDOWID

to your ~/.ssh/config file

Then by running an X-aware vim on the remote machine (e.g., gvim -v), you can copy and paste to and from the X clipboard. Just visually select the text (e.g., using V) and yank it into the * register.

Edit

I forgot to include the part about setting SendEnv WINDOWID in your ~/.ssh/config, so I added that and moved the enabling of X forwarding from the command line (ssh -X option) to the ~/.ssh/config file (ForwardX11 yes).