Skip to content

%paste system clipboard from wls2 #13511

@duke13137

Description

@duke13137

I use ipython from wsl2 in windows terminal and vscode remote (both has keymap sending %paste without switch editor panel). One problem is that %paste gives

In [50]: %paste
ERROR:root:Getting text from the clipboard on this platform requires tkinter.

Since wsl2 is headless GUI, I don't think tkinter would work either. I found a workaround that seems working fine (a bit hacky). Post it here in case anyone is interested. Ideally, ipython could detect wsl2 and include this function.

First, find out ipython package path by "pip show ipython", then "find -name clipboard.py"

Replace tkinter_clipboard_get with below code.

def tkinter_clipboard_get():
    """ Get the clipboard's text using Tkinter.

    This is the default on systems that are not Windows or OS X. It may
    interfere with other UI toolkits and should be replaced with an
    implementation that uses that toolkit.
    """
    p = subprocess.Popen(['powershell.exe', '-command', 'Get-Clipboard'],
        stdout=subprocess.PIPE)
    bytes_, stderr = p.communicate()
    # Text comes in with old Mac \r line endings. Change them to \n.
    bytes_ = bytes_.replace(b'\r', b'')
    text = py3compat.decode(bytes_)
    return text

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions