How can I share my clipboard between two X servers?
I've recently set up my Ubuntu machine so that I run another X session in pty8. I mostly run virtual machines or remote desktop sessions on this other X server, which helps mediate some of the frustrations that can happen with keyboard integration in these environments.
However, now if I copy something from some window on :0, I can't paste it into some window on :1.
Is there a way I can share the clipboard between these two sessions?
I came up with a solution that seems to work pretty well. I'm not sure if there's a better way, but I wrote a script that starts my VM and then monitors the clipboard on display :0 for changes. When a change is detected, it copies the clipboard contents to display :1. It does this bidirectionally, so I can copy and paste from the VM just fine too.
Here's the script:
#!/bin/bash
virtualbox --startvm "Windows 7" --fullscreen &
waitpid=$!
watch_clip() {
local curr="" prev="" from=$1 to=:0
# On first run, we need to copy from :0 to :1 but not vice versa
if [[ "$from" == ":0" ]]; then
xclip -o -selection clipboard -d :0 2> /dev/null | xclip -selection clipboard -d :1
to=:1
fi
while true; do
# Get the current clipboard contents
curr=`xclip -o -selection clipboard -d $from 2> /dev/null`
# Compare to previous results and copy if it's changed
if [[ "$curr" != "$prev" ]]; then
echo "$curr" | xclip -selection clipboard -d $to
fi
prev="$curr"
sleep 0.5
done
}
watch_clip :0 &
watch_clip :1 &
wait $waitpid
Then all that's needed is the command to start the second X session:
startx ./.startwin7 -- :1
I haven't noticed any issues, but if anyone can think of a better way I'd definitely appreciate the input.
If you want something turnkey, Synergy should do the trick. I've never used it on just one box, across X sessions. It works to share clipboards between separate machines, so it would be surprising if a design flaw or usability issue kept it from working for you.
Also: for your vms, they have builds for Mac and Windows.
I had the same problem. Synergy didn't work well for me (it has major bugs in the X11 part of its clipboard syncing), and I didn't want to have a script that just polls the clipboard with a time delay, as that wastes CPU and/or adds random delays before I can paste. I wrote a script called xclipsync that solves the problem for me.
https://github.com/apenwarr/xclipsync