When you press Ctrl-C over a file in the file manager, the file's contents IS NOT copied to the clipboard. A simple test: select a file in file manager, press Ctrl-C, open a text editor, press Ctrl-V. The result is not file's contents but its full path.
In reality the situation is a bit more complicated because you can't do the opposite - copy a list of filenames from a text editor and paste them into file manager.
To copy some data from command line to X11 clipboard you can use xclip
command, which can be installed with
sudo apt-get install xclip
to copy contents of a file or output of some command to clipboard use
cat ./myfile.txt|xclip -i
the text can be then pasted somewhere using middle mouse button (this is called "primary selection buffer").
If you want to copy data to the "clipboard" selection, so it can be pasted into an application with Ctrl-V, you can do
cat ./myfile.txt|xclip -i -selection clipboard
To be able to copy files from the command line and paste them in a file manager, you need to specify a correct "target atom" so the file manager recognizes the data in the clipboard, and also provide the data in correct format - luckily, in case of copying files in a file manager it's just a list of absolute filenames, each on a new line, something which is easy to generate using find
command:
find ${PWD} -name "*.pdf"| xclip -i -selection clipboard -t text/uri-list
(at least this works for me in KDE). Now you can wrap into a small script which you can call, say, cb
:
#!/bin/sh
xclip -i -selection clipboard -t text/uri-list
then you put it in ~/bin
, set executable bit on it and use it like this:
find ${PWD} -name "*.txt"| cb
Nice, isn't it?
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.
Best Answer
Persistence is probably the best selling point for me. Most CBMs allow you to store the same items across sessions (even platforms), mobile (if your profile is) and especially in the case of X-based applications, selected items* are remembered after the source application closes (something that is not natively true).
Multiple clipboards is something I don't use too much but it' very handy if you find yourself juggling several bits and bobs at once. You can do all your copying at once and then get on with whatever you're doing. Less back-and-forth.
These are both negatives if you regularly copy sensitive data as it could be a security issue.
* Just an explanation: you do know you effectively get two clipboards out the box? There's the traditional Control+C, Control+V clipboard but there's also something called "primary selection". Select some text and then middle click where you want it pasted. I find this very useful for quick, precise, multiple pasting.