1

If I copy to X11 (copy) clipboard in Vim using "+y, I can only paste contents as long as Vim is open. The reason is as follows:

From Ubuntu Wiki: 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. It doesn't go and retrieve the actual data from the source program until the user requests a paste. It saves a lot of unneeded transfer of data this way, at the expense of having no way of retrieving data from a closed program that hasn't saved its clipboard somewhere else.

I.e. after exiting Vim, X11 cannot retrieve data from it anymore, since the reference is invalid.

How can I overcome this "limitation" of X11 to paste the X11 (copy) clipboard after Vim has exited, e.g. using a clipboard manager?

Ultimately, what I want is behavior similar to:

Freedesktop's ClipboardManager specification comes to the rescue. Gnome settings daemon, the component of Ubuntu that handles all copying and pasting by default, conforms by allowing applications to explicitly request to save their clipboard contents in a safe place. Applications conform by requesting a save before they exit. Everything gets squared away before a quit and we don't lose any data.

clipmenu was recommended, but it's not available as a Debian package. Futhermore,autocutsel didn't persisted the clipboard on exit using autocutsel -selection PRIMARY -fork or autocutsel -selection CLIPBOARD -fork.

3
  • The first link in my answer on your question at Vim SE lists a handful of clipboard managers. You don't want to synchronize PRIMARY, CLIPBOARD and cut buffer selections so Autocutsel is not what you're after. You can use pretty much any other, such as CopyQ, Qlipper, Diodon, Xclipboard, to mention GUI ones. Just leave then running and you're done.
    – Quasímodo
    Oct 18, 2021 at 17:14
  • @Quasímodo Thanks. I ended up using clipmenu compiled locally, and it works perfectly. Seems it (clipmenud) monitors both the PRIMARY and CLIPBOARD selections for changes, saves them to files, afterwards it’s possible to overwrite both selections with a change. Can’t figure out how to overwrite only one selection with a change?
    – Shuzheng
    Oct 19, 2021 at 5:05
  • @Quasímodo Using clipmenu I select an earlier selection, saved by clipmenud to a file, which then becomes the current PRIMARY and CLIPBOARD selections
    – Shuzheng
    Oct 20, 2021 at 9:41

1 Answer 1

0

That page which you are quoting from, is dated 2010.

I'm using vim 8.1 and it works correctly.

Are you sure you are using a version of vim with the gui installed ? use the :version to display the enabled features. +clipboard should be displayed.

I installed vim-gtk3 to test this (vim-tiny does not have the clipboard feature enabled, and shows as -clipboard.

I'm using ubuntu 20.04.02


--- edit -- Replying to comment

Just double checked

  • vim.gtk3 - works

  • vim.tiny - fails

  • I yank a line using following sequence: "+yy

  • I paste using following sequence: "+p

:version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Sep 20 2021 11:42:42)
Included patches: 1-2269
Modified by [email protected]
Compiled by [email protected]

[2nd edit] Further clarification on the initial two tests and the subsequent 3rd test.

Initially I tried 2 tests

Test 1 In terminal 1

  1. run vim
  2. yank line with following sequnce "+yy
  3. exit vim
  4. run vim
  5. paste line with "+p

Test 2 In terminal 1

  1. run vim
  2. yank line with following sequnce "+yy
  3. exit vim
  4. In a 2nd terminal, run vim
  5. paste line with "+p

Test 3 In terminal 1

  1. run vim
  2. yank line with following sequnce "+yy
  3. exit vim
  4. run cat
  5. paste line with CTRL+Shift+v

All tests work when I use vim.gtk3


[Edit 3] Adding link to asciicast demo

I am using X11 and not wayland

echo $XDG_SESSION_TYPE
x11
4
  • I'm using vim-gtk3 too with +clipboard and specs VIM - Vi IMproved 8.1 (2018 May 18, compiled Jun 15 2019 16:41:15). Are you sure? It doesn't work for me; as soon as I close Vim, the X11 clipboard is empty
    – Shuzheng
    Oct 17, 2021 at 17:25
  • Do you paste what you yanked into the terminal after you close Vim?
    – Shuzheng
    Oct 19, 2021 at 5:01
  • Maybe I've slightly mis-understood you. 1) run vim and yank a line, 2) exit vim, 3) run new vim on new file and can paste yanked line. I'll test again. changing step 3) with cat
    – X Tian
    Oct 21, 2021 at 13:24
  • Yes, you should paste what you yanked to ”+ in the terminal. Vim preserves registers across sessions, so that’s why you can paste what you yanked in the previous session.
    – Shuzheng
    Oct 22, 2021 at 5:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .