skip to Main Content

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


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

    Login or Signup to reply.
  1. (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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search