Search code examples
ubuntuvimsshdebian

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


There are many similar questions but none with an answer that works, and it should be trivial because it used to be.

Scenario: on an Ubuntu desktop system, I am running a terminal where I have SSH'ed to a remote server. Normally, I can select text from the terminal (that is, "from" the remote server) with the mouse, and then copy it locally with CTRL+SHIFT+C (instead of CTRL+C as I would do in any app other than a terminal) and that copies the selection to the local clipboard, which I can then paste anywhere. And that would also allow me to paste it into the terminal itself (ar another one) with CTRL+SHIFT+V, which eventually pastes it to whatever is running on the remote server (including vim).

This used to work (with debian 8 on the remote server) ALSO when running vim on the remote server , so just to clarify: in my local ubuntu machine is a terminal; inside the terminal I've ssh'd to the remote server; on the server I'm running vim; so the contents of the file opened in vim on the remote server are visible in the terminal in my local machine. Ctrl+Shift+C used to allow me to copy the selection to the LOCAL clipboard on my local machine, which I would then be able to paste into a local text editor OR back into vim on the remote machine. This way, I could copy-paste from the remote vim to a local gedit, or viceversa, or from the remote vim back to the remote vim (via my local clipboard), or from a remote vim into another remote vim in another terminal, be it on the same or another server. I'm perfectly aware that the remote server would know nothing about copying and pasting in this scenario. The local system was just copying the text it was seeing in the terminal (which happened to be the output of something running via ssh on a remote machine), and then pasting it back into the terminal (which caused it to go through ssh into the remote machine). I know that, and it worked for me.

That does not work anymore (with debian 10 on the remote machine) when vim is involved. Now on the remote server I'm running Debian 10. Ctrl+Shift+C still works (copies to local clipboard) as long as on the remote server I'm NOT running vim. For example, I can use Ctrl+Shift+C to copy text from the remote console into my local clipboard. And Ctrl+Shift+V still works whether or not I'm in vim on the remote machine. However, when vim is open on the remote machine within the terminal, Ctrl+Shift+C does NOT copy to local clipboard.

My guess is that Ctrl+Shift+C is somehow captured by vim.

So how do I copy text from a terminal into the clipboard on ubuntu, when this terminal happens to be running ssh and vim happens to be open on the remote machine?

I know I can use y to "yank" (i.e. copy) and p to paste remotely in vim. However that's not what I'm looking for. That all happens remotely and only allows me to copy from vim to vim on the remote machine (maybe also from vim to something else on the remote machine, I don't care). That does not allow me to copy from vim on the remote machine to a text editor on the local machine.

I don't want a convoluted solution involving "yanking" from vim to something else on the remote machine and then ftp'ing a file to my local machine or something. That's garbage. I want to LOCALLY COPY whatever is seen locally in the terminal despite the fact that what happens to be running in the terminal is vim through ssh on a remote machine.

I used to be able to do that already. I still can with a remote server that is running debian 8. But not on a remote server that is running debian 10.

How can I do that?


Solution

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