1

I want to copy contents of the recently used " register automatically into my system clipboard after a yank command.

The way I achieve this now is by nnoremap <C-@> :call system("wl-copy", @")<CR>.

wl-copy is a Command-line copy/paste utilities for Wayland and it will copy the input you give it to system clipboard.

What my mapping above achieves is

  1. take the contents of the " register,

    • denoted by the @" argument
  2. and pass contents of @" as an argument to the system wl-copy function

    • shown by :call system("wl-copy", @").

But it requires me to press Ctrl + @ when I want to populate my system clipboard with contents of the " register.

How can I automatically execute :call system("wl-copy", @") after a yank command successfully finishes (I'd like to preserve y{motion} but not have to continuously poll to see if the " register changed.

Other solutions that achieve this effect are welcome.

  • 2
    Check the TextYankPost autocommand – Christian Brabandt Apr 23 at 5:41
  • @MartinTournoij yeah, however this question is very old and asked at a time, when we not had the TextYankedPost autocommand. – Christian Brabandt Apr 23 at 10:48
  • Yeah, that's why I didn't mark it as a duplicate @ChristianBrabandt; maybe we should mark the other one a duplicate of this? – Martin Tournoij Apr 23 at 10:53
  • @MartinTournoij that (or simply point to a newer ansewr here as comment). Don't know how to mark that as a dupclicate for another stackexchange site – Christian Brabandt Apr 23 at 10:57
1

You can use a TextYankPost autocmd to trigger the copy to system clipboard whenever text is yanked into a register.

A very simple version of that is:

augroup wayland_clipboard
  au!
  au TextYankPost * call system("wl-copy", @")
augroup END

This will trigger the operation whenever a yank into any register happens, which is a bit wasteful but probably not too bad... You can refine it by inspecting which register was yanked into from the v:event special variable.

See :help TextYankPost and :help v:event for more details.

| improve this answer | |
  • Can you explain the need for the first au! in the augroup? – brother-bilo Apr 23 at 18:39
  • 1
    @brother-bilo It removes all autocmd's in that augroup. See :help autocmd-remove. That's so that if you source your vimrc or this script again, it won't install repeated autocmd's with the same action. That's a very common pattern when using autocmd's. See also :help :augroup which mentions this exact pattern. – filbranden Apr 23 at 19:01

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.