17

Using the Linux Subsystem for Windows (LSW), clip.exe can be used to copy data to the windows clipboard:

$ clip.exe /?

CLIP

Description:
    Redirects output of command line tools to the Windows clipboard.
    This text output can then be pasted into other programs.

Parameter List:
    /?                  Displays this help message.

Examples:
    DIR | CLIP          Places a copy of the current directory
                        listing into the Windows clipboard.

    CLIP < README.TXT   Places a copy of the text from readme.txt
                        on to the Windows clipboard.

Is there any way to pipe FROM the clipboard? Examples of intended use:

$ paste.exe > foo.txt

$ paste.exe | tr , '\n' | clip.exe
2

1 Answer 1

27

Solution

This prints the Windows' clipboard to stdout:

powershell.exe Get-Clipboard

Examples

$ echo "hello" | clip.exe
$ powershell.exe Get-Clipboard
hello

$ date +%Y-%m-%d | clip.exe
$ powershell.exe Get-Clipboard
2019-06-13

$ alias paste.exe='powershell.exe Get-Clipboard'
$ echo "world" | clip.exe
$ paste.exe
world

paste() {  # Add me to your .bashrc, perhaps.
    powershell.exe Get-Clipboard | sed 's/\r$//'
    # The `sed` will remove the unwelcome carriage-returns
}

source: https://github.com/Microsoft/WSL/issues/1069#issuecomment-391968532

3
  • 4
    Nice work @NonlinearFruit! I see that with my version of WSL/Windows, Get-Clipboard appends \r\n to the end of the content. I used the following as my alias to trim these unwanted additions: alias paste.exe="powershell.exe Get-Clipboard | perl -p -e 's/\r\n$//'" Commented Jun 13, 2019 at 20:15
  • seems to also add trailing line, add ;$d to sed to fix
    – CervEd
    Commented Feb 10, 2023 at 12:56
  • 1
    I would strongly suggest a different name for such a function. paste is an existing POSIX tool
    – CervEd
    Commented Jun 2, 2023 at 7:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.