(转)vim 访问系统剪贴板
原文出处:http://vim.wikia.com/wiki/Accessing_the_system_clipboard
Please review this tip:
- This tip was imported from vim.org and needs general review.
- You might clean up comments or merge similar tips.
- Add suitable categories so people can find the tip.
- Please avoid the discussion page (use the Comments section below for notes).
- If the tip contains good advice for current Vim, remove the
{{review}}
line.
Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
created 2005 · complexity basic · author JimD · version 7.4
When performing copy, cut, and paste with commands like y
, d
, and p
, by default Vim uses its own location for this, called the unnamed register (:help quotequote). Note that this is different from what most modern graphical text editors and other applications like web browsers do; these applications interact with the system clipboard when using keybindings like CTRL-C, CTRL-X, and CTRL-V. Fortunately, in most cases it is easy to get Vim to work with the system clipboard.
Vim offers the +
and *
registers to reference the system clipboard (:help quoteplus and :help quotestar). Note that on some systems, +
and *
are the same, while on others they are different. Generally on Linux, +
and *
are different: +
corresponds to the desktop clipboard (XA_SECONDARY
) that is accessed using CTRL-C, CTRL-X, and CTRL-V, while *
corresponds to the X11 primary selection (XA_PRIMARY
), which stores the mouse selection and is pasted using the middle mouse button in most applications. We can use these registers like any other register. Here are a few common commands that demonstrate this:
gg"+yG
– copy the entire buffer into+
(normal mode)"*dd
– cut the current line into*
(normal mode)"+p
– paste from+
after the cursor (works in both normal and visual modes):%y *
– copy the entire buffer into*
(this one is an ex command)
One distinction to make is that using +
and *
is different from using CTRL-SHIFT-V on the terminal (or doing a right-click and then selecting "paste" in the terminal menu), where Vim essentially inserts each character in the clipboard one-by-one. In general using +
and *
is much more reliable than using CTRL-SHIFT-V.
See also pasting registers for more general information about pasting from registers.
I searched though the tips and found only a few passing references to this feature: VimTip71 (implies it's a feature of gvim, but I find it works in console Vim just fine under Linux; I only use gvim under MS Windows). VimTip771 and VimTip964 refer to an extra utility named xclip which is only for X11 and seems to be completely unnecessary for Vim using the *
register), and VimTip960 (misses the point but the comments mention it a few times).
Using the clipboard as the default registerEdit
VimTip21 is spot on but the title suggests it's for MS Windows only. It also mentions that we can change out settings so the "anonymous" register is aliased to the *
register using:
set clipboard=unnamed
- Note: in vim 7.3.74 and higher you can set
clipboard=unnamedplus
to alias unnamed register to the+
register, which is the X Window clipboard.
VimTip432 and VimTip600 use the *
register in some native Vim code (using @*
) without explaining it in general. VimTip448 also uses it (for converting hex to decimal) but explains it in passing. VimTip478 uses it in a function for copying the results of a :g
search into the *
register. VimTip687 suggests that Mac OS X doesn't implement the * register (yet?) but suggests a workaround using the pbcopy and pbpaste utilities. VimTip876 also makes passing reference to it.
There are several tips for copying the current filename into the clipboard including: VimTip432, VimTip891, VimTip600(most of the fuss is resolving forward slashes and backslashes for MS Windows paths).
Checking for X11-clipboard support in terminalEdit
Getting Vim to work with the X11 clipboard can be a struggle if you want to run Vim in a terminal. In this case, you will have to check for X11 clipboard support. The GUI version of Vim always has clipboard support.
From the console, type:
vim --version | grep clipboard
If you see +clipboard
or +xterm_clipboard
, you are good to go. If it's -clipboard
and -xterm_clipboard
, you will need to look for a version of Vim that was compiled with clipboard support. On Debian and Ubuntu, to obtain clipboard support install the packages vim-gtk
or vim-gnome
(not vim-tiny).
If you are running Redhat/CentOS, you can install the vim-X11 package (if you have gvim then this is already installed). This provides the vimx command, which is a console version of Vim with X11-clipboard support. Or if you are running Debian (only test on 7.0), installing vim-gnome package fixes the problem. I like aliasing Vim to vimx by adding this line to my .bashrc/.cshrc:
.bashrc or .zshrc: if [ -e /usr/bin/vimx ]; then alias vim='/usr/bin/vimx'; fi
.cshrc: if (-e /usr/bin/vimx) alias vim '/usr/bin/vimx'
If you are running Gentoo, emerge vim with the 'X' USE flag. For instance, adding this line to /etc/portage/package.use:
app-editors/vim X
See alsoEdit
CommentsEdit
Under Windows, the * and + registers are equivalent. For X11 systems, though, they differ. For X11 systems, * is the selection, and + is the cut buffer (like clipboard).
Text selected, or otherwise highlighted in one X11 app is available in the selection buffer, and text explicitly copied or cut is available in the cut buffer.
All my inter-window copy & paste to gvim is handled by the <F7> key. This works for all OSes only for gvim.
"copy
vmap <F7> "+ygv"zy`>
"paste (Shift-F7 to paste after normal cursor, Ctrl-F7 to paste over visual selection)
nmap <F7> "zgP
nmap <S-F7> "zgp
imap <F7> <C-r><C-o>z
vmap <C-F7> "zp`]
cmap <F7> <C-r><C-o>z
"copy register autocmd FocusGained * let @z=@+
The + and * registers do not work running Vim under cygwin without an X session (for instance running Vim directly from a cmd or rxvt window). They also don't work under Mac OS X using Vim from the command line (via Terminal.app or iTerm.app). This should be documented (as should workarounds).
- For such systems, fakeclip plugin provides comprehensive and extensible emulation for the clipboard registers.
in response to above comment (cygwin vim sans X session), as of the software versions below, there doesn't seem to be any problems using the "* register in or outside of rxvt, in a cmd window:
WinXP (SP3)
cygwin (2.831)
rxvt (20050409-21)
vim (7.3.1314-1)
If you're having trouble with GVIM on Ubuntu, and it doesn't seem to be doing what everybody is saying it should, try closing it completely and reopening. It's supposed to "just work" in the case of copy-paste integration with the system clipboard, especially if you use GVIM's GUI buttons or menus. However, sometimes it just stops working, and you have to close and reopen.
The Ubuntu vim-gtk package contains the +xterm_clipboard option, while the standard vim package does not ...
- I used to use the xsel/xclip kludges years ago with gpm, on Debian... but I've found out here that simply installing vim-gtk3 with vim-gnome (they are dependent, and vim.gtk3 gets mapped to vim) makes using "*p a complete breeze, in old CLI vim. --October 5, 2016
If for some reason your version of vim under Unix-like system has no support for clipboard, you can use a handy utility called xsel. Xsel lets you manipulate X selections (check man xsel for more info). To work around the lack of clipboard I use following mappings:
" Copy to X CLIPBOARD
map <leader>cc :w !xsel -i -b<CR>
map <leader>cp :w !xsel -i -p<CR>
map <leader>cs :w !xsel -i -s<CR>
" Paste from X CLIPBOARD
map <leader>pp :r!xsel -p<CR>
map <leader>ps :r!xsel -s<CR>
map <leader>pb :r!xsel -b<CR>
So, to paste from X clipboard, you use :r!xsel -p|-s|-b depending which X clipboard you want to access. To copy, use :w!xsel -i -p|-s|-b
.
This was recently added, but there is no indication why it is useful or needed. I have moved it here for now because to my knowledge the normal clipboard access commands should work and are much more desireable.
--Fritzophrenic 17:30, November 8, 2010 (UTC)
I am running ubuntu 10.04 with vim-gtk. The * register works as described for gvim but not the command line. vim --version does show the " +xterm_clipboard" option. This is version 7.2.
- I had a similar issue on Ubuntu 14.04. It turns out the default clipboard setting is
"autoselect,exclude:cons\|linux"
and I was changing it withset clipboard+=unnamedplus
, which turned it into"autoselect,exclude:cons\|linux,unnamedplus"
. However, according to the help page for clipboard-exclude, the exclude option must come last. Changing my code toset clipboard^=unnamedplus
solved the problem by prepending the unnamedplus setting, rather than appending it. - BTW, I have Vim GNOME 7.4.52 from the Ubuntu repositories, and
clipboard=unnamed
uses the X clipboard, whileclipboard=unnamedplus
uses the (GNOME) System Clipboard. - --LeahCim (talk) 02:01, September 1, 2014 (UTC)
I have found using the autocutsel package on X11 in conjunction with set clipboard=unnamed provides a seamless experience.
Simple workaround for X clipboardsEdit
There is a convenient workaround to create a few commands for pasting/copying selections into/out of various X11 clipboards with `xsel`.
:command -range Cz :silent :<line1>,<line2>w !xsel -i -b
:command -range Cx :silent :<line1>,<line2>w !xsel -i -p
:command -range Cv :silent :<line1>,<line2>w !xsel -i -s
:cabbrev cv Cv
:cabbrev cz Cz
:cabbrev cx Cx :command -range Pz :silent :r !xsel -o -b
:command -range Px :silent :r !xsel -o -p
:command -range Pv :silent :r !xsel -o -s :cabbrev pz Pz
:cabbrev px Px
:cabbrev pv Pv
With this code blob dumped in your ~/.vimrc you can do the following
1. Select a blob of text in visual mode
2. Step into command line with ':'
3.
:`<,`>cz
and your selected text will be copied into one of X's 3 default clipboards.
there are 6 commands, 3 for pasting, 3 for copying, and each command represents a buffer.
z = "X11-Clipboard"
x = "X11 Primary Selection"
v = "X11 Secondary Selection"
DISPLAY environment variable on Linux systemsEdit
Make sure your DISPLAY environment variable is set appropriately - otherwise vim can not connect to your x-session to access the clipboard.
To check run:
echo $DISPLAY
which should output something like:
:0.0
Workaround using x11 minimum packages (under Linux)Edit
If nothing work and you would like to copy from vim (under xterm) to leafpad, you may use this add-on to your ~/.vimrc
Press F9, and it will copy to the x11 clipboard.
function Func2X11()
:call system('xclip -selection c', @r)
endfunction
vnoremap <F9> "ry:call Func2X11()<cr>
vnoremap <m-c> "ry:call Func2X11()<cr>
vnoremap <ESC-c> "ry:call Func2X11()<cr>
I hope that it may help you.
Workarounds for xtermEdit
If :version
shows "+xterm_clipboard
", but using the clipboard still does not work, try using other system tools as a workaround. For example, you can search for a clipboard.sh script which can be used like 'cat a.txt | clipboard
' to copy the contents of file a.txt to the system clipboard. When using kubuntu, the system clipboard is managed by 'Klipper', you can use 'Klipper' shortcut to paste anything to the system clipboard from anywhere because its shortcut has the highest precedence. --Changc11, October 10, 2013
- Has a case like this been discussed at the vim_use mailing list? If clipboard support is provided by Vim, and if the versions of Vim and the system are not excessively old, the clipboard should work. This idea is better as a comment until something more conclusive is known. JohnBeckett (talk) 10:27, October 13, 2013 (UTC)
(转)vim 访问系统剪贴板的更多相关文章
- BX2001: IE 支持使用 window.clipboardData 访问系统剪贴板,Chrome 和 Safari 中存在类似的 Clipboard 对象但尚未实现,Firefox 和 Opera 不支持这类对象
http://www.w3help.org/zh-cn/causes/BX2001 标准参考 无 问题描述 IE 支持使用 window.clipboardData 对象内的一系列方法访问系统剪贴板: ...
- VI/VIM 无法使用系统剪贴板(clipboard)?(Ubuntu&Mac OS X已解决)
在 Ubuntu/Mac OS X 中使用VI/VIM时,发现无法使用系统的剪贴板. Ubuntu 上网一查,原来是少装了几个东西. 使用如下命令,安装相关的包.安装成功后,就可以使用系统剪贴板了. ...
- VI/VIM 无法使用系统剪贴板(clipboard)
来自: http://www.bubuko.com/infodetail-469867.html vim 系统剪贴板 "+y 复制到系统剪切板 "+p 把系统粘贴板里的内容粘贴到v ...
- 【docker】 VI/VIM 无法使用系统剪贴板(clipboard)
docker 容器里边操作系统是ubuntu .默认是没有vim 的,需要自己安装一下 1 更新源 apt-get update 2 安装 vim apt-get install vim 此时.系统不 ...
- 终于掌握vim的寄存器和系统剪贴板的使用了- 要安装vim-X11包
vim的系统剪贴板 vim的 加号寄存器 "+ 是和系统剪贴板 相关联的. 加号寄存器和系统剪贴板之间的内容, 可以互相切换. 要把 加号寄存器中的内容, -> 放到/转移到系统剪贴板 ...
- vim复制内容到系统剪贴板
vim提供了y键盘操作用于复制文本,但是复制之后的文本位于当前窗口的缓冲区中,不在系统剪贴板中,这给跨程序文本拷贝代码很来很多麻烦.搜索发现,可以使用]y指令快速将选定的文本复制到系统剪贴板中. 顺便 ...
- Ubuntu12.04无法使用vim系统剪贴板解决方法
以前在 vim 下工作需要在 vim 和其它的编辑器之间复制东西,使用 Shift + Ctrl + v/c.感觉这样很不方便,今天在网上搜索了以下可以用 “+y/p,但是自己实验怎么也不行,在命令模 ...
- Vim-复制选中内容至系统剪贴板,光标移动到指定行的行首和行尾
1.全选并复制到系统剪贴板 ggVG或ggvG 然后 "+y gg 让光标移到首行,在vim才有效,vi中无效 V 是进入Visual(可视)模式 G 光标移到最后一行 "+y 复 ...
- vim与系统剪贴版的交互
1 概述 vim中的复制,删除,替换(d,r,s,x,y等)的内容都会被保存到默认的未命名的寄存器中,之后可以通过p进行粘贴,但是,这个寄存器不是系统的剪贴版,很多时候需要vim与系统剪贴版的交互,那 ...
随机推荐
- 【面试 IO】【第十一篇】 java IO
1.什么是比特(Bit),什么是字节(Byte),什么是字符(Char),它们长度是多少,各有什么区别 1>Bit最小的二进制单位 ,是计算机的操作部分 取值0或者1 2>Byte是计算机 ...
- 如何突破Windows环境限制打开“命令提示符”
如今,许多企业或组织都会通过使用受限的windows环境来减少系统表面的漏洞.系统加固的越好,那么也就意味着能被访问和使用到的功能就越少. 我最近遇到的情况是,一个已经加固的系统同时受到McAfee ...
- PriorityQueue ,ArrayList , 数组排序
static class E implements Comparable<E>{ int x ; int y ; int state ; int money ; public E(int ...
- POJ 3978(求素数)
知识点: 1.求素数的test,从2~sqrt(n): 2.假设数据非常多,能够用素数表记录,然后sum=prime[m]-prime[n]求得! ! !! !!! !! ...
- PPAPI插件的动态创建、改动、删除
一旦你完毕了PPAPI插件的开发,实际使用时可能会有下列需求: 动态创建PPAPI插件 删除PPAPI插件 改变PPAPI插件的尺寸 实现起来非常easy,从JS里直接訪问DOM(BOM)就可以.以下 ...
- 工作总结 mvc 调页面传参数 参数值会一直保存 在这个页面上的
意思是 两个页面均可以 获取到id 和 goodsType 都可以获取 id goodsType post 的 还多带点属性值 form data 页面上带过去的 (新增 编辑)
- Oracle oledb 打包并集成到程序安装包(VC ADO訪问Oracle)
近期有一个项目和oracle交互,我用的是ADO的方式进行试问操作. 首先把数据库连接的东东记录一下,老了记性不好啊! 操作例如以下: //连接串这么写的话就能够不用配置 tnsnames.ora配置 ...
- web前端面试系列 - 数据结构(两个栈模拟一个队列)
一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将 ...
- linux 块设备驱动(五)——块设备应用层的操作
一: 加载好sbull驱动 root@CarRadio:/# ls /dev/sbull -l brw-r--r-- 1 root root 254, 16 Mar 25 08:25 /dev/sbu ...
- linux 块设备驱动(四)——简单的sbull实例
#include <linux/module.h> #include <linux/moduleparam.h> #include <linux/init.h> # ...