How can I copy the contents of a file directly into my Windows clipboard when I'm running the command on a remote Linux machine via ssh in Git Bash?
Asked Answered
N

1

1

I'm using Windows 10. I open Git Bash and then ssh into an Ubuntu server. Often I want to copy the whole contents of a large text file.

Rather than using scp to download the file to my Windows machine, I sometimes would rather quickly copy the contents to my clipboard.

Using cat and then scrolling thousands of lines and then manually copying to clipboard is possible but isn't practical.

I'd rather pipe cat to a command that copies the output to my Windows clipboard. Or call some other command like xclip.

https://unix.stackexchange.com/questions/211817/copy-the-contents-of-a-file-into-the-clipboard-without-displaying-its-contents and How can I copy the output of a command directly into my clipboard? are similar questions, but xclip causes this error:

xclip -sel c < /etc/php/7.4/cli/php.ini
Error: Can't open display: (null)

Update after comment:

https://mcmap.net/q/12901/-error-can-39-t-open-display-null-when-using-xclip-to-copy-ssh-public-key-closed was interesting, but X11Forwarding yes is already in my server config, and when I prepended ForwardX11 yes to ~/.ssh/config and then ran ssh -v -X -t -i ~/.ssh/id_rsa myuser@■■.■■■.■■■.■■, I still got:

debug1: No xauth program.
Warning: untrusted X11 forwarding setup failed: xauth key data not generated

and then when I ran xsel -b < /etc/php/7.4/cli/php.ini:

xsel: Can't open display: (null)
: Inappropriate ioctl for device

Maybe X session in Git Bash on Windows? will help me further.

Neelyneeoma answered 7/2, 2020 at 16:18 Comment(1)
Does this help Error: Can't open display: (null) when using Xclip to copy ssh public keyAutoroute
S
2

You need and X server on your Windows host and X-tunnelling in your ssh connection. xclip will send the clipboard to your X server, and the server will provide it to Windows.

  1. Install an X server to your Windows machine. I use VcXsrv, there are XMing and others. The flavor of X is not important.
  2. Launch the server
  3. in Git Bash use command export DISPLAY=localhost:0.0
  4. Make sure that /etc/ssh/sshd.config on the remote node has line X11Forwarding yes
  5. enable X11 tunneling in ssh command: add -Y flag to ssh: ssh -Y <server_address>

While there are some recipies on Stack Overflow already, there is one glitch. Note DISPLAY=localhost:0.0. If you omit localhost, that is export DISPLAY=:0.0, then xclip will fail on the remote node :

connect /tmp/.X11-unix/X0: No such file or directory xterm: Xt error: Can't open display: localhost:10.0

Steffi answered 7/2, 2020 at 18:22 Comment(2)
Thanks for these steps. I haven't tried them yet, but they sound like they're probably correct, so I've accepted the answer.Neelyneeoma
They do work. I tested on my windows 10 - Ubuntu 18.04 pair. Copied 1 Mb log file over the clipboard.Steffi

© 2022 - 2024 — McMap. All rights reserved.