Tuesday, January 10, 2012

Copy and paste from the system clipboard with vim

Often, when using vim, highlighting text in the terminal to copy-and-paste it around is plausible.  This is definitely true when on a true terminal.  I use the rnu option so that I have relative line numbers on each line.  So copying multiple lines with the mouse grabs the line number with unwanted indentation.  When I need to move code around it is annoying to have to manually remove the numbers.

To understand how to get vim's clipboard to match your system's you need to understand vim registers.  On a computer we usually only get one clipboard.  Every time we ctrl-c, the contents of the clipboard are discarded and replaced with whatever is highlighted.  We don't have any option to copy multiple objects and then paste them around.  However vim has multiple "registers" where text can be copied and pasted from.  To see the registers type :reg in command mode.

The register that we are interested in is register +.  To test it out, copy some text from another application then run :reg and see the contents displayed in register +.

So now we just need to know how to access the contents of register +.  Register access is done with ".  To paste from they keyboard we type "+p  To copy, for example an entire line, into the system clipboard we type "+yy  To delete the current line and store it in the system clipboard we type "+dd

We can also use registers with visual mode.  If we wanted to copy the line underneath the cursor we would type V"+y  We could copy the next three lines into the system clipboard by typing Vjj"+y

To sum it up, to use the system clipboard to copy and paste in vim simply do what you would usually do in vim, then prepend "+ to your y (yank) or p (paste) command.

Update:
Depending on the version of vim you are using, the register for the clipboard may work differently. In some cases the * and + registers are the same register. If you want to find out which register is for your system clipboard, simply copy some text from anywhere into your clipboard, then run :reg. Whichever register has the text you copied is the register that holds the contents of your clipboard.

3 comments:

  1. I find this option very useful for using the system clipboard:
    set guioptions+=a
    This automatically puts the current Vim selection in the system register. No more RSI from typing in all those register commands!

    ReplyDelete
  2. It seems that now vim uses * as the register for system clipboard, instead of +. I checked on MacVim on Mac and gvim on Ubuntu 12.04. You have to check it out on your system what register is used by vim on your OS.

    ReplyDelete
    Replies
    1. I am using vim 7.3 on Ubuntu 12.04 and the = register is still the system clipboard, and the * register is completely separate. A mac-using coworker of mine tried it on mac vim 7.3 and his = and * registers are the same, so either works.
      I have update the article to reflect the possibility of the * register and how to determine the appropriate register to use.

      Delete