question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set-Clipboard -Remote with OSC52

See original GitHub issue

Summary of the new feature / enhancement

Set-Clipboard does not really work over SSH, as it always sets the clipboard of the target machine, not the host. This means that if you SSH e.g. from your PC to a server, then Set-Clipboard will set the server’s clipboard, not your PCs, so you cannot paste it anywhere.

This can be resolved by using ANSI escape sequence (same mechanism as for setting colors) OSC52, which sets the clipboard of the host machine, which in our example is the PC, so you can paste it anywhere. This sequence is supported by most terminals, including Microsoft Terminal.

Example:

function Set-RemoteClipboard($text) {
    Write-Host "`e]52;;$([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($text)))`a"
}
# Copy "original" into a clipboard from anywhere
> Set-Clipboard 'changed'
# Press Ctrl+Shift+V to see clipboard still "original"
> Set-RemoteClipboard 'changed'
# Press Ctrl+Shift+V to see clipboard now "changed"

OSC also has similar Get-Clipboard -Remote sequence, but no terminal implements it as it is rightly considered a security and privacy risk, and you ca simply “get” the host clipboard with Ctrl+Shift+V.

Opens:

  1. Should we autodetect when to use the OSC52 instead of xclip or other OS target-implementation? E.g. SSH can be detected by presence of environment variables $env:SSH_CLIENT and $env:SSH_TTY. I would say no, as it could be potentially breaking behavior in case of false positives on terminals which don’t support the OSC. If user wants such behavior, they can add $PSDefaultParameters['Set-Clipboard:Remote'] = $env:SSH_CLIENT -or $env:SSH_TTY to their $PROFILE.
  2. Some terminal multiplexers like screen and tmux may require wrapping OSC52 into a different escape, as can be seen in the vim plugin implementation. Should we detect and handle those cases? tmux can be forced to work with default escape by setting set -s set-clipboard on. I would say keep it simple for now, then maybe add -RemoteMethod = auto | osc52 | tmux | screen in separate issue if required.

Proposed technical implementation details (optional)

See example for the escape sequence format.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jhoneillcommented, Sep 17, 2022

I was just about to say something similar to @237dmitry

This is a good idea, but there doesn’t appear to be any way for the remote session to know what OSC commands are supported (see also https://github.com/PowerShell/PowerShell/issues/18098#issuecomment-1249224249 ) . It is a useful thing to add but may cause wrong expectations if it is the default

@237dmitry the two formats

dGVzdAo= <— Contains “test” with a trailing Line feed
dGVzdA== <— Contains “test” only.

I’m guessing the latter is preferred but the former is mostly harmless.

0reactions
msftbot[bot]commented, Jun 29, 2023

🎉This issue was addressed in #18222, which has now been successfully released as v7.4.0-preview.4.🎉

Handy links:

Read more comments on GitHub >

github_iconTop Results From Across the Web

nvim-osc52: copy text from remote SSH sessions with OSC52
Hello, I've released a small plugin: nvim-osc52 to copy text to the system ... Tmux is configured with set-option -s set-clipboard on ....
Read more >
Copy from Remote Server to Local Clipboard via OSC 52 ...
Recently, I have found out the OSC 52 terminal sequence, which is really useful when we want to copy text from remote to...
Read more >
Clipboard integration using OSC 52 - Recipes & Guides
I recently learned about and set up copying to system clipboard using OSC 52 escape sequence, after seeing that tmux has a set-clipboard...
Read more >
Feature Request: Support OSC 52 (copy to clipboard)
OSC 52 is a escape sequence that allows terminal applications to ... such sequences when copying text, if the set-clipboard option is on....
Read more >
tmux in practice: integration with the system clipboard
That's because set-clipboard does not work when you have a remote tmux session nested in a local one. And it only works in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found