Sun 28 November 2010

vim : copying to the system clipboard

Posted by ankur in Tech (267 words, approximately a 2 minute read)

  • Share this post:

Copying a file off vim to paste some place is sometimes a problem. If you have line numbers enabled, you must disable them before selecting the text, plus you manually need to re-compose your lines that broke during the paste etc. I was pretty sure that there would be an easier way of doing this. Google helped, as always. Here's the solution:

VIM has the clipboard facility. To see if it's enabled or not, run :

vim --version grep -i clipboard

This is what I get:

[ankurGuest@070905042 ~]$ rpm -qa grep vim
[ankurGuest@070905042 ~]$ vim --version grep -i clipboard
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
 -xterm_clipboard -xterm_save
[ankurGuest@070905042 ~]$

As you see, it's disabled. How then does one enable it? Well, gvim in the vim-X11 is what you need to use. If you like to stick to the cli version, use gvim -v.

[ankurGuest@070905042 ~]$ rpm -ql vim-X11 grep gvim
[ankurGuest@070905042 ~]$ gvim --version grep clipboard
+clientserver +clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
+xsmp_interact +xterm_clipboard -xterm_save

Once done, it's pretty simple to use the functionality. The "+ register holds the contents of the system clipboard, and the "* register holds the contents of the X clipboard. So, if you want to copy the entire file to the clipboard, you go gg"+yG or gg"*yG. Beware, the latter works only when you're using xterm. The former works for gnome-terminal (what I use). Added reading :

or in vim, go

 :h "+
I thank the folks on the -devel list and #vim on Freenode who helped me get to this. (and google) Cheers!

  • Share this post: