在启用 set mouse=a 的情况下在 Vim 外部复制文本

发布于 2024-10-10 12:07:38 字数 199 浏览 10 评论 0原文

启用 set mouse=a 后,在 Vim 内部复制的文本将不会粘贴到 Vim 外部。有办法解决这个问题吗?

在这里,用鼠标选择文本会打开可视模式并禁用弹出菜单中的 Copy 选项:

在此输入图像描述

After enabling set mouse=a, text copied inside of Vim will not paste outside of Vim. Is there a way to fix this?

Here, selecting text with the mouse turns on visual mode and disables the Copy option in the popup menu:

Enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

月亮邮递员 2024-10-17 12:07:38

使用鼠标选择时按 Shift。这将使鼠标选择的行为就像未启用 mouse=a 一样。

注意:这个技巧也适用于“中间按钮粘贴”:如果您想粘贴在外部选择的 Vim 文本,请在单击中间按钮的同时按 Shift。只需确保执行此操作时激活插入模式即可(您可能还需要 :set Paste 以避免意外效果)。

操作系统 X(苹果机):
按住Alt/Option,同时选择(来源)

Press Shift while selecting with the mouse. This will make mouse selection behave as if mouse=a was not enabled.

Note: this trick also applies to "middle button paste": if you want to paste in Vim text that was selected outside, press Shift while clicking the middle button. Just make sure that insert mode is activated when you do that (you may also want to :set paste to avoid unexpected effects).

OS X (Mac):
hold Alt/Option while selecting (source)

笑忘罢 2024-10-17 12:07:38

使用键盘或鼠标进行视觉选择后使用 "+y。您不应使用终端的复制命令无论如何,因为它复制了终端看到的内容而不是实际内容,其作用如下:

  • ",+ 告诉 Vim 使用名为 +< 的寄存器。 /code> 用于下一次删除、拉取或放置。名为+的寄存器是一个特殊寄存器,它是X11剪贴板寄存器。 (在其他系统上,我认为您可以使用 * 代替,请参阅 :help Clipboard:help x11-selection
  • y 是 yank 命令,它告诉 Vim 将选择内容放入先前指定的寄存器中。

您可以这样映射它:

:vmap <C-C> "+y

然后用鼠标突出显示某些内容并按 Control-C 复制它。

此功能仅在使用 +xterm_clipboard 选项编译 Vim 时才有效。运行 vim --version 来查看是否有。

Use ", +, y after making a visual selection either with the keyboard or the mouse. You shouldn’t be using the terminal’s copy command anyway, because that copies what the terminal sees instead of the actual content. Here is what this does:

  • ",+ tells Vim to use the register named + for the next delete, yank or put. The register named + is a special register, it is the X11 clipboard register. (On other systems, you would use * instead, I think, see :help clipboard and :help x11-selection)
  • y is the yank command, which tells Vim to put the selection in the register named previously.

You could map it like this:

:vmap <C-C> "+y

And then highlight something with the mouse and press Control-C to copy it.

This feature only works when Vim has been compiled with the +xterm_clipboard option. Run vim --version to find out if it has.

拍不死你 2024-10-17 12:07:38

.vimrc.

Instead of set mouse=a, use set mouse=r in .vimrc.

幸福%小乖 2024-10-17 12:07:38

在 OS X 上,使用 Fn 而不是 Shift

On OS X, use Fn instead of Shift.

绝對不後悔。 2024-10-17 12:07:38

X-Mac 操作系统的另一个选项是取消选中菜单查看允许鼠标报告(或按 + R来切换它)。这允许您在鼠标交互和鼠标选择之间切换,这在选择和复制/粘贴一些位时可能很有用,因为您不必按住修饰键来执行此操作。

带行号的多行注意事项:

我通常启用行号,因此如果您选择多行,这也会复制行号。如果您想复制不带行号的多行,请使用 :set nonu 禁用行号,然后可以在完成后使用 :set nu 重新启用它们复制。

Another OS X-Mac option is to uncheck menu ViewAllow Mouse Reporting (or press + R to toggle it). This allows you to toggle between mouse interaction and mouse selecting, which might be useful when selecting and copy/pasting a few bits, because you don't have to hold a modifier key to do it.

Note for Multiline with line numbers:

I usually have line numbers enabled, so this will also copy the line numbers if you select multiple lines. If you want to copy multiple lines without the line numbers, disable the numbers with :set nonu and then you can :set nu to re-enable them after you're done copying.

鹊巢 2024-10-17 12:07:38

Ubuntu 中,可以使用 X-Term 副本和在 VIM 中粘贴绑定(Ctrl + Shift + CCtrl + Shift + V)在使用 Shift 键突出显示的文本上。

In Ubuntu, it is possible to use the X-Term copy & paste bindings inside VIM (Ctrl + Shift + C and Ctrl + Shift + V) on text that has been highlighted using the Shift key.

爱情眠于流年 2024-10-17 12:07:38

当我重读这个问题并发现OP一开始就不想关闭它时,我不小心解释了如何关闭set mouse=a。无论如何,对于任何搜索如何集中关闭鼠标(set mouse=)的人,我在这里留下对我的答案的引用:https://unix.stackexchange.com/a/506723/194822

I accidently explained how to switch off set mouse=a, when I reread the question and found out that the OP did not want to switch it off in the first place. Anyway for anyone searching how to switch off the mouse (set mouse=) centrally, I leave a reference to my answer here: https://unix.stackexchange.com/a/506723/194822

心欲静而疯不止 2024-10-17 12:07:38

set Clipboard=unnamed 添加到 .vimrc 中。因此它将使用剪贴板寄存器“*”而不是未命名的寄存器来进行所有的复制、删除、更改和放置操作(注意它不仅影响鼠标)。

寄存器 '*' 的行为取决于您的平台以及 vim 的编译方式(或者您是否使用 neovim)。

如果它不起作用,您可以尝试使用 set Clipboard=unnamedplus,但此选项仅在 X11 系统上有意义(因此也适用于 gvim)。

Add set clipboard=unnamed to your .vimrc. So it will use the clipboard register '*' instead of the unnamed register for all yank, delete, change and put operations (note it does not only affect the mouse).

The behavior of register '*' depends on your platform and how your vim has been compiled (or if you use neovim).

If it does not work, you can try with set clipboard=unnamedplus, but this option only makes sense on X11 systems (and gvim therefore).

东风软 2024-10-17 12:07:38

您可以在 Vim 命令行中使用 :set mouse& 来复制/粘贴使用鼠标选择的文本。然后,您只需使用鼠标中键或 Shift + Insert 即可粘贴它。

You can use :set mouse& in the Vim command line to enable copy/paste of text selected using the mouse. You can then simply use the middle mouse button or Shift + Insert to paste it.

你怎么敢 2024-10-17 12:07:38

Vim 编译时使用的编译设置是问题的一部分。 vim --version 显示这些。

在 OS X 中,默认的 Vim 有 -clipboard。但你需要+clipboard

在 OS X 上,您可以而且通常应该使用 MacVim。您可以执行brew cask install macvim。那个有+clipboard

您将安装两个 Vim。

~$ ls -l /usr/bin/vim   <--- default vim
-rwxr-xr-x  1 root  wheel  1745984 15 Jul  2017 /usr/bin/vim

~$ ls -l /usr/local/bin/vim   <-- macvim, installed recently via that mentioned brew line.
lrwxr-xr-x  1 apple  admin  42 16 May 23:32 /usr/local/bin/vim -> /Applications/MacVim.app/Contents/bin/mvim

运行 Vim 将运行 MacVim,因为 /usr/local/bin 应该位于路径中的 /usr/bin 之前,尽管您可以使用 which vim 检查代码>.

运行 Vim(运行 MacVim)很好,但您可能希望将 vi 映射到 MacVim,因为否则运行 vi 会保留默认的 Vim!您可以重写或删除(使用 rm)并重新创建 vi 符号链接,带有 ln。要做到这一点而不出现“不允许操作”错误,您必须(暂时)禁用 SIL。如何在 macOS 上禁用系统完整性保护 (SIP),又称“无根”?

MacVim有 +clipboardvim --version 所示,

这是一个工作的 ~/.vim/vimrc ,其中包含默认的行

:set mouse=a
:map <leader>c "+y
:map <leader>v "+p

。领导键是反斜杠。

我读到一个建议,应该使用领导键...(当然,控制有很多键已经在使用,所以建议是不要使用控制。我不知道这是否也适用于命令键,但无论如何)。

通过上述映射,\c 将执行 "+y 操作,将从称为 + 的寄存器复制到剪贴板。 \v 将从称为 + 的寄存器粘贴。

这就是在窗口之间进行的复制/粘贴。

另一个操作系统可能需要 "* 而不是 "+

Compilation settings that Vim was compiled with, are part of the issue. vim --version shows these.

In OS X, the default Vim has -clipboard. But you need +clipboard.

On OS X, you can and apparently generally should, use MacVim. You can do brew cask install macvim. That one has +clipboard.

Them you'll have two Vim installations.

~$ ls -l /usr/bin/vim   <--- default vim
-rwxr-xr-x  1 root  wheel  1745984 15 Jul  2017 /usr/bin/vim

~$ ls -l /usr/local/bin/vim   <-- macvim, installed recently via that mentioned brew line.
lrwxr-xr-x  1 apple  admin  42 16 May 23:32 /usr/local/bin/vim -> /Applications/MacVim.app/Contents/bin/mvim

Running Vim will run MacVim, because /usr/local/bin should be before /usr/bin in the path, though you can check with which vim.

Running Vim (to run MacVim), is fine, but you may want to map vi to MacVim, because otherwise running vi stays at the default Vim! You can rewrite or delete (with rm) and recreate the vi symbolic link, with ln. And to do that without an 'operation not permitted" error, you have to (temporarily) disable SIL. How do I disable System Integrity Protection (SIP) AKA "rootless" on macOS?.

MacVim has +clipboard as shown by vim --version.

Here is a working ~/.vim/vimrc with just the required lines.

:set mouse=a
:map <leader>c "+y
:map <leader>v "+p

The default leader key is backslash.

I read a suggestion that one should use the leader key... (certainly control has many keys already in use, so the suggestion was to not use control. I don't know if that applies to command key too, but anyhow).

With that mentioned mapping, \c will do "+y which will copy from the register known as +, to the clipboard. And \v will paste from the register known as +.

So that's a copy/paste that works between windows.

Another OS may require "* rather than "+.

感性 2024-10-17 12:07:38

另外值得一提的是,通过 set mouse=nvi,在进行选择时然后按 : 您将获得鼠标选择复制到主选择剪贴板(相当于 "*y)。

参考:help mouse

这种方法的主要优点是,如果您有多个垂直分割,它只会从当前缓冲区中进行选择,如主要答案中所述,在这种情况下,将同时从所有 3 个文件中进行复制,这并不完全是人们想要的。 、期望或需要。

Also worth mentioning, by having set mouse=nvi, when doing a selection and then pressing : <ESC> you will get the mouse selection copied to the primary selection clipboard (equivalent to a "*y).

Reference: help mouse

Main advantage of this method is the fact that if you have multiple vertical splits, it will only select from the current buffer. Using <Shift> as mentioned in the main answer, will, in this case, copy from all 3 files at the same time which is not exactly what one would want, expect or need.

眼眸里的快感 2024-10-17 12:07:38

如果您使用 PuTTY 会话,它会自动复制选择。
如果我们在 Vim 中使用了“set mouse=a”选项,则使用 Shift + 鼠标拖动选择文本会自动选择。
需要检查一下 X-term。

If you are using a PuTTY session, then it automatically copies the selection.
If we have used "set mouse=a" option in Vim, selecting using Shift + Mouse drag selects the text automatically.
One needs to check in X-term.

疧_╮線 2024-10-17 12:07:38

一个值得添加的好解决方法:

可以使用 GPM 守护程序,它是一个剪切和粘贴实用程序以及用于虚拟控制台的鼠标服务器。它将提供所有虚拟控制台的功能!

复制粘贴操作可以通过 Ctrl + CCtrl + V 来完成。

sudo apt-get install gpm

GPM 手册页

A good workaround which is worth adding:

The GPM daemon can be used which is a cut and paste utility and mouse server for virtual consoles. It will provide functionalities across all the virtual consoles!

Copy-paste actions can be done by Ctrl + C and Ctrl + V.

sudo apt-get install gpm

Man pages of GPM

眼趣 2024-10-17 12:07:38

在vi中设置set mouse=a,使用MobaXterm,在服务器上安装vim-gtk3后,用鼠标拖动,Ctrl + Insert起作用,但似乎它只适用于 MobaXterm。

安装 vim-gtk3 后,vi 将链接到它:

sudo apt install gvim

Output:

[sudo] password for lala:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package gvim is a virtual package provided by:
  vim-gtk3 2:8.2.2434-3ubuntu3.2
  vim-athena 2:8.2.2434-3ubuntu3.2
You should explicitly select one to install.

E: Package 'gvim' has no installation candidate

And:

which vi

Output:

/usr/bin/vi

And:

file /usr/bin/vi

Output:

/usr/bin/vi: symbolic link to /etc/alternatives/vi

And:

file /etc/alternatives/vi

Output:

/etc/alternatives/vi: symbolic link to /usr/bin/vim.gtk3

Set set mouse=a in vi, using MobaXterm, after installing vim-gtk3 on the server, dragging with mouse and Ctrl + Insert works, but seems it only work with MobaXterm.

After installing vim-gtk3, vi will link to it:

sudo apt install gvim

Output:

[sudo] password for lala:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package gvim is a virtual package provided by:
  vim-gtk3 2:8.2.2434-3ubuntu3.2
  vim-athena 2:8.2.2434-3ubuntu3.2
You should explicitly select one to install.

E: Package 'gvim' has no installation candidate

And:

which vi

Output:

/usr/bin/vi

And:

file /usr/bin/vi

Output:

/usr/bin/vi: symbolic link to /etc/alternatives/vi

And:

file /etc/alternatives/vi

Output:

/etc/alternatives/vi: symbolic link to /usr/bin/vim.gtk3
酷到爆炸 2024-10-17 12:07:38

按住Shift,然后单击鼠标右键。

Hold down Shift and then click the right mouse button.

若水般的淡然安静女子 2024-10-17 12:07:38

在ESC模式下,当set mouse=a时,使用鼠标选择文本。这将在 Vim 中启用视觉模式。然后,您可以按 Y 拉出所选文本,然后按 P 将其粘贴到您想要的位置。这种情况只发生在 Vim 中。

In ESC mode, when set mouse=a, select the text using the mouse. This would enable the visual mode in Vim. Then you can press Y to yank the selected text and P to paste it wherever you want. This happens only within Vim.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文