简体   繁体   English

vim + COPY + mac over SSH

[英]vim + COPY + mac over SSH

I access a sever over ssh on which I run vim for editing files.我访问了一个超过 ssh 的服务器,我在上面运行 vim 来编辑文件。 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.当我尝试使用 y 或 "+y 将文本从 vim 猛拉到我的 mac(狮子)本地编辑器时,它不起作用。我最终得到了我上次在本地复制的文本。如果我只是在其中使用 p,它确实有效vim 好的。

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.当您通过 SSH远程服务器上使用 Vim,您在 Vim 中所做的一切都在远程服务器上完成。 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.远程服务器您在其上运行的远程 Vim 对本地计算机及其系统剪贴板的实际知识为零。

Because of that, y will never put the yanked text in your local clipboard.正因为如此, y永远不会把拉取的文本放在你的本地剪贴板中。

In order to copy a chunk of text from the remote Vim to your local machine's clipboard you have three options:要将一段文本远程 Vim 复制本地计算机的剪贴板,您有以下三个选项:

  • Select the text with your mouse and hit Cmd + C like in any Mac OS X application.用鼠标选择文本,然后像在任何 Mac OS X 应用程序中一样点击Cmd + C。

    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 .它不能很好地与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 .使用此选项,任何使用鼠标选择内容的尝试都将导致无法使用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 .作为一种解决方法,您可以使用Alt +mouse 选择文本而不进入可视模式,或者只需从远程~/.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.将提取的文本放在一个临时文件中,将其scp到您的本地机器并使用pbcopy将其放入您的系统剪贴板。

    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.如果可用,使用 X-forwarding 将本地剪贴板连接到远程剪贴板。

My first answer on stackoverflow, but I feel it's a cool (albeit tiny) trick and it's worth posting.我在 stackoverflow 上的第一个答案,但我觉得这是一个很酷(虽然很小)的技巧,值得发布。 So here's what I do :所以这就是我所做的:

cat <filename>

When the text is printed onto the terminal, I select all the text with my mouse (the mouse scroll works since we're on the terminal window).当文本打印到终端上时,我用鼠标选择所有文本(鼠标滚动工作,因为我们在终端窗口上)。 Then copy that text with Cmd + C and paste into my local text editor.然后使用Cmd + C复制该文本并粘贴到我的本地文本编辑器中。

The only flaw with this trick is that it's impractical to use if your files are tens of thousands of lines long since selecting all the lines with your mouse would be a task in itself.这个技巧的唯一缺陷是,如果您的文件有数万行,那么使用它是不切实际的,因为用鼠标选择所有行本身就是一项任务。 But for a file of ~2k lines it works well.但是对于大约 2k 行的文件,它运行良好。

My go-to solution is to edit the file with vim from your local machine via scp.我的首选解决方案是通过 scp 从本地机器使用 vim 编辑文件。

:e scp://remoteuser@server.tld//path/to/document

This keeps your buffer local and makes it easy to copy to your local clipboard.这使您的缓冲区保持在本地,并使复制到本地剪贴板变得容易。

The other advantage is that you get to use your local vim setup (.vimrc settings, plugins, etc.)另一个优点是您可以使用本地 vim 设置(.vimrc 设置、插件等)

Had this problem - log in from OSX over SSH to a linux box and cannot copy text from a file, opened with vim.遇到了这个问题 - 通过 SSH 从 OSX 登录到 linux 机器并且无法从使用 vim 打开的文件中复制文本。

My workaround is :set mouse=i我的解决方法是: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.当您将其设置为仅在插入模式下启用时,您可以在不编辑时(正常模式)滚动和复制,但是当您开始编辑(通过按 I 或 Insert 键)并进入插入模式时,鼠标充当光标位置并且您无法从终端复制。

You can set that option in ~/.vimrc您可以在 ~/.vimrc 中设置该选项

See :help mouse for more information about the values you can set and the modes.有关您可以设置的值和模式的更多信息,请参阅 :help mouse。

iTerm2 Shell Utilities come with it2copy which allows copying from a remote server to a client clipboard. iTerm2 Shell Utilities 附带it2copy ,它允许从远程服务器复制到客户端剪贴板。 Install Shell Utilities on the remote server and make sure you have Applications in terminal may access clipboard checked.在远程服务器上安装 Shell Utilities 并确保您已选中Applications in terminal may access clipboard 在此处输入图片说明

Then, go into visual mode , select the text and execute <,'>:w !it2copy然后,进入visual mode ,选择文本并执行<,'>:w !it2copy

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.在您通过 ssh 连接到的终端中的vi中,将行复制到远程机器上的 vi 内部缓冲区中,而不是复制到 Mac 的剪贴板中。

Use your mouse.使用鼠标。 :) :)

Here's an update on the solution #2 from romainl.这是来自 romainl 的解决方案 #2 的更新。 It creates and alias of the ssh command and launches the remotecopyserver if it's not running and installs the remotecopy(rclip) in the remote server.它创建 ssh 命令的别名并启动 remotecopyserver(如果它没有运行)并在远程服务器中安装 remotecopy(rclip)。 In short, you don't have to do anything except paste the code snippet below into your bash_profile.简而言之,除了将下面的代码片段粘贴到您的 bash_profile 中之外,您无需执行任何操作。

######################## For SSH Remote Copy #########################
export LC_SETUP_RC='command -v rclip >/dev/null 2>&1 || { echo "executing"; mkdir -p /usr/local/bin; if [ ! -f /usr/local/bin/rclip ];then wget https://raw.githubusercontent.com/justone/remotecopy/master/remotecopy -P /usr/local/bin/; ln -s /usr/local/bin/remotecopy /usr/local/bin/rclip; chmod +x /usr/local/bin/remotecopy; fi; if [[ \":\$PATH:\" == *\"/usr/local/bin:\"* ]]; then export PATH=/usr/local/bin:$PATH; fi } > /var/log/rclip.log 2>&1 || echo "Some error occured in setting up rclip. check /var/log/rclip.log"'

ssh_function() {
count="`ps -eaf | grep remotecopyserver | grep -v grep | wc -l`";
if [ "$count" -eq "0" ]; then 
   mkdir -p $HOME/bin;
   if [ ! -f $HOME/bin/remotecopyserver ]; then 
      wget https://raw.githubusercontent.com/justone/remotecopy/master/remotecopyserver -P $HOME/bin;
      chmod +x $HOME/bin/remotecopyserver;
   fi;
   nohup $HOME/bin/remotecopyserver & 
fi;
ssh_cmd=`which ssh`
PARAMS=""
for PARAM in "$@"
do
  PARAMS="${PARAMS} \"${PARAM}\""
done
bash -c "ssh ${PARAMS} -R 12345:localhost:12345 -t 'echo \$LC_SETUP_RC | sudo bash; bash -l'"
}
alias ssho=`which ssh`
alias ssh=ssh_function
alias ssh2=ssh_function

vssh_function() {
ssh_config=`vagrant ssh-config`;
if [ "$?" -eq "1" ]; then
echo "Problem with Vagrant config. run 'vagrant ssh-config' to debug"
return 1
fi
PORT=`echo "$ssh_config" | grep Port | grep -o "[0-9]\+"`; 
ID_FILE=`echo "$ssh_config" | grep IdentityFile | awk '{print $2}'`
ssh2 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -i $ID_FILE vagrant@localhost -p $PORT "$@"
}
alias vssh=vssh_function

Once the alias is activated, you can normally use ssh and whenever you need to copy to local clipboard from vim, use别名激活后,您通常可以使用 ssh 并且每当您需要从 vim 复制到本地剪贴板时,请使用

:w !rclip 

to copy the whole file to clipboard将整个文件复制到剪贴板

:'<,'> !rclip

to copy selected lines from visual mode.从视觉模式复制选定的行。 You have to press "Cmd+V" or "Ctrl+V" whenever it asks for the secret key.每当它要求提供密钥时,您都必须按“Cmd+V”或“Ctrl+V”。

Bonus奖金

For those who work on Vagrant, there's a wrapper vssh which will execute vagrant ssh but also launches and install the necessary components.对于那些在 Vagrant 上工作的人,有一个包装器vssh ,它将执行vagrant ssh,但也会启动和安装必要的组件。

Reference参考

Latest code snippet here - https://gist.github.com/ningsuhen/7933b206b92fc57364b2最新代码片段在这里 - https://gist.github.com/ningsuhen/7933b206b92fc57364b2

http://endot.org/2011/12/04/remotecopy-copy-from-remote-terminals-into-your-local-clipboard/ http://endot.org/2011/12/04/remotecopy-copy-from-remote-terminals-into-your-local-clipboard/

https://github.com/justone/remotecopy https://github.com/justone/remotecopy

Caveats注意事项

The alias wraps the ssh command and there might be some unexpected issues.别名包装了 ssh 命令,可能会出现一些意外问题。 ssho is available if you need to execute ssh without the whole remotecopyserver thing.如果您需要在没有整个 remotecopyserver 的情况下执行 ssh,则ssho可用。 Alternatively, you can use the alias ssh2 and keep the ssh command as it is.或者,您可以使用别名 ssh2 并保持 ssh 命令不变。

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.如果行数很少超出我的屏幕,我在使用鼠标复制 vim 文本时经常使用的一个技巧是最小化(Cmd + '-')文本。 :) Minimize so much that you can not see by eyes but you can copy all the text in one go. :) 尽量减少肉眼看不到的内容,但您可以一次性复制所有文本。

Or, from the terminal, write less [filename] to get it written to the terminal.或者,从终端, less [filename]less [filename]以将其写入终端。 Then start byt selecting with your mouse, while you hold down-arrow -key down.然后开始用鼠标选择,同时按住down-arrow键。 Then you can select the whole bunch.然后你可以选择整束。

On MacOS, when SSH from machine A to machine B and using vim in machine B, I add this to my .vimrc in machine B:在 MacOS 上,当从机器 A SSH 到机器 B 并在机器 B 中使用 vim 时,我将其添加到机器 B 中的.vimrc中:

nmap yr :call system("ssh $machineA_IP pbcopy", @*)<CR>

That way, in normal mode, if you copy something to * register, then type yr , the content of * register in vim@machine_B is copied to machine A's local clipboard, assuming you have setup Vim correctly with +clipboard and * register这样,在正常模式下,如果您将某些内容复制到 * register,然后键入yr ,则 vim@machine_B 中* register 的内容将复制到机器 A 的本地剪贴板,假设您已使用+clipboard* register 正确设置 Vim

I was hoping to improve on my solution here, but instead will share it as it seems on par with some of the others.我希望在这里改进我​​的解决方案,但会分享它,因为它似乎与其他一些解决方案相当。

When using iTerm2 on a Mac, sshing into machines, running tmux, and then editing with Vim, I have a vertical split iTerm2 window on the Mac that I pull all the way off to the side to make it as skinny as possible.当在 Mac 上使用 iTerm2、sshing 到机器上、运行 tmux,然后使用 Vim 进行编辑时,我在 Mac 上有一个垂直拆分的 iTerm2 窗口,我将它一直拉到一边以使其尽可能瘦。

Then when I want to copy text from Vim, I will click into the tiny slice of iTerm2 window, and go back over and highlight and then copy the text from Vim.然后当我想从 Vim 复制文本时,我会点击 iTerm2 窗口的一小部分,然后返回并突出显示,然后从 Vim 复制文本。 This works the best for single lines of text.这最适用于单行文本。

If there is a tmux vertical split, highlighting multiple lines in the Vim buffer won't wrap properly, and will copy text from the other tmux window, but otherwise this is great for copying 90% of what I need, without having to exit Vim, cat a file, or do something else.如果存在 tmux 垂直拆分,则在 Vim 缓冲区中突出显示多行将无法正确换行,并且会从另一个 tmux 窗口复制文本,否则这非常适合复制我需要的 90% 的内容,而无需退出 Vim , cat 文件,或做其他事情。

I also have ample horizontal window space, making the small iTerm2 window not a space hog.我也有充足的水平窗口空间,使小的 iTerm2 窗口不是空间猪。

Just use MobaXterm.只需使用 MobaXterm。
I tried lots of ways and none of them is easy.我尝试了很多方法,但没有一个是容易的。 So the only solution I could find is using MobaXterm.所以我能找到的唯一解决方案是使用 MobaXterm。 You can see all of the files and open them any way you want.您可以查看所有文件并以任何方式打开它们。

https://github.com/ojroques/vim-oscyank + iTerm2 + tmux worked in my case, this is my workflow: https://github.com/ojroques/vim-oscyank + iTerm2 + tmux 在我的情况下工作,这是我的工作流程:

Some parts of my .vimrc我的 .vimrc 的某些部分

Plug 'ojroques/vim-oscyank'  " clipboard over ssh through tmux

" yank operation don't need to use the *" register (system clipboard)
set clipboard+=unnamedplus

autocmd TextYankPost * if v:event.operator is 'y' && v:event.regname is '' | OSCYankReg " | endif

The easiest way to copy to local machine is via selecting with mouse and then CMD + c to copy.复制到本地计算机的最简单方法是通过鼠标选择,然后选择CMD + c进行复制。 As is noted in another answer this has a few limitations/drawbacks.正如在另一个答案中指出的那样,这有一些限制/缺点。 Consequently, I sought out the alternatives, but after trying for hours (unsuccessfully) to get X11 forwarding working, I instead came up with the following simple workaround (at least for the line-numbering caveat) for aid in mouse copying.因此,我寻找了替代方案,但在尝试了几个小时(未成功)让 X11 转发工作之后,我想出了以下简单的解决方法(至少对于行号警告)来帮助鼠标复制。

Add the following to your .vimrc to easily toggle line numbers for mouse copying:将以下内容添加到您的.vimrc以轻松切换行号以进行鼠标复制:

nnoremap L :set invnumber <CR>

You can easily change the L to any key of your choice that you wish to be the shortcut key.您可以轻松地将L更改为您希望成为快捷键的任何键。

Instructions for mac + ssh + iterm2 + neovim: mac + ssh + iterm2 + neovim 使用说明:

  1. Ssh to your server. Ssh 到您的服务器。
  2. Install iterm shell integration .安装 iterm shell 集成
  3. Add ~/.iterm2 to the $PATH variable.~/.iterm2添加到$PATH变量。
  4. Create a ttyup command in eg ~/.local/bin .在例如~/.local/bin中创建一个ttyup命令。
  5. Add ~/.local/bin to $PATH , if it's not already.~/.local/bin添加到$PATH (如果还没有的话)。
  6. Add the g:clipboard declaration to your init.vim .g:clipboard声明添加到您的init.vim中。

ttyup : ttyup

#!/bin/bash
# @medgar's ttyup script for passing the output to the parent shell

parent() { awk '{print $4}' "/proc/$1/stat"; }
leader() { awk '{print $6}' "/proc/$1/stat"; }
it2copy > "/proc/$(parent $(leader $$))/fd/0"

init.vim : init.vim

let g:clipboard = {
\   'name': 'myClipboard',
\   'copy': {
\      '+': ['ttyup'],
\      '*': ['ttyup'],
\   },
\   'paste': {
\      '+': '+',
\      '*': '*',
\   },
\   'cache_enabled': 0,
\ }

@andrewgazelka had a great solution @andrewgazelka 有一个很好的解决方案

I don't have it2copy script: https://github.com/gnachman/iTerm2-shell-integration/blob/main/utilities/it2copy我没有 it2copy 脚本: https://github.com/gnachman/iTerm2-shell-integration/blob/main/utilities/it2copy

Here is the code I downloaded from:)这是我下载的代码:)

Hope someone will feel it is helpful:)希望有人会觉得它有帮助:)

I moved it2copy in my /usr/bin directory for both my server and macbook我将 it2copy 移动到我的服务器和 macbook 的 /usr/bin 目录中

尝试另一个剪贴板寄存器 - "*y .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM