In line copy and paste to system clipboard

 Contribute 

Watchlist Random page Recent changes

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)

    时间: 09-19

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

    [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 allows you to scroll back in a tmux sessoin. We'll cover how to use copy and paste in tmux using C-b [ and C-b ], for both emacs- and vim-style keybi

    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 the content. How to Paste: Use Ctrl + Shift + V to paste the content. 版权声明:本文为博主原创文章,未经博主允许不得转载.

    linux与windows共享剪贴板(clipboard)

    linux与windows共享剪贴板(clipboard)的方法 先说两句废话,其实linux和windows之间不需要共享剪贴板,直接在putty中,按住SHIFT+鼠标选择就可以了. 但是作为一种hack行为或者不用鼠标实现复制功能,就需要这种方法了. 组合: putty+tmux+vim+mobaiterm/xserver 如果熟悉的朋友应该早已经用习惯了前面三个软件,号称三剑客. 后面的两个软件只是使用其中的一部分功能,即x window的剪贴板. xserver是指windows这边的

    java调用copy复制子文件夹及文件到指定目录(非xcopy)

    因为作所以烦因为烦所以快乐...(为什么不先查一下有没有现成的命令,后悔啊!!) 不知道有xcopy这个命令就自己想写个复制子目录和文件的功能...以下故事诞生了 是这样的,昨天在学校弄的半成品,半成品弄完晚上就跟朋友打cf了...他是个会带队团灭的牛逼人物!! 然后隔天早上去学校就拿半成品测试了一下,发现直接用copy只能复制文件啊,不能直接复制子目录里的文件(目录指的是文件夹,子目录就是文件夹里面还有文件夹当然也有可能是文件).....那怎么搞???纠结了一下,就开始把半成品稍微改了一下,似

    怎样修复“Windows/System32/Config/System中文件丢失或损坏”故障

    怎样修复“Windows/System32/Config/System中文件丢失或损坏”故障 英文原文引自 http://xphelpandsupport.mvps.org/how_do_i_repair_a_missing_or_cor1.htm 此类错误通常说明计算机由于注冊表的原因不能启动,下列操作应该可以修正以上错误.然而,须要特别注意的是,你须要将5个注冊表区域所有进行替换.仅仅简单的替换一个或者两个将会潜在的导致其它的错误 注意 假设你使用的是WindowsXP的OEM版本号,那么你

    System.arraycopy和Arrays.copyOf的比较

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 用了native关键字,调用的为C++编写的底层函数,可见其为JDK中的底层函数. public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy

    C# Array类的浅复制Clone()与Copy()的差别

    1 Array.Clone方法 命名空间:System 程序集:mscorlib 语法: public Object Clone() Array的浅表副本仅复制Array的元素,不管他们是引用类型还是值类型.可是不负责这些引用所引用的对象. 新Array中的引用与原始Array的引用指向同样的对象. 例: int[] intArray1 = {1, 2}; int [] intArray2 = (int [])intArray1.Clone(); 这里须要说明的是.须要使用强制类型转换,原因在于

    System.arraycopy() or Arrays.copyOf()

    1. Simple Code Examples System.arraycopy() int[] arr = {1,2,3,4,5}; int[] copied = new int[10]; System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy System.out.println(Arrays.toString(copied)); Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1

    mac 福利 快捷键(英文版)

    官方原版:希望能用苹果电脑的同学能喜欢 OS X: Keyboard shortcuts You can use keyboard shortcuts to do things on your Mac by pressing a combination of keys on your keyboard. Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a c