If I copy text into the xterm clipboard from vim, it is cleared when I exit vim.

How do I prevent this?


I recently solved this problem with this single line in .vimrc (requires utility xsel):

autocmd VimLeave * call system("echo -n $'" . escape(getreg(), "'") . "' | xsel -ib")

It stores current registry text to the clipboard buffer.

  • 2
    ..or even better to use direct input redirection with system: system("xsel -ib", getreg()) – sgtpep Feb 14 '12 at 21:04

I use xclip to do so:

autocmd VimLeave * call system("xclip -o | xclip -selection c")


It’s fairly simple:

  1. Install Parcellite or glipper for Gnome, or klipper for KDE
  2. Restart your system or run it manually

See this page for more information and options.


I don't have a good answer, and I don't have access at the moment to a system running X to experiment with, but this topic is discussed in the Vim reference manual here:

:help x11-selection

You didn't say how you copied text to the clipboard, but if you used the * register, it might help to use the + register instead.

  • I use the + register. – Clark Gaebel Jun 22 '11 at 14:38

Try selecting while holding down shift instead. This (usually) tells your terminal emulator to directly copy text instead of sending the mouse events to Vim. Exiting Vim after doing it this way should not destroy the contents of the copy buffer, and a middle-click in another application should paste the selected text.

This will not help you with gVim. However, you note you use the + register, which means you should be accessing the clipboard, which is not destroyed on exit of the application that last "wrote" to it. This register is not pasted using middle-click with the mouse. Either use control-v (for applications that support pasting from the clipboard with that hot key) or right-click and select paste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.