macos vim + COPY + mac 通过 SSH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10694516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:11:24  来源:igfitidea点击:

vim + COPY + mac over SSH

macosvimssh

提问by evolution

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.

我通过 ssh 访问服务器,在该服务器上运行 vim 以编辑文件。当我尝试使用 y 或 "+y 将 vim 中的文本从我的 mac (lion) 本地拖入编辑器时,它不起作用。我最终得到的是我在本地复制的文本。如果我只在其中使用 p ,它确实有效vim 好吧。

回答by romainl

To expand on Ray's answer…

扩展雷的回答......

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

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

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

因此,y永远不会将拉出的文本放在您的本地剪贴板中。

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

要将一段文本远程 Vim复制本地计算机的剪贴板,您有以下三个选项:

  • Select the text with your mouse and hit Cmd+Clike 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 visualmode selection which can't be copied with Cmd+C. As a workaround, you can use Alt+mouse to select the text without entering visualmode or simply remove this setting from your remote ~/.vimrc.

    3. Line numbers are copied as well.

  • Put the yanked text in a temporary file, scpit to your local machine and use pbcopyto 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.

  • 用鼠标选择文本并在任何 Mac OS X 应用程序中点击Cmd+ C

    显然,它似乎是最简单的,但它至少有三个限制:

    1. 它仅限于当前屏幕。如果您要拉取的文本未完全显示,您将无法复制所有内容。

    2. 它与set mouse=a. 使用此选项,任何使用鼠标选择内容的尝试都将导致无法使用+复制的视觉模式选择。作为一种解决方法,您可以使用+mouse 选择文本而不进入可视模式,或者只需从遥控器中删除此设置。CmdCAlt~/.vimrc

    3. 行号也被复制。

  • 将提取的文本放在一个临时文件中,将scp它放到您的本地机器上并用于pbcopy将其放入您的系统剪贴板中。

    这个解决方案似乎有点复杂,但它有效(问题本身也有点复杂)。多年来,我看到了许多不同的实现,从简单的单行代码到客户端/服务器设置。这是一个,请随意谷歌搜索其他人。

  • 如果可用,使用 X-forwarding 将本地剪贴板连接到远程剪贴板。

回答by Dimitar K

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。

回答by Dean

My go-to solution is to edit the file with vim from your local machine via scp.

我的首选解决方案是通过 scp 从本地机器使用 vim 编辑文件。

:e scp://[email protected]//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 设置、插件等)

回答by Mohit Gianani

My first answer on stackoverflow, but I feel it's a cool (albeit tiny) trick and it's worth posting. So here's what I do :

我在 stackoverflow 上的第一个答案,但我觉得这是一个很酷(虽然很小)的技巧,值得发布。所以这就是我所做的:

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+Cand 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 行的文件,它运行良好。

回答by Ray Toal

Yanking within viin 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.

vi您通过 ssh 连接到的终端中猛拉,将这些行复制到远程机器上 vi 的内部缓冲区中,而不是复制到 Mac 的剪贴板中。

Use your mouse. :)

使用鼠标。:)

回答by andrewgazelka

iTerm2 Shell Utilities come with it2copywhich allows copying from a remote server to a client clipboard. Install Shell Utilities on the remote server and make sure you have Applications in terminal may access clipboardchecked. enter image description here

iTerm2 Shell Utilities 附带it2copy允许从远程服务器复制到客户端剪贴板。在远程服务器上安装 Shell Utilities 并确保您已Applications in terminal may access clipboard检查。 在此处输入图片说明

Then, go into visual mode, select the text and execute <,'>:w !it2copy

然后,进入visual mode,选择文本并执行<,'>:w !it2copy

回答by ningsuhen

Here's an update on the solution #2 from romainl. 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. In short, you don't have to do anything except paste the code snippet below into your bash_profile.

这是来自 romainl 的解决方案 #2 的更新。它创建 ssh 命令的别名并启动 remotecopyserver(如果它没有运行)并在远程服务器中安装 remotecopy(rclip)。简而言之,除了将下面的代码片段粘贴到您的 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 }'`
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 vsshwhich will execute vagrant sshbut 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. sshois available if you need to execute ssh without the whole remotecopyserver thing. Alternatively, you can use the alias ssh2 and keep the ssh command as it is.

别名包装了 ssh 命令,可能会出现一些意想不到的问题。ssho如果您需要在没有整个 remotecopyserver 的情况下执行 ssh,则可用。或者,您可以使用别名 ssh2 并保持 ssh 命令不变。

回答by mdev

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.

如果行数很少超出我的屏幕,我在使用鼠标复制 vim 文本时经常使用的一个技巧是最小化(Cmd + '-')文本。:) 尽量减少肉眼看不到的内容,但您可以一次性复制所有文本。

回答by Linh

On MacOS, when SSH from machine A to machine B and using vim in machine B, I add this to my .vimrcin machine B:

在 MacOS 上,当 SSH 从机器 A 到机器 B 并在机器 B 中使用 vim 时,我将其添加到我.vimrc的机器 B 中:

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 +clipboardand *register

这样,在正常模式下,如果您将某些yr内容复制到 * register,然后键入,则*vim@machine_B 中的register内容将复制到机器 A 的本地剪贴板,假设您已正确设置 Vim+clipboard*注册

回答by Zeth

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.

或者,从终端写入less [filename]以将其写入终端。然后开始用鼠标选择,同时按住 -down-arrow键。然后你可以选择整串。