Vim Tip #3: the X11 clipboard and other registers

dirkrob's picture

Getting Vim to work with the X11 clipboard can be a struggle, at least when you like to run Vim in a terminal. gvim, the GTK2 version of vim, always has clipboard support. But, if you like to use Vim from a terminal like I do, you will have to check for X11-clipboard support.

From the console, type:
% vim --version

If you see a "+xterm_clipboard", you are good to go. If it's "-xterm_clipboard", then you will need to look for a version of vim that was compiled with clipboard support. I have found that the vim that ships with Ubuntu has clipboard support, but the one with Redhat/CentOS does not. :(

If you are running Redhat/CentOS, you can install the vim-X11 package (if you have gvim then this is already installed). This provides the vimx command, which is a console version of vim with X11-clipboard support. I like aliasing vim to vimx by adding this line to my .bashrc/.cshrc:
.bashrc: if [ -e /usr/bin/vimx ]; then alias vim='/usr/bin/vimx'; fi
.cshrc: if (-e /usr/bin/vimx) alias vim '/usr/bin/vimx'

Now on to registers in general in vim. Sometimes it is nice to have more than just the default yank/put buffer, so vim offers 62 general purpose buffers named a-z, A-Z and 0-9. To tell vim to use one of these buffers, hit the quote key ", then a register name, and finally a delete/yank/put command.

The clipboard corresponds to a special register named +. So we have:
"+2yy - copy two lines to X11 clipboard
"+dd - cut line to X11 clipboard
"+p - paste X11 clipboard

Happy vimming!