Contribute 

In line copy and paste to system clipboard

1,616PAGES ON
THIS WIKI

 Edit  Talk0

 
Tip 1511 Printable Monobook Previous Next

created 2007 · complexity basic · author Robert Iannucci · version 7.0


This tip applies to those Mac, Linux, and Windows users who are unable to access the System clipboard using the builtin copy and paste mechanism that Vim provides. Note that most Vim users can use the built-in methods of Accessing the system clipboard with no trouble. If you have problems, first make sure your Vim is compiled with clipboard support. If you still have problems, or cannot get a Vim compiled with clipboard support for some reason, this tip can help provide a workaround.

This has been covered in other places, but it can be annoying to only be able to copy and paste line-wise. It can be particularly annoying when you just want to yank a single word to do a web search or similar. Luckily, Vim can use external utilities on many systems to access the clipboard with a system call:

" On OSX
vmap <C-c> y:call system("pbcopy", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("pbpaste"))<CR>p
" On ubuntu (running Vim in gnome-terminal)
" The reason for the double-command on <C-c> is due to some weirdness with the X clipboard system.
vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p

But the double command problem seems to be caused by the xclip utility. That bug is present in xclip 0.08-7. But xclip 0.11 downloaded from sourceforge works fine and:

vmap <C-c> y: call system("xclip -i -selection clipboard", getreg("\""))<CR>

is sufficient.

" groovyness in Insert mode (lets you paste and keep on typing)
" This blows away i_CTRL-V though (see :help i_CTRL-V)
imap <C-v> <Esc><C-v>a

"+ and "* are supposed to 'do the right thing', but there isn't any such integration with OS X, and I have simply had complete failure with ubuntu (even with +xterm_clipboard +clipboard +X11).

Though, it has been reported to work fine on Ubuntu 8.04 with default packages (vim 7.1-138).

One other thing to note: mapping <C-v> blows away the whole visual block mode, but I never use it. A possible alternative mapping would be to map these with something like ':vnoremap y' so that it's automatic in visual mode.

This same problem has been observed not just on OS X, but also on Linux machines. Fortunately, if the "+ and "* buffers don't work, a call can be made to the xclip utility. The following maps 'ctrl+c' to copy and 'insert' to paste (since ctrl+v is used for visual block mode in vim)

vmap <C-c> :<Esc>`>a<CR><Esc>mx`<i<CR><Esc>my'xk$v'y!xclip -selection c<CR>u
map <Insert> :set paste<CR>i<CR><CR><Esc>k:.!xclip -o<CR>JxkJx:set nopaste<CR>

The copy shortcut uses marks so that rather than having the entire line copied, only the text selected in visual mode is copied. Finally, an undo at the end restores the text that would otherwise have been deleted. The paste shortcut switches to paste mode so that certain options like indenting are disabled, then switches back after pasting the text. Note the <CR> and J commands so that text can be pasted in the middle of the line.

On Windows machines that have Cygwin support, /dev/clipboard can be used in place of xclip to gain access to the clipboard, should users find the "+ and "* registers don't work.

CommentsEdit

 TO DO 

  • Explain the point of this tip. Why is it necessary to avoid using the copy/paste mechanisms provided by Vim?
  • Is this tip only related to Mac OS X or the Ubuntu Linux? If so, state that at the top.
  • Is there some problem with Vim (what version?) on Mac OS X? Exactly what? Has the appropriate mailing list been informed?
  • Do other users experience this problem? If not, should the tip be flagged for deletion?
  • VimTip1199 claims to be related to character-wise pasting. Is it helpful?

To address the first question, the point of mapping <C-c> is to be able to copy large blocks of text from vim into another application. I use this to copy from vim into an rdesktop session, for example.

But my point in the above "todo" is that the tip needs to read coherently for people who might not be familiar with the particular problem that the tip addresses. For example, Vim is run by a lot of Windows users, where the idea of having trouble copying between apps is totally unknown. The text in the tip only makes sense to people who are used to having the problem.
Also, you might use <C-c> to copy large blocks of text, but the author of the tip explicitly talks about yanking a single word. The text should make sense: Is the tip only for short snippets? Do all Mac OS X users need this tip to copy between apps? What about Ubuntu users?
Vim has "+y and "+p. The tip needs to explain when that is not adequate.
If anyone has some suggestions, please edit the tip, or add suggested text below. In time, suggestions could be enhanced and merged into the tip. --JohnBeckett 03:34, 11 November 2008 (UTC)
It is not that one wishes to avoid the builtin vim shortcuts. Rather, the "+ and "* are sometimes not integrated with the system clipboard, and in those cases there needs to be some workaround so copy and paste is enabled between Vim and external applications. This problem has been observed on Windows machines as well as Linux (CentOS), and the problem seems to be there on Mac OS X as well. --Preceding unsigned comment added by Radtad82 19:42, May 19, 2011
The very page referenced by the dodgy section notes that the builtin mechanism doesn't work with OS X... --Preceding unsigned comment added by 68.231.48.165 04:33, August 6, 2010
On a system where you see a problem, please run Vim and enter :version and note whether it shows +clipboard (support for clipboard has been compiled into Vim), or -clipboard (clipboard notsupported). See :help +clipboard.
The "builtin mechanism doesn't work with OS X" is an old comment, and it probably refers to an obsolete tip. I don't use a Mac, but I asked Björn Winckler (who maintains MacVim), and two years ago he said that the clipboard works with Vim on OS X ("the pasteboard is fully supported in both GUI and terminal mode ... The '+' and '*' registers are one and the same and by default you can use <D-x>/<D-c>/<D-v> to cut/copy/paste just like in any other Mac app"). I believe that any Vim in the last few years that has been compiled to support the clipboard, will support it (although Ctrl-C needs a mapping; I hope to cleanup these copy/paste tips and explain how to map Ctrl-C etc without requiring other Notepad-like features). I suspect this tip is a workaround for someone who is not using the correct version of Vim. JohnBeckett 10:53, May 20, 2011 (UTC)

Copied from talk page which will be deletedEdit

This page is very useful for Linux. As far as I know, this is only an issue in linux, and possibly Mac OS X.

Copying out of terminal emulators in Linux is sometimes problematic. For example, I use a perl script to send text (on highlight) via the 'xsel' program to the PRIMARY clipboard in rxvt-unicode. This perl script, however, does not function in other applications, case in point, vim, when vim is running in my urxvt virtual terminal.

The solution suggested here allows me (and others) to select a block (or single word of text) and transfer it to another application, vim in a another terminal, web browser, anything. Vim's built in features only work for the same terminal window in which vim is already running. --mtkoan 23:16, April 9, 2010 (UTC)

In line copy and paste to system clipboard的更多相关文章

  1. 【Android】Android之Copy and Paste

    Android为复制粘贴提供了一个强大的基于剪切板的框架,它支持简单和复杂的数据类型,包括纯文本,复杂的数据结构,二进制流,甚至app资源文件.简单的文本数据直接存储在剪切板中,而复杂的数据则存储的是 ...

  2. 解决autolt上传图片报错cannot open system clipboard

    今天调试代码,发现本地可以上传图片,但是集成环境无法上传报错cannot open system clipboard: 百度查了下,我的系统没有剪切板程序,才报错. 验证方法如下: win+r,输入c ...

  3. 我的Android进阶之旅------>解决如下错误failed to copy 'Settings2.apk' to '/system/app//Settings2.apk': Read-only

    push apk的时候报错 ouyangpeng@oyp-ubuntu:~/apk升级$ adb push Settings2.apk /system/app/ failed to copy 'Set ...

  4. [tmux] Copy and paste text from a tmux session

    One non-obvious, but extremely useful, feature in tmux is copy-pasting text between panes. This also ...

  5. Flex copy and paste

    <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx ...

  6. How to Copy and Paste in the Ubuntu Gnome Terminal

    How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...

  7. COPY ORCHARD GET 404: System.UnauthorizedAccessException: mappings.bin的访问被拒绝

    COPY ORCHARD 得到 404 错误,结果翻看Logs,得到的错误是: 014-07-31 17:36:46,217 [16] Orchard.Environment.DefaultOrcha ...

  8. VMware Workstation 11, 客户机Ubuntu14.04.1 LTS 64bit,宿主机Windows 8.1 64bit,剪贴板共享(copy and paste)失效问题

    Ubuntu14.04是从12.04升级上来的,因为GUI性能的原因相继装了Xubunbu和Lubuntu的包(Lubuntu的桌面果然轻量级,但是请神容易送神难,卸载Xubuntu很麻烦,就先放下了 ...

  9. Windows下adb push 总是提示Failed to copy "XX.apk" to 'system/app':Read-only file system

    一般情况看到这种提示我们会想到需要root权限,然后敲上adb remount,但是当我们执行过adb remount后,提示成功,但执行push命令依旧无法完成push. 那么此时我们的做法应该是重 ...

随机推荐

  1. 任务28:RequestDelegate管道实现思路

    任务28:RequestDelegate管道实现思路 管道的实现机制 RequestDelegate是管道的核心.ApplicationBuilder就是接收了很多个RequestDelegae把它拼 ...

  2. emacs配置文件的基础知识 (转载)

    转自:http://blog.csdn.net/schumyxp/article/details/2278268 emacs的配置文件,叫作.emacs,是个隐藏文件,存在于当前用户的根目录下面,也就 ...

  3. 【Codeforces自我陶醉水题篇~】(差17C code....)

    Codeforces17A 题意: 有一种素数会等于两个相邻的素数相加 如果在2~n的范围内有至少k个这样的素数,就YES,否则就NO; 思路: 采用直接打表,后面判断一下就好了.那个预处理素数表还是 ...

  4. poj 1733 Parity game【hash+带权并查集】

    hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...

  5. IT兄弟连 JavaWeb教程 使用Java同步机制对多线程同步

    对于前面AdderServlet案例,它的sum实例变量用来累计客户端请求进行加法运算的和.sum变量的初始为100,如果第一个客户请求加上100,那么sum变量变为200,接着第二个客户请求加上20 ...

  6. 第十篇 .NET高级技术之委托

    委托是一种可以指向方法的数据类型,可以声明委托类型变量. 声明委托的方式:delegate返回值类型   委托类型名(参数) 比如delegate void MyDel(int n) 注意这里的除了前 ...

  7. codeforces 615 D. Multipliers (数论 + 小费马定理 + 素数)

    题目链接: codeforces 615 D. Multipliers 题目描述: 给出n个素数,这n个素数的乘积等于s,问p的所有因子相乘等于多少? 解题思路: 需要求出每一个素数的贡献值,设定在这 ...

  8. 模拟 HDOJ 5095 Linearization of the kernel functions in SVM

    题目传送门 /* 题意:表达式转换 模拟:题目不难,也好理解题意,就是有坑!具体的看测试样例... */ #include <cstdio> #include <algorithm& ...

  9. 维骨力Glucosamine的最关键的几点...

    1.每日劑量應為多少?長期服用安全嗎? 由於葡萄糖胺(Glucosamine)和軟骨素(Chondroitin)原來就存在於人體,是人體每天會生產製造的必需營養素,因此,一般認為服用此類產品的安全性相 ...

  10. 基于webmagic的爬虫小应用

    以前没有写过爬虫程序,最近两天就研究了一下java的爬虫框架webmagic.然后写了一个demo 写爬虫的基本思想: 1.抓取目标连接 2.根据页面中标签,抓捕你需要的内容 3.保存结果集 以下是实 ...