Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy and cut commands don't work on pwsh on WSL2 Ubuntu #1993

Open
fsackur opened this issue Nov 28, 2020 · 3 comments
Open

Copy and cut commands don't work on pwsh on WSL2 Ubuntu #1993

fsackur opened this issue Nov 28, 2020 · 3 comments
Assignees

Comments

@fsackur
Copy link

@fsackur fsackur commented Nov 28, 2020

Environment

PS version: 7.1.0
PSReadline version: 2.1.0
os: Linux L7P30N13 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
PS file version: 7.1.0.0
HostName: ConsoleHost
BufferWidth: 138
BufferHeight: 70

I am using Windows Terminal.

Steps to reproduce

  • Install WSL 2
  • Install Ubuntu
  • Install pwsh in WSL
  • Set up these handlers for windows-style Ctrl-C / Ctrl-X:
    Set-PSReadlineKeyHandler -Chord Ctrl+c -Function CopyOrCancelLine
    Set-PSReadlineKeyHandler -Chord Ctrl+x -Function Cut

Expected behavior

Select text. Ctrl-C or Ctrl-X should place that text on the system clipboard. It should be possible to paste that text in the same WSL terminal or anywhere in the Windows system.

Actual behavior

Text is not placed on the clipboard. Paste, even in the same console, is still pasting the previous clipboard contents.

@msftbot msftbot bot added the Needs-Triage 🔍 label Nov 28, 2020
@fsackur
Copy link
Author

@fsackur fsackur commented Nov 28, 2020

I believe this is caused here: https://github.com/PowerShell/PSReadLine/blob/v2.1.0/PSReadLine/Clipboard.cs#L72-L82

On my WSL Ubuntu, the following returns true:
[System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Linux)

I have no xclip binary that I can find with which xclip.

clip.exe does work.

I wonder if this is sufficient to disambiguate?
[System.Runtime.InteropServices.RuntimeInformation]::OSDescription
Linux 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020

@fsackur
Copy link
Author

@fsackur fsackur commented Nov 28, 2020

I offer this as a workaround:

Set-PSReadLineKeyHandler -Chord Ctrl+c -ScriptBlock {
    [int]$SelStart = 0
    [int]$SelLength = 0
    [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$SelStart, [ref]$SelLength)

    if ($SelStart -eq -1 -and $SelLength -eq -1)
    {
        return [Microsoft.PowerShell.PSConsoleReadLine]::CancelLine()
    }

    [string]$Buffer = ''
    [int]$Cursor = 0
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$Buffer, [ref]$Cursor)

    $Buffer.SubString($SelStart, $SelLength) | clip.exe
}

Set-PSReadLineKeyHandler -Chord Ctrl+x -ScriptBlock {
    [int]$SelStart = 0
    [int]$SelLength = 0
    [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$SelStart, [ref]$SelLength)

    if ($SelStart -eq -1 -and $SelLength -eq -1)
    {
        return
    }

    [string]$Buffer = ''
    [int]$Cursor = 0
    [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$Buffer, [ref]$Cursor)

    $Buffer.SubString($SelStart, $SelLength) | clip.exe

    [Microsoft.PowerShell.PSConsoleReadLine]::Cut()
}

I don't have a way to avoid the trailing newline chars.

@fsackur
Copy link
Author

@fsackur fsackur commented Nov 30, 2020

This may be a better workaround.

Create /usr/local/bin/xclip with the following contents (and grant sudo chmod a+x /usr/local/bin/xclip):

#!/bin/sh

# https://github.com/PowerShell/PSReadLine/issues/1993
# this is not an xclip replacement! It does just enough to let PSReadline work on WSL.

if [ "$3" = "-out" ]; then
    # paste
    if command -v pwsh.exe >/dev/null 2>&1; then
        pwsh="pwsh.exe"
    elif command -v powershell.exe >/dev/null 2>&1; then
        pwsh="powershell.exe"
    else
        exit 1
    fi
    pwsh="$pwsh -NoProfile -NoLogo -NonInteractive"

    $pwsh -command Get-Clipboard | sed 's/\r$//' | head -c -1
    exit 0

else
    # cut or copy
    tee <&0 | clip.exe
    exit 0
fi

For reasons I don't understand, it doesn't suffer from the annoying trailing newline that clip.exe normally appends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.