Search code examples
debianclipboardwindows-subsystem-for-linux

Copy the contents of a file to clipboard from WSL to Windows?


Rig description:

I've installed Debian in Windows Subsystem Linux (WSL) on Windows 10.

  • The host means Windows 10.
  • The box means my WSL-Debian box/container.

Goal:

  • I have an SSH key on WSL (my_rsa.pub);
  • I want to copy the contents of that file to the host's clipboard;
  • by running a command in the box (command-line code, not using the mouse).

Explanation:

SSH files are very long, and it doesn't make sense to copy them with the mouse cursor.

I'm also told that Debian doesn't ship with a clipboard: you need to install a clipboard? So, I don't even know where to begin to look.

What I've tried:

"Copy to clipboard" in VIM is configured and working properly, but any contents copied to the 'clipboard' are removed from the 'clipboard' once VIM is closed; similarly, Nano has a CTRL+u function to cut text, and its clipboard does not survive outside of Nano.


Solution

    • Command:
    cat /path/to/file | clip.exe
    
    • Description:

    cat command put the file contents to the output. It is then pipe to the clip.exe, a Win32 program, redirects output to the Windows clipboard. Do not forget to add the .EXE extension of the later one. There are multiple cat alternatives can be used, see this and this.