5

As show in the following screencast, I see that using Ctrl-Z makes most recent written register of + and * overwrite the other one.

You can see me doing the following:

  1. opening a file via vi -u NONE file to cut my vimrc out of the equation, and setting set showcmd for clarity
  2. yanking the lines star and plus in the * and + registers and showing the result via :reg
  3. hitting Ctrl-Z and going back to Vim with fgEnter
  4. verifying that both * and + are filled with plus via :reg
  5. yanking star into * and showing the result via :reg
  6. Ctrl-ZfgEnter
  7. verifying that both * and + are filled with star via :reg
  8. yanking plus into + and showing the result via :reg
  9. Ctrl-ZfgEnter
  10. verifying that both * and + are filled with plus via :reg

asciicast

Why is that the case?

3
  • Not entirely sure what's going on in the video but perhaps :h x11-cut-buffer explains it. There is some specific behavior w/r/t plus and star registers when suspending vim.
    – B Layer
    May 29 at 6:40
  • 1
    @BLayer, it looks like the answer is indeed in :h x11-cut-buffer. Sadly, I'd paraphrase it as you can't have the cake and eat it too (which is a bit less tragic now that I've had breakfast).
    – Enlico
    May 29 at 7:39
  • 1
    I have a bit more robust explanation ;) that I'm posting soon.
    – B Layer
    May 29 at 7:41
7

Note that this is only applicable to Vim on *nix systems with an X server.

Let's reduce the test to this:

  1. vim -u NONE
  2. insert a line of text
  3. yank it into either the plus or star register
  4. suspend (Ctrl-Z)
  5. restore (fg)

Result: the value that was yanked into one register is now also in the other register.

I think some inference is needed but the baseline facts are found in :h x11-selection and :h x11-cut-buffer. From the former:

X11 provides two basic types of global store, selections and cut-buffers, which differ in one important aspect: selections are "owned" by an application, and disappear when that application (e.g., Vim) exits, thus losing the data, whereas cut-buffers, are stored within the X-server itself and remain until written over or the X-server exits (e.g., upon logging out).

I think "exits" here should be "exits or is suspended". Why? Because if you change the last two steps of the above test to...

  1. exit vim
  2. vim -u NONE

The result will be exactly the same. Both registers will have the same value. So some mystery action is happening either way. What is it? From :h x11-cut-buffer:

Whenever Vim is about to become unavailable (either via exiting or becoming suspended), and thus unable to respond to another application's selection request, it writes the contents of any owned selection to CUT_BUFFER0. If the "+ CLIPBOARD selection is owned by Vim, then this is written in preference, otherwise if the "* PRIMARY selection is owned by Vim, then that is written.

We can infer that whatever value was yanked last, regardless of which register it was stored in, is the value that gets stored in the cut buffer before Vim is suspended or exited.

Finally, when starting or resuming from standby, Vim loads the plus and star registers with the contents of the clipboard/cut-buffer. No inference required here as it is easy to demonstrate: copy some text (from anywhere), start Vim as before, :reg * + and there's the copied text.

8
  • 1
    Ok, +1, but ...
    – Enlico
    May 29 at 7:52
  • Sir, your challenge is accepted! Don't wait around for me, though. Could be a while before I get around to digging through that code. :D
    – B Layer
    May 29 at 7:55
  • No worries, nobody is chasing me. By the way, unrelated, but maybe you're interested, I'm trying to have my vimrc reviewed. I think that would be off-topic here, so I posted it on CodeReview.
    – Enlico
    May 29 at 8:07
  • 1
    @Enlico FYI I've cut the number of inferences in half (from 2 to 1) as I remembered that Vim loading star/plus from the clipboard upon resume/start is a fairly well known and easily demonstrable feature/fact.
    – B Layer
    May 30 at 5:48
  • 1
    Oh, and I'll give your vimrc a scan when I get a minute.
    – B Layer
    May 30 at 5:55

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.