如何使vim从系统剪贴板粘贴(并复制到系统)? [英] How to make vim paste from (and copy to) system's clipboard?
问题描述
与其他编辑器不同,vim将复制的文本存储在其自己的剪贴板中。因此,对我来说很难从网页上复制一些文本并将其粘贴到当前的工作文件中。碰巧是我必须打开gedit或手动键入它。
Unlike other editors, vim stores copied text in its own clipboard. So, it's very hard for me to copy some text from a webpage and paste it into the current working file. It so happens I have to either open gedit or type it manually.
我可以在系统剪贴板中粘贴vim吗?
Can I make vim paste from and to the system's clipboard?
推荐答案
" *
和" +
寄存器用于系统剪贴板(:help寄存器
)。根据您的系统,他们可能会做不同的事情。例如,在OSX或Windows等不使用X11的系统上, ** $code>寄存器用于读取和写入系统剪贴板。在X11系统上,两个寄存器都可以使用。有关更多详细信息,请参见
:help x11-selection
,但是基本上 ** $$$ c>类似于X11的
_PRIMARY _
选择(通常使用鼠标复制您选择的内容并用鼠标中键粘贴),并且 ++ $$$ c与X11类似
_CLIPBOARD _
选择(这是剪贴板上适当的选项)。
The "*
and "+
registers are for the system's clipboard (:help registers
). Depending on your system, they may do different things. For instance, on systems that don't use X11 like OSX or Windows, the "*
register is used to read and write to the system clipboard. On X11 systems both registers can be used. See :help x11-selection
for more details, but basically the "*
is analogous to X11's _PRIMARY_
selection (which usually copies things you select with the mouse and pastes with the middle mouse button) and "+
is analogous to X11's _CLIPBOARD_
selection (which is the clipboard proper).
如果所有内容都超出您的承受范围,请尝试使用 * yy
或 + yy
将一行复制到系统剪贴板中。假设您具有适当的编译选项,则一个或另一个都应该起作用。
If all that went over your head, try using "*yy
or "+yy
to copy a line to your system's clipboard. Assuming you have the appropriate compile options, one or the other should work.
您可能希望将其重新映射为更方便的方式。例如,您可以将
vnoremap< C-c> " * y
在您的〜/ .vimrc
中,以便您可以直观地选择并按 Ctrl + c 即可拉到系统剪贴板。
You might like to remap this to something more convenient for you. For example, you could put
vnoremap <C-c> "*y
in your~/.vimrc
so that you can visually select and press Ctrl+c to yank to your system's clipboard.
请注意,如果<$,则无法从系统剪贴板复制/粘贴c $ c>:echo has('clipboard')返回0 。在这种情况下,vim不会使用 + clipboard
功能进行编译,因此您必须安装其他版本或重新编译它。某些Linux发行版默认提供最小的vim安装,但如果您安装 vim-gtk
或 vim-gtk3
包,您仍然可以获得额外的功能。
Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard')
returns 0. In this case, vim is not compiled with the +clipboard
feature and you'll have to install a different version or recompile it. Some linux distros supply a minimal vim installation by default, but if you install the vim-gtk
or vim-gtk3
package you can get the extra features nonetheless.
您可能还想看看'剪贴板'$ c $
:help cb
中所述的c>选项。在这种情况下,您可以:set剪贴板=未命名
或:set剪贴板= unnamedplus
自动进行所有取消/删除操作复制到系统剪贴板。在某些情况下,如果您要在剪贴板中存储其他内容,可能会带来不便。
You also may want to have a look at the 'clipboard'
option described in :help cb
. In this case you can :set clipboard=unnamed
or :set clipboard=unnamedplus
to make all yanking/deleting operations automatically copy to the system clipboard. This could be an inconvenience in some cases where you are storing something else in the clipboard as it will override it.
要粘贴,您可以使用> + p
或 * p
(同样,取决于您的系统和/或所需的选择),也可以将它们映射到其他内容。我明确输入了它们,但我经常发现自己处于插入模式。如果您处于插入模式,则仍可以使用< C-r>< C-p> *
或 < C-r>< C-p> +
。参见:help i_CTRL-R_CTRL-P
。
To paste you can use "+p
or "*p
(again, depending on your system and/or desired selection) or you can map these to something else. I type them explicitly, but I often find myself in insert mode. If you're in insert mode you can still paste them with proper indentation by using <C-r><C-p>*
or <C-r><C-p>+
. See :help i_CTRL-R_CTRL-P
.
还值得一提的是vim的 paste
选项(:帮助粘贴
)。这会将vim置于特殊的粘贴模式 中,该功能会禁用其他几个选项,从而使您可以使用终端仿真器或多路复用器熟悉的粘贴快捷方式轻松粘贴到vim中。 (只需键入:set paste
启用它,粘贴您的内容,然后键入:set nopaste
禁用它。)另外,您可以使用 pastetoggle
选项设置用于切换模式的键码(:help pastetoggle
)。
It's also worth mentioning vim's paste
option (:help paste
). This puts vim into a special "paste mode" that disables several other options, allowing you to easily paste into vim using your terminal emulator's or multiplexer's familiar paste shortcut. (Simply type :set paste
to enable it, paste your content and then type :set nopaste
to disable it.) Alternatively, you can use the pastetoggle
option to set a keycode that toggles the mode (:help pastetoggle
).
我建议使用寄存器而不是这些选项,但是如果它们仍然太吓人,这可以是在完善vim印章时的便捷解决方法。
I recommend using registers instead of these options, but if they are still too scary, this can be a convenient workaround while you're perfecting your vim chops.
有关更多详细信息,请参见:帮助剪贴板
。
See :help clipboard
for more detailed information.
这篇关于如何使vim从系统剪贴板粘贴(并复制到系统)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!