Vim and the x11 clipboard
En Español  

We can access the clipboard of x11 (the graphical system over which most desktop environments run, such as Gnome and KDE) to share text between Vim and other graphical applications.

x11 have two different global areas where it stores content to be shared between applications, one is known as the "cut-buffer", this is the location where the text that we copy or cut is stored, this is what most people associate with system clipboard. And the other is known as "selection", in x11, whenever we select something with our mouse, we can paste this selection in the location of the mouse cursor by pressing the middle button. Vim can access the content of both storages, get the text from this global storages, or put text on them.

We can access "cut-buffer" in Vim by using the register "+, e.g.:

  • “+y will yank the visual selection and store it in "cut-buffer".
  • “+p will paste after the cursor the contents of "cut-buffer".
  • “+dd will cut the current line and store it in "cut-buffer".
  • “+yy will yank the current line and store it in "cut-buffer".

We can access "selection" in Vim by using the register "*, e.g.:

  • “*y will yank the visual selection and store it in "selection".
  • “*p will paste after the cursor the contents of "selection".
  • “*dd will cut the current line and store it in "selection".
  • “*yy will yank the current line and store it in "selection".

Notes

If we launch Vim from xterm, Vim must have been compiled with support for the xterm clipboard. To check if the support for the xterm clipboard is enabled, use the command (if it isn't found, it isn't supported):

vim --version | grep "+xterm_clipboard"

The xterm doesn't use "cut-buffer", so when using Vim in xterm always use the "* register, the "+ register won't work.

If we store information in this two special registers, and we paste it in a different Vim buffer, whether this is another view port, a different tab, or a different window, the type of the selection will be preserved (character, line, block), but if we paste it in a different program, it will be treated as the type character, i.e., the type information is lost and only plain text is pasted.