Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Vim from clearing the clipboard on exit

Tags:

vim

clipboard

When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.

How to keep the copied text in the clipboard?

like image 409
Thanos Avatar asked Jun 23 '11 11:06

Thanos


People also ask

How do I save to clipboard in Vim?

In vim command mode press v , this will switch you to VISUAL mode. Move the cursor around to select the text or lines you need to copy. Press y , this will copy the selected text to clipboard.

How do I enable clipboard support in Vim?

To work with it, essentially you have two options: Set the + register as the default: :set clipboard=unnamedplus . After this, every time you simply y or p , Vim will use the system clipboard. Yank to the system clipboard explicitly only when you need it with "+y , and paste from it with "+p .

How do I copy and paste outside of Vim?

To copy text from outside applications into Vim editor, first copy the text using the usual Ctrl-C command then go to Vim editor and type "+p in Normal Mode. I find the above commands very tedious to type every time I copy-paste from outside Vim, so I mapped the Ctrl-y to copy and the Ctrl-p to paste in Vim.

How do I clear the clipboard in Vim?

Suspending vim with ctrl+z is clearing the copied text like exit.


2 Answers

Synthesizing answers from superuser, just add the following to your .vimrc

autocmd VimLeave * call system("xsel -ib", getreg('+')) 
like image 174
Matt Avatar answered Sep 21 '22 02:09

Matt


Install Parcellite, or glipper for Gnome and klipper for KDE.

Restart your computer or run it manually.

see: https://wiki.ubuntu.com/ClipboardPersistence

like image 31
Soli Avatar answered Sep 19 '22 02:09

Soli