Copy-from-remote trick 📋

You probably know that printing escape sequences on a terminal can set the colours. I just found out that you can also use them to set your local clipboard contents with text from a remote machine.


By default, it's probably disabled on your local terminal.

Web page on how to switch it on in various terminals


printf "\033]52;c;$(printf "%s" "hello" | base64)\a"

Your local clipboard should now say "hello", even if you do this on a remote machine. The base64 is necessary - stops the data breaking the escape sequence. If you want to try echo instead of printf, go ahead.


I did this:

function clip () {
  printf "\033]52;c;$(clip.py "$*")\a"
}
export -f clip

...and had the python script do the "openAnything" stuff from Dive Into Python so I can copy a file or a literal string or something from a pipe. It does the base64 in there too. Adjust all of this to your taste.

Sadly, I can't get it to work from inside screen - I guess screen is filtering out the escape sequence?

Yes, seems so.

I came across this somewhere on Gemini, but I can't remember where, so I can't give the person a hat-tip. [sad face]

If you're familiar with colour escape sequences, notice that in this case the square bracket is the other way round. If you search for OSC escapes (it stands for Operating System Command), there are a few others, but I didn't find any that I wanted to use.

#clipboard

#OSC

#TechYawn

back to gemlog


Source