35

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.

2 Answers 2

53
  • 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.

3
  • Basically what I had expected after discovering that clip was a Windows package. I didn't know how to pipe to Powershell until now, though! Thank you! Commented Jan 31, 2020 at 0:39
  • 1
    @Wolfpack'08 Watch any WSL video from Microsoft, this feature is their first marketing feature.
    – Biswapriyo
    Commented Jan 31, 2020 at 6:51
  • It's not working. Is there some way to debug?➜ ~ echo aaaaaaaa | clip.exe ➜ ~ echo $? 1
    – Jiang YD
    Commented Oct 19, 2022 at 3:00
4

(Alternatives to @Biswapriyo's correct answer)

Using win32yank.exe

Having the .exe in $PATH:

cat /path/to/file | win32yank.exe -i

Using Neovim

cat /path/to/file | nvim -c 'normal ggVG"+yZQ' --headless -

Explanation

Neovim uses an external program to interact with the system clipboard, and this specific program varies among platforms. On Windows it uses win32yank.exe. The -c option allows to execute a command after the file is loaded, --headless makes Neovim run without a UI, and the trailing - indicates to read from standard input. So if you have Neovim already configured in WSL to work with the clipboard, this should also work (doing about the same as just directly calling win32yank.exe).

Configuring Neovim's clipboard in WSL:

https://github.com/neovim/neovim/wiki/FAQ#how-to-use-the-windows-clipboard-from-wsl

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