How to copy to clipboard in Vim?

Is it possible to copy to clipboard directly from Vim? yy only copies stuff to Vim's internal buffer. I want to copy to the OS's clipboard. Is there any such command in Vim or you can only yank stuff within Vim?


The * register will do this. In Windows, + and * are equivalent. In unix there is a subtle difference between + and * :

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). http://vim.wikia.com/wiki/Accessing_the_system_clipboard

* is probably what you want most of the time, so I use * because it functions as I expect it to in both environments.

In Linux distros, for some reason, you have to install vim-gtk first to gain clipboard functionality.

And for those confused about how to use registers when yanking or putting, you merely write " then the name of the register. So for copying something to the clipboard register you type "*y and then to put you type "*p


On Mac OSX

  • copy selected part: visually select text(type v or V in normal mode) and type :w !pbcopy

  • copy the whole file :%w !pbcopy

  • paste from the clipboard :r !pbpaste

  • On most Linux Distros, you can substitute:

  • pbcopy above with xclip -i -sel c or xsel -i -b
  • pbpaste using xclip -o -sel -c or xsel -o -b
    -- Note: In case neither of these tools ( xsel and xclip ) are preinstalled on your distro, you can probably find them in the repos

  • In your vimrc file you can specify to automatically use the system clipboard for copy and paste.

    On Windows set:

    set clipboard=unnamed
    

    On Linux set (vim 7.3.74+):

    set clipboard=unnamedplus
    

    NOTE: You may need to use an up to date version of Vim for these to work.

    http://vim.wikia.com/wiki/Accessing_the_system_clipboard

    链接地址: http://www.djcxy.com/p/8734.html

    上一篇: 具有可触发初始化的C#单例模式

    下一篇: 如何在Vim中复制到剪贴板?