3

If I yank some text in the clipboard selection from Vim (e.g. "+yiw), then quit Vim, I can't paste it in st.

Here's a MWE:

$ git clone https://git.suckless.org/st
$ cd st
$ make
$ ./st
$ echo foo | vim -Nu NONE -
"+yiw
:q

If I press C-S-v to paste foo, nothing is pasted on the shell's command-line. I can't paste the text in firefox, nor in xterm, but for some reason I can in urxvt.

OTOH, if I don't quit Vim, but just suspend it with :shell:

$ ./st
$ echo foo | vim -Nu NONE -
"+yiw
:shell

Then I can paste the yanked text anywhere, including in the current st terminal.


If E558 is raised when you start Vim, this may fix the issue:

$ curl -LO http://invisible-island.net/datafiles/current/terminfo.src.gz
$ gunzip terminfo.src.gz
$ tic -x terminfo.src

Here's another MWE:

$ ./st
$ vim -Nu NONE +'let @+ = "bar"' +q

Again, if I press C-S-v to paste bar, nothing is pasted on the shell's command-line.

I can reproduce with Vim (8.1.1200), but not with Nvim (NVIM v0.4.0-620-geada8f5aa).
I'm on Ubuntu 16.04, and I can also reproduce on Ubuntu 18.04 in a virtual machine.


Is it due to:

  • a bug in Vim?
  • a bug in st?
  • a missing configuration in one of them?

If it's a bug, what's the best workaround?

Currently, I'm experimenting this autocmd:

augroup fix_yank_in_clipboard_from_st
   au!
   au TextYankPost * if v:event.regname is# '+'
       \ | call system('echo ' . shellescape(@+) . ' | xclip -selection clipboard')
       \ | endif
augroup END

It requires the $ xclip shell utility. The autocmd is called whenever some text is yanked to invoke $ xclip and make it write the text in the clipboard selection.

One issue with the current code is that it adds a trailing newline at the end of the yanked text.


Edit:

The behavior of st seems normal.

I was confused by the behavior of urxvt which allows you to paste the text yanked in the + register, back into a urxvt terminal, even after quitting Vim. But it requires some custom configuration; i.e. these lines in ~/.Xresources:

URxvt.perl-ext-common: selection-to-clipboard,pasta
URxvt.keysym.Control-Shift-V: perl:pasta:paste

And this pasta perl script in ~/.urxvt/ext:

#! /usr/bin/env perl -w
# Usage: put the following lines in your .Xdefaults/.Xresources:
# URxvt.perl-ext-common           : selection-to-clipboard,pasta
# URxvt.keysym.Control-Shift-V    : perl:pasta:paste

use strict;

sub on_user_command {
  my ($self, $cmd) = @_;
  if ($cmd eq "pasta:paste") {
    $self->selection_request (urxvt::CurrentTime, 3);
  }
  ()
}

1 Answer 1

6

That is not how the clipboard works in Unix land. Basically when the clipboard content is requested, it means a client requests from the X-Server the selection of the clipboard from whoever owns it. The X Server will then relay this request to the other X client, that owns the clipboard.

Now, obviously, this does not work, if you quit the X client, that initially owns the selection (in this case Vim). So once the original client did quit, the selection is usually lost.

A work-around is, to use a so called clipboard manager that comes with the big desktop environments like klipper, or parcellite or gnomes clipboard manager. What they do is, whenever they notice that the owner of the clipboard changes, take ownership of the selection and pretend to be the actual new owner (at least, if i remember correctly).

3
  • 1
    Thank you for your answer. I was confused by this behavior because usually I don't run st but urxvt, and in urxvt when some text is yanked in the clipboard, it persists even after quitting Vim.
    – user938271
    Commented Apr 24, 2019 at 10:39
  • Ah, it persists only in urxvt, not in other applications. It must be due to some custom config in ~/.Xresources, which I had copied from here.
    – user938271
    Commented Apr 24, 2019 at 10:53
  • 1
    it looks like urxvt does kind of act like a clipboard manager itself. Commented Apr 24, 2019 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.