How to Send Data to Local Clipboard from a Remote Ssh Session

How to send data to local clipboard from a remote SSH session

I'm resurrecting this thread because I've been looking for the same kind of solution, and I've found one that works for me. It's a minor modification to a suggestion from OSX Daily.

In my case, I use Terminal on my local OSX machine to connect to a linux server via SSH. Like the OP, I wanted to be able to transfer small bits of text from terminal to my local clipboard, using only the keyboard.

The essence of the solution:

commandThatMakesOutput | ssh desktop pbcopy

When run in an ssh session to a remote computer, this command takes the output of commandThatMakesOutput (e.g. ls, pwd) and pipes the output to the clipboard of the local computer (the name or IP of "desktop"). In other words, it uses nested ssh: you're connected to the remote computer via one ssh session, you execute the command there, and the remote computer connects to your desktop via a different ssh session and puts the text to your clipboard.

It requires your desktop to be configured as an ssh server (which I leave to you and google). It's much easier if you've set up ssh keys to facilitate fast ssh usage, preferably using a per-session passphrase, or whatever your security needs require.

Other examples:

ls  | ssh desktopIpAddress pbcopy
pwd | ssh desktopIpAddress pbcopy

For convenience, I've created a bash file to shorten the text required after the pipe:

#!/bin/bash
ssh desktop pbcopy

In my case, i'm using a specially named key

I saved it with the file name cb (my mnemonic (ClipBoard). Put the script somewhere in your path, make it executable and voila:

ls | cb

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?

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

Ubuntu: How to copy text from terminal which is running vim on ssh, into local clipboard

What you're seeing is actually the result of :set mouse=a (or :set mouse=nvi) in Vim, which means Vim will tell the terminal it wants to capture and process mouse events, so they'll go to the remote Vim rather than stay with the local terminal.

So the problem is not actually that CTRL+SHIFT+C isn't working... But that it has nothing to copy, since there's no selection in the terminal, as the mouse events were sent to the remote Vim which used them to select a Visual block.

Debian 8 shipped Vim version 7.4, while Debian 10 ships Vim 8+ and starting with Vim 8 a new defaults.vim is shipped which runs when you have no vimrc file and will set mouse=a (or mouse=nvi) automatically, so perhaps that's where you're getting the setting from? Or if you do have a vimrc file, perhaps Debian now included this setting on the system vimrc file and that's where it's coming from...

You have a couple alternatives to solve this issue. One is to disable Vim capture of the mouse, with :set mouse=, which you can execute on a running Vim to confirm it fixes the issue for you, or add to your ~/.vimrc file to make that permanent. (If you're creating a new vimrc file for the first time, see the instructions in :help defaults.vim to preserve the defaults you've been otherwise using!)

Another option is that your terminal probably has a way to override mouse capture and use a local selection, even when the application running in the terminal has captured the mouse. That probably involves doing a selection while holding a modifier key, such as ALT or CTRL or perhaps SHIFT, so that the terminal knows you actually want the other behavior of the mouse for this particular action. Terminals might vary in which modifier key they use, but one of these is likely to trigger the behavior you want in yours. It's usually easy to tell, since mouse selection in a terminal is visually quite different from Visual mode in Vim. Once you have done the normal mouse selection using an override, you can then copy text using CTRL+SHIFT+C as you're used to. This approach gets you the benefits of both worlds, since you still get to use the mouse inside Vim, but when you actually want a selection for copy and paste, it's available to you.

How to run local code on remote host using SSH client?

In general, you can run your local scripts remotely like that:

$ ssh <your-server> "bash -s" < ./script-1.sh

Edit - This is probably a very good related post: How can I execute local script on remote machine and include arguments?



Related Topics



Leave a reply



Submit