54

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Thanos
  • 771
  • 7
  • 10
  • 4
    Read the FAQ people voting to close. "Software tools commonly used by programmers" are not off topic. – Karl Bielefeldt Jun 23 '11 at 13:24
  • 7
    @TLP It must be Linux. If I am not mistaking, process is the following: 1. You copy text. 2. Vim tells X that it has data for clipboard («tells that it has data», not «puts data into clipboard»). 3. You exit vim. 4. X now has no idea where he can get data for clipboard: clipboard is «cleared». – ZyX Jun 23 '11 at 14:06
  • So the workaround may be the following: create a daemon that when something tells X that it (something) has data for clipboard will obtain this data and in turn tell X that it (daemon) has this data. In this case when something exits, daemon will still be present and able to give this data. Search for «clipboard manager» in your repository, it should solve this issue. – ZyX Jun 23 '11 at 14:36
  • This does not happen for me on OS X. I can access the contents of the data in clipboard even after quitting vim. Are you sure you're using the `+` register to copy? – abcd Jun 23 '11 at 15:09
  • @yoda It should happen only if you use X (xorg-server, not OS X). – ZyX Jun 23 '11 at 15:17
  • @zyx: I did not confuse X with OS X. I was merely stating that it works fine on OS X. BTW, I also tested on a Linux machine using X and it is still not reproducible. – abcd Jun 23 '11 at 15:58
  • @yoda. It must be reproducible with `*` register and it started to work this way for `+` register after some update (if I am not mistaking, it was X update). – ZyX Jun 23 '11 at 21:34
  • Its happening for me in ubuntu. In vim version I can see +xterm_clipboard. Yes I am using "+y for copying. – Thanos Jun 24 '11 at 09:39
  • Related: https://superuser.com/q/299419/442991 – Mateusz Piotrowski Jun 03 '18 at 16:15

7 Answers7

49

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

autocmd VimLeave * call system("xsel -ib", getreg('+'))
Community
  • 1
  • 1
Matt
  • 2,139
  • 1
  • 16
  • 20
  • 3
    It does not do exactly what TS wants: it saves value of default register to clipboard, not clipboard register. You should have used `getreg('+')`. – ZyX Feb 22 '12 at 14:07
  • 4
    Thanks for the work around Matt and ZyX. It requires installing 'xsel' though. I used getreg('+'). – Thanos Mar 13 '12 at 06:59
  • 1
    @Mike Have you found the solution for gvim? I've faced with the same problem. – DmitMedv Oct 04 '15 at 12:01
  • @dmitmedv No I still haven't. I have no idea why this works for vim but doesn't work for gvim. – MichalH Oct 06 '15 at 16:45
  • On Ubuntu 18.04, this sometimes causes vim to crash (seg fault) or hang on exit – Rufus Apr 11 '19 at 02:42
  • 1
    I just want to add few lines from my experience. Remember that if you used this copy paste using `sudo`, you can use only in applications with sudo access. It took me an hour to realize why I was not able to paste in my terminal after copying something which I had opened as `sudo`. – Dexter Jul 17 '19 at 14:33
  • @DmitMedv see [clipboard persistence bug](https://wiki.ubuntu.com/ClipboardPersistence). "The problem happens because Xorg takes a conservative approach to copying. It copies only a reference to the original data when the user performs a select or copy." – jchook Oct 29 '19 at 15:09
  • If using a Wayland compositor, one can use `autocmd VimLeavePre * call system("wl-copy -o", getreg('+'))` In my case this also does not clear the clipboard in case `getreg('+')` returns nothing. – falstaff Nov 25 '19 at 09:23
10

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

Restart your computer or run it manually.

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

Soli
  • 484
  • 5
  • 8
  • 2
    I don't understand why people would vote this answer down. It shows that the problem isn't just because of the way Vim behaves - it affects many applications. The link shows how to fix the issue for all affected applications. What more could you want? – Jonathan Hartley Feb 15 '16 at 14:24
  • Some people, such as myself, don't actually want a full clipboard manager because we want the clipboard to go away as soon as it is no longer needed (e.g. because it contains sensitive data). Fortunately, Parcellite offers a daemon mode in which it will "just" keep the clipboard safe. – Kevin May 19 '16 at 21:21
  • I found that parcelite seems to mess with clipboard, overall not reliable enough. – Leo Ufimtsev Sep 13 '17 at 21:15
9

Based on Matt's answer, the following uses xclip instead of xsel:

autocmd VimLeave * call system('echo ' . shellescape(getreg('+')) . 
            \ ' | xclip -selection clipboard')
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • This mostly works for me, but every copied line includes a trailing backslash when pasted back. Do others have this issue / does anyone know a solution? – evanrmurphy Oct 18 '18 at 02:59
  • 1
    Make that `echo -n` and it won't add a newline to your clipboard ;) – DarkWiiPlayer Dec 06 '18 at 12:59
  • 3
    Oh, just saw this answer after editing the other one. Still, `xclip` solution is probably less error prone using this: `system("xclip -selection clipboard -i", getreg('+'))` – Charlie Gorichanaz Jan 20 '19 at 01:43
4

I ran into this issue and a related problem where suspending vim with ctrl-z would also clear the clipboard. I've extended Matt's solution to fix both:

set clipboard=unnamedplus

if executable("xsel")

  function! PreserveClipboard()
    call system("xsel -ib", getreg('+'))
  endfunction

  function! PreserveClipboadAndSuspend()
    call PreserveClipboard()
    suspend
  endfunction

  autocmd VimLeave * call PreserveClipboard()
  nnoremap <silent> <c-z> :call PreserveClipboadAndSuspend()<cr>
  vnoremap <silent> <c-z> :<c-u>call PreserveClipboadAndSuspend()<cr>

endif

The if executable("xsel") guard is there to avoid errors if xsel is not installed. The nnoremap mapping preserves the clipboard when suspending from normal mode and the vnoremap mapping handles suspending from visual or select modes.

I've confirmed this works on vim 7.4 and 8.0.

Robbie Clarken
  • 181
  • 2
  • 7
  • Hi thank for your post! It works well. But if I use `xclip` like `call system('echo ' . shellescape(getreg('+')) . \ ' | xclip -selection clipboard')`. When I use ctrl+shift+v will freeze my terminal for a minutes after use suspend vim. – roachsinai May 13 '20 at 04:33
3

Use NeoVim. It by default doesn't clear the clipboard on exit. You will still need to set clipboard=unnamedplus (typically in ~/.config/nvim/init.vim) and have xsel or xclip tools installed.

Keep in mind that some other defaults are different as well.

mik13ST
  • 95
  • 7
  • Thank you! Adding the `clipboard=unnamedplus` did the trick for me. It even fixed the issue of the clipboard not carrying across tabs. – Misguided Chunk Feb 08 '23 at 22:40
1

Based on Matt's answer

When using his method copying multiple lines added slashes to the end of lines when pasting.

This should remedy that.

autocmd VimLeave * exe ":!echo " . shellescape(getreg('+')) . " | xclip -selection clipboard"

When i used "shellescape" with "system" newlines kept getting escaped. But that didn't happen when i used exe.

Don't know the real reason. but this worked.

Adithya Sama
  • 345
  • 3
  • 16
0

Please correct me if I'm wrong but from my understandings of Vim...
1) Vim uses registers instead of the clipboard to store copied/cut data.
2) These registers are preserved when exiting vim in a status file but are not accessible outside of the running process unless you manually open the file and inspect its contents
3) Saving stuff to the + registre while Vim runs allows you to paste to other applications.
4) By suspending vim (CTRL-Z) instead of closing it, these registers are still accessible.

Does that provide assistance?

Grambot
  • 4,370
  • 5
  • 28
  • 43
  • 1
    I am able to paste to other window/terminal when vim is still running. Suspending vim with ctrl+z is clearing the copied text like exit. – Thanos Jul 11 '11 at 09:33
  • Again, my experience is very limited as I usually work with vi through a PuTTY terminal. The gist of what I was able to learn is that the program uses local registers while running to store data to the clipboard, once the application is terminated the registers are cleared and data is unaccessable save the cache files it writes on close. If this effects the suspended state as well I can't suggest anything else. Good luck :D – Grambot Jul 11 '11 at 14:54