vim + COPY + mac over SSH

Had this problem - log in from OSX over SSH to a linux box and cannot copy text from a file, opened with vim.

My workaround is :set mouse=i

By default mouse is enabled in all modes. When you set it to be enabled only in Insert mode you can scroll around and copy when you are not editing (normal mode) but when you start editing (by hitting the I or Insert key) and enter insert mode the mouse acts as cursor placement and you cannot copy from terminal.

You can set that option in ~/.vimrc

See :help mouse for more information about the values you can set and the modes.


To expand on Ray's answer…

When you are using Vim on a remote server via SSH, everything you do in Vim is done on the remote server. The remote server and the remote Vim that you are running on it have zero practical knowledge of your local computer and its system clipboard.

Because of that, y will never put the yanked text in your local clipboard.

In order to copy a chunk of text from the remote Vim to your local machine's clipboard you have three options:

  • Select the text with your mouse and hit Cmd+C like in any Mac OS X application.

    Obviously, it seems to be the easiest but it has at least three limitations:

    1. It is limited to the current screen. If the text you want to yank is not displayed entirely you won't be able to copy all of it.

    2. It doesn't play well with set mouse=a. With this option, any attempt to select something with the mouse will result in a visual mode selection which can't be copied with Cmd+C. As a workaround, you can use Alt+mouse to select the text without entering visual mode or simply remove this setting from your remote ~/.vimrc.

    3. Line numbers are copied as well.

  • Put the yanked text in a temporary file, scp it to your local machine and use pbcopy to put it in your system clipboard.

    This solution seems to be a little convoluted but it works (and the problem itself is also a little bit convoluted). Over the years I've seen a lot of different implementations ranging from simple one liners to client/server setups. Here is one, feel free to google around for others.

  • Use X-forwarding to connect your local clipboard to the remote clipboard if available.

Tags:

Macos

Vim

Ssh