在启用 set mouse=a 的情况下在 Vim 外部复制文本
启用 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
使用鼠标选择时按 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)
使用键盘或鼠标进行视觉选择后使用 "、+、y。您不应使用终端的复制命令无论如何,因为它复制了终端看到的内容而不是实际内容,其作用如下:
+< 的寄存器。 /code> 用于下一次删除、拉取或放置。名为
+
的寄存器是一个特殊寄存器,它是X11剪贴板寄存器。 (在其他系统上,我认为您可以使用*
代替,请参阅:help Clipboard
和:help x11-selection
)您可以这样映射它:
然后用鼠标突出显示某些内容并按 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:
+
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
)You could map it like this:
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. Runvim --version
to find out if it has.在 .vimrc.
Instead of
set mouse=a
, useset mouse=r
in .vimrc.在 OS X 上,使用 Fn 而不是 Shift。
On OS X, use Fn instead of Shift.
X-Mac 操作系统的另一个选项是取消选中菜单查看 → 允许鼠标报告(或按 ⌘ + R来切换它)。这允许您在鼠标交互和鼠标选择之间切换,这在选择和复制/粘贴一些位时可能很有用,因为您不必按住修饰键来执行此操作。
带行号的多行注意事项:
我通常启用行号,因此如果您选择多行,这也会复制行号。如果您想复制不带行号的多行,请使用
:set nonu
禁用行号,然后可以在完成后使用:set nu
重新启用它们复制。Another OS X-Mac option is to uncheck menu View → Allow 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.在 Ubuntu 中,可以使用 X-Term 副本和在 VIM 中粘贴绑定(Ctrl + Shift + C 和 Ctrl + 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.
当我重读这个问题并发现OP一开始就不想关闭它时,我不小心解释了如何关闭
set mouse=a
。无论如何,对于任何搜索如何集中关闭鼠标(set mouse=
)的人,我在这里留下对我的答案的引用:https://unix.stackexchange.com/a/506723/194822I 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将
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 useneovim
).If it does not work, you can try with
set clipboard=unnamedplus
, but this option only makes sense on X11 systems (and gvim therefore).您可以在 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.Vim 编译时使用的编译设置是问题的一部分。
vim --version
显示这些。在 OS X 中,默认的 Vim 有
-clipboard
。但你需要+clipboard
。在 OS X 上,您可以而且通常应该使用 MacVim。您可以执行
brew cask install macvim
。那个有+clipboard
。您将安装两个 Vim。
运行 Vim 将运行 MacVim,因为
/usr/local/bin
应该位于路径中的/usr/bin
之前,尽管您可以使用which vim
检查代码>.运行 Vim(运行 MacVim)很好,但您可能希望将 vi 映射到 MacVim,因为否则运行 vi 会保留默认的 Vim!您可以重写或删除(使用 rm)并重新创建 vi 符号链接,带有 ln。要做到这一点而不出现“不允许操作”错误,您必须(暂时)禁用 SIL。如何在 macOS 上禁用系统完整性保护 (SIP),又称“无根”?。
MacVim有
+clipboard
如vim --version
所示,这是一个工作的
~/.vim/vimrc
,其中包含默认的行。领导键是反斜杠。
我读到一个建议,应该使用领导键...(当然,控制有很多键已经在使用,所以建议是不要使用控制。我不知道这是否也适用于命令键,但无论如何)。
通过上述映射,
\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.
Running Vim will run MacVim, because
/usr/local/bin
should be before/usr/bin
in the path, though you can check withwhich 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 byvim --version
.Here is a working
~/.vim/vimrc
with just the required lines.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"+
.另外值得一提的是,通过
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.如果您使用 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.
一个值得添加的好解决方法:
可以使用 GPM 守护程序,它是一个剪切和粘贴实用程序以及用于虚拟控制台的鼠标服务器。它将提供所有虚拟控制台的功能!
复制粘贴操作可以通过 Ctrl + C 和 Ctrl + 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
在vi中设置
set mouse=a
,使用MobaXterm,在服务器上安装vim-gtk3后,用鼠标拖动,Ctrl + Insert起作用,但似乎它只适用于 MobaXterm。安装 vim-gtk3 后,vi 将链接到它:
Output:
And:
Output:
And:
Output:
And:
Output:
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:
Output:
And:
Output:
And:
Output:
And:
Output:
按住Shift,然后单击鼠标右键。
Hold down Shift and then click the right mouse button.
在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.