防止 Vim 在退出时清除剪贴板
当我从终端打开 Vim,将一些文本复制到系统剪贴板,然后退出 Vim 时,系统剪贴板将被清除。
如何将复制的文本保留在剪贴板中?
When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.
How to keep the copied text in the clipboard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我遇到了这个问题和一个相关的问题,使用 ctrl-z 暂停 vim 也会清除剪贴板。我扩展了 Matt 的解决方案 来修复这两个问题:
ifexecutable("xsel")
防护如果未安装xsel
,是否可以避免出现错误。nnoremap
映射在从正常模式挂起时保留剪贴板,而vnoremap
映射处理从可视或选择模式挂起。我已经确认这适用于 vim 7.4 和 8.0。
I ran into this issue and a related problem where suspending vim with
ctrl-z
would also clear the clipboard. I've extended Matt's solution to fix both:The
if executable("xsel")
guard is there to avoid errors ifxsel
is not installed. Thennoremap
mapping preserves the clipboard when suspending from normal mode and thevnoremap
mapping handles suspending from visual or select modes.I've confirmed this works on vim 7.4 and 8.0.
使用 Neovim。默认情况下,退出时不会清除剪贴板。您仍然需要
设置clipboard=unnamedplus
(通常在~/.config/nvim/init.vim
中)并拥有xsel
或xclip
工具已安装。请记住,其他一些默认值也有所不同。
Use Neovim. It by default doesn't clear the clipboard on exit. You will still need to
set clipboard=unnamedplus
(typically in~/.config/nvim/init.vim
) and havexsel
orxclip
tools installed.Keep in mind that some other defaults are different as well.
基于 Matt 的回答
当使用他的方法复制多行时,粘贴时会在行尾添加斜杠。
这应该可以解决这个问题。
当我将“shellescape”与“system”一起使用时,换行符不断被转义。但当我使用exe时,这并没有发生。
不知道真正的原因。但这有效。
Based on Matt's answer
When using his method copying multiple lines added slashes to the end of lines when pasting.
This should remedy that.
When i used "shellescape" with "system" newlines kept getting escaped. But that didn't happen when i used exe.
Don't know the real reason. but this worked.
如果我错了,请纠正我,但根据我对 Vim 的理解......
1) Vim 使用寄存器而不是剪贴板来存储复制/剪切的数据。
2) 这些寄存器在退出 vim 时保留在状态文件中,但在运行进程之外无法访问,除非您手动打开文件并检查其内容
3) 在 Vim 运行时将内容保存到 + 寄存器允许您粘贴到其他应用程序。
4) 通过暂停 vim (CTRL-Z) 而不是关闭它,这些寄存器仍然可以访问。
这能提供帮助吗?
Please correct me if I'm wrong but from my understandings of Vim...
1) Vim uses registers instead of the clipboard to store copied/cut data.
2) These registers are preserved when exiting vim in a status file but are not accessible outside of the running process unless you manually open the file and inspect its contents
3) Saving stuff to the + registre while Vim runs allows you to paste to other applications.
4) By suspending vim (CTRL-Z) instead of closing it, these registers are still accessible.
Does that provide assistance?
这是 Neovim 的
init.lua
的解决方案,它也避免了这个 bug< /a> 及其产生的错误消息,退出 Neovim 时:我当前无法测试 Wayland 的解决方案(假设 Wayland 具有相同的问题),但对于 wl-copy ,它应该能够在命令之后将要复制的文本作为字符串列表,因此这样的方法应该可行:
不过,退出后似乎需要一两秒钟才能真正复制。
Here's a solution for Neovim's
init.lua
, which also avoids this bug and its resulting error message, when exiting Neovim:I can't test a solution for Wayland currently (assuming Wayland has the same issue), but for
wl-copy
, it should be able to take the text to be copied as a list of strings after the command, so an approach like this should work:It does seem like it takes a second or two to actually get copied after exiting, though.
综合来自超级用户的答案,只需将以下内容添加到您的 <代码>.vimrc
Synthesizing answers from superuser, just add the following to your
.vimrc
安装 Parcellite,或适用于 Gnome 的 glipper 和适用于 KDE 的 klipper。
重新启动计算机或手动运行它。
请参阅:https://wiki.ubuntu.com/ClipboardPersistence
Install Parcellite, or glipper for Gnome and klipper for KDE.
Restart your computer or run it manually.
see: https://wiki.ubuntu.com/ClipboardPersistence
基于 Matt 的回答,以下使用
xclip
而不是xsel
:Based on Matt's answer, the following uses
xclip
instead ofxsel
: