How to 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

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

How can I copy the output of a command directly into my clipboard?

One way of doing it follows:

  1. Install xclip, such as:

    sudo apt-get install xclip

  2. Pipe the output into xclip to be copied into the clipboard:

    cat file | xclip

  3. Paste the text you just copied into a X application:

    xclip -o

To paste somewhere else other than an X application, such as a text area of a web page in a browser window, use:

cat file | xclip -selection clipboard

Consider creating an alias:

alias "c=xclip"
alias "v=xclip -o"

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):

Terminal 1:
pwd | c

Terminal 2:
cd `v`

Notice the ` ` around v. This executes v as a command first and then substitutes it in-place for cd to use.

Only copy the content to the X clipboard

cat file | xclip

How do you copy and paste into Git Bash

Press Insert.

Also, to copy from the window, try clicking the console's window icon (topleft) and choosing Edit -> Mark, then drag a box on the text, then press Enter. (You can also paste via the window icon menu, but the key is faster.)

UPDATE

Starting from Windows 10 the CTRL + C, CTRL + V and a lot of other feature are implemented in conhost.exe so they should work with every console utility on Windows. (You have to enable Properties -> Option tab -> Quick Edit Mode)

Ref: http://blogs.windows.com/buildingapps/2014/10/07/console-improvements-in-the-windows-10-technical-preview/

Sending clipboard contents as keystrokes to terminal window in Ubuntu

If you copied the text by marking it with your mouse, you should be able to paste it by pressing the middle mouse button in the terminal. If you copied it using ctrl+c, pressing shift+insert in the terminal should work.

Copy all the lines to clipboard

You should yank the text to the * or + registers:

gg"*yG

Explanation:

  • gg to get the cursor to the first character of the file
  • "*y to start a yank command to the register * from the first line, until...
  • G to go the end of the file

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.

Error: Can't open display: (null) when using Xclip to copy ssh public key

DISPLAY=:0 xclip -sel clip < ~/.ssh/id_rsa.pub didn't work for me (ubuntu 14.04), but you can use :

cat ~/.ssh/id_rsa.pub

to get your public key



Related Topics



Leave a reply



Submit