Copy Paste Hell Between Windows Host and Linux Guest in VirtualBox

Filed under Linux, VirtualBox
Tagged as , , ,

The X server maintains three different selection buffers, PRIMARY, SECONDARY and CLIPBOARD. The PRIMARY buffer is usually used for copying and pasting via the middle mouse button. And the CLIPBOARD buffer is used when the user selects some data and explicitly requests it to be “copied” to the clipboard, such as by invoking “Copy”.

The VirtualBox client tool synchronizes the Windows clipboard content to the PRIMARY and CLIPBOARD buffer of a Linux host. But it only synchronizes the CLIPBOARD buffer back out to the Windows host. The reason why it’s not using the PRIMARY selection is that by only selecting a text in Linux overwrites the Windows clipboard immediately, which is an unexpected behavior for Windows users. At least that’s their excuse.

It might not be the expected behavior for Windows users. But it’s definitely not the expected behavior for Linux users. The forums are filled with posts like “Copy/Paste from Linux guest to Windows doesn’t work”. It is especially annoying that the selected text from a terminal window can not be easily pasted out to the Windows host. There are a couple solutions. But none are really satisfying.

Automatically synchronize PRIMARY and CLIPBOARD

There are tools which let you synchronize the different buffers. For example autocutsel can do this by running two instances of it to work in both ways:

# keep clipboard in sync with primary buffer
autocutsel -selection CLIPBOARD -fork

# keep primary in sync with clipboard buffer
autocutsel -selection PRIMARY -fork

This causes the contents of the PRIMARY buffer to be automatically copied into the CLIPBOARD buffer and vice versa. So the selected text is immediately pastable on the host Windows. The -fork argument lets autocutsel run in the background. All that’s left to do is to insert those lines in the startup/init script of your choice to make it permanently available.

However there is a big disadvantage. The buffers are not meant to be mixed together. In many applications you can select some text and paste over a previously copied text by hitting CTRL-V or SHIFT-Ins. This stops to work, because as soon as you select a text it is synchronized from the PRIMARY to the CLIPBOARD buffer and pasting would just paste the selected text over the selected text again.

Manually synchronize PRIMARY and CLIPBOARD

To synchronize the buffers all the time does not seem to be best solution. But we can copy the PRIMARY buffer to CLIPBOARD on demand. For example:

xsel | xsel -b

xsel is a very useful tool which lets you pipe content in and out different buffers. The above command pipes to content of PRIMARY into stdout which is then piped back into CLIPBOARD. The argument -b tells xsel to use CLIPBOARD. Without an argument it uses PRIMARY. xsel is a very cool tool. Check out it’s man page for different uses.

We could probably package this command into a script and bind it to a system wide keyboard shortcut. That way we would only have to hit this shortcut after selecting some text to be able to paste it on the windows side.

Use CTRL-SHIFT-C / CTRL-Ins / CTRL-C

Different applications offer different shortcuts to copy to and from the CLIPBOARD buffer. For example, the Gnome Terminal uses CTRL-SHIFT-C/CTRL-SHIFT-V. Other applications support the Windows style shortcuts CTRL-C/CTRL-V or CTRL-Ins/SHIFT-Ins (e.g. Firefox). However it’s a hit and miss because there doesn’t seem to be a standard way to copy/paste in X11. So while it’s possible in most programs it’s difficult to remember which program uses which shortcut. Most of the time I keep hitting different shortcuts until it works. It’s a mess.

Conclusion

To make a long story short. I have not found a really satisfiable solution. Either of the above solutions work, but they are either awkward or have unwanted side effects. So if you find a better solution or any other solution at all, please let me know by leaving a comment.

5 Comments

  1. Tom says:

    Seems to me that VirtualBox should be taking care of this better, much like the VNC clients do.

  2. Gerard H. Pille says:

    The only way I managed to copy from Linux Guest to Windows Host, while using fluxbox, was to add the following line to my .fluxbox/keys

    Control Insert :Exec xsel | xsel -i -b

    Thanks for the xsel hint, but it took me quite a while to realize the -i option was needed.

  3. Lex says:

    This annoyed the heck out of me too, so I hacked together a method of synchronizing the primary selection between host and VM. See github:lexelby/vbox-sync-selection.

  4. Lex says:

    Watch out for the current version -- if you highlight twice in rapid succession, the scripts start fighting in a loop for the clipboard. I'll push a new version tomorrow that avoids looping.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*