Yes, this is a good question. Ideally, one would want to be able to temporarily turn clipboard sharing on and off. There is a virt-manager issue (https://github.com/virt-manager/virt-manager/issues/358) requesting this capability. It was deemed "way too niche", and there was even a pull request (https://github.com/virt-manager/virt-manager/pull/166) which was not accepted.
My normal solution is to share text back-and-forth in a shared terminal (using tmux). This works, but I don't really like it.
You don't go into a lot of detail with your shared file approach, but you could certainly automate things using xclip, for example.
To copy the clipboard to the shared file, use something like
xclip -o -selection clipboard > share_file_name
And to copy from the shared file to the clipboard, use something like
xclip -i -selection clipboard share_file_name
You can make scripts for these and put them on both the host and the VM. You could assign shortcut keys to them, so that it is relatively easy to copy from one clipboard to the file and then from the file to the other clipboard. Maybe this is what you've already done. If not, maybe this is an improvement.
One drawback of the above approach is that you have to have a shared file that can be accessed by both the host and the VM. If you don't trust the VM, then maybe you don't want any shared files either.
Without shared files, there still are some options. A program like netcat or some other texting application could be used to send text back and forth. For the use case of pasting the text from the host's clipboard into an application on the vm, I use an auto-type script using xclip and xdotool. For example, my xautotype_clipboard script contains:
#!/bin/bash
delay=${1:-5}
txt=$(/bin/xclip -o -selection clipboard)
/bin/xdotool sleep "${delay}" type "${txt}"
I copy text into the host's clipboard and then run the above script (using a window manger shortcut). I move the mouse to the VM to where I want to paste the text. The script puts the text as if I had typed it. The script takes an optional argument, the delay in seconds. It defaults to 5, and is the delay before the auto-typing starts. This gives time to position the mouse in the correct window/application.
This works for short, one-line items. It doesn't always do what I want, especially for clipboards containing multiple lines of text.
For multiple lines of text, the script can be modified to type a line at a time. My xautotype_clipboard2 file contains:
#!/bin/bash
delay=${1:-5}
IFS=
do_autotype()
{ local first=1
local string
while read string
do
if [[ "${first}" == 1 ]]
then
/bin/xdotool sleep "${delay}" type "${string}"
first=0
else
/bin/xdotool type "${string}"
fi
/bin/xdotool key Return
done
}
echo $(/bin/xclip -o -selection clipboard) | do_autotype
This works for host clipboards containing multiple lines of text.
Unfortunately, I don't have a similar solution to go from the VM back to the host.
Note with xclip you can specify which selection you want to use. In the above, I use -selection clipboard, but you could also use -selection primary or -selection secondary, depending on which X selection you want to use.
Edit-1: Also, please be sure to test to be sure it is doing what you want. xautotype_clipboard will remove linefeed characters. xautotype_clipboard2 will add a linefeed character at the end, even if there wasn't one.