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?
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.
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 .
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.
Suspending vim with ctrl+z is clearing the copied text like exit.
Synthesizing answers from superuser, just add the following to your .vimrc
autocmd VimLeave * call system("xsel -ib", getreg('+'))
Install Parcellite, or glipper for Gnome and klipper for KDE.
Restart your computer or run it manually.
see: https://wiki.ubuntu.com/ClipboardPersistence
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With