Vim, X11 and the clipboard (Copy, paste)
Tuesday, April 5th, 2011
A while ago I fiddled around with my Vim configuration, and I removed some things I thought weren’t necessary. A little while later I noticed that, when I copied things in a terminal Vim by selecting them with the mouse or a normal visual selection, I couldn’t paste them into other X11 programs such as the terminal, Firefox and other Vim instances. Of course, I didn’t have a backup of my old vimrc.
After much fiddling around with the various guioptions, mouse and clipboard settings, and many a Google search, I found a page which offered some insight:
For this to work, your Vim has to be compiled with the +xterm_clipboard setting. Run
vim --version | grep xterm_clipboardto see if that option is compiled in
My Vim (Xubuntu 10.10) showed -xterm_clipboard instead of +xterm_clipboard meaning it didn’t support directly cutting/copying to the X11 clipboard. It turns out that I had removed the vim-gtk package and installed the normal vim package, since I never use the GTK/Gnome version anyway.
Re-installing the vim-gtk package solved the problem:
aptitude install vim-gtk
If you want to be able to select with the mouse, you will also need to set the following in your ~/.vimrc:
set mouse=a
If you don’t have that you can still select with the mouse, but it won’t be Vim who does your selection but your terminal emulator. That results in line numbers being included in your selection (if you’ve set number) as well as lines longer than the terminal being cut off.
Update: Imran Chaudhry mails to say:
On Debian Squeeze which I’m using I have to re-run the alternatives method so that vim finds the right vim:
$ update-alternatives --config vim
Thanks, Imran!