vim + COPY + mac over SSH

2019-03-08 01:03发布

I access a sever over ssh on which I run vim for editing files. When I try to yank text from vim into an editor locally on my mac (lion) either with y OR "+y it does not work. I end up with the text I copied last locally. It does work if I just use p within vim alright.

标签: macos vim ssh
9条回答
再贱就再见
2楼-- · 2019-03-08 01:10

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.

查看更多
神经病院院长
3楼-- · 2019-03-08 01:13

Yanking within vi in a terminal to which you ssh'd into copies the lines into vi's internal buffer on the remote machine, not into your Mac's clipboard.

Use your mouse. :)

查看更多
看我几分像从前
4楼-- · 2019-03-08 01:14

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.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-03-08 01:19

Try the other clipboard register - "*y.

查看更多
Melony?
6楼-- · 2019-03-08 01:24

One trick which i use often during copying vim text using mouse if number of lines get little over-flown my screen is to minimize (Cmd + '-') the text. :) Minimize so much that you can not see by eyes but you can copy all the text in one go.

查看更多
可以哭但决不认输i
7楼-- · 2019-03-08 01:30

Or, from the terminal, write less [filename] to get it written to the terminal. Then start byt selecting with your mouse, while you hold down-arrow-key down. Then you can select the whole bunch.

查看更多
登录 后发表回答