How to copy from Vim to system clipboard using wayland and without compiled vim `+clipboard` feature flag
Asked Answered
P

6

9

I cannot copy into the + or * register.

:echo has('clipboard') from within Vim returns 0 meaning I don't have that feature flag, I don't want to recompile.

I'm running wayland so I cannot use X11 based solutions

Planoconvex answered 23/4, 2020 at 4:20 Comment(2)
Possibly related plugin: fauxClipDiminished
For neovim users: If you have wl-clipboard installed, neovim automagically uses itBarocchio
P
20

I had trouble finding resources so here is what ended up working by adding in ~/.vimrc.

nnoremap <C-@> :call system("wl-copy", @")<CR>

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

What mapping above achieves is

  1. Activate the mapping with Ctrl + @. or choose any convenient key combo
    • nnoremap <C-@>
  2. take the contents of the " register,

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

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

Alternatively

Assuming you only want to copy line sections of the file, do shift+v to go into visual mode and only highlight the lines I want to copy. Then do.

:'<,'>w !wl-copy

where

  • '<,'> - means you used visual mode to select a range (you don't type this)
  • w !{cmd} - write the range to the stdin of cmd, see more at :help w_c

You can map that with

xnoremap <silent> <C-@> :w !wl-copy<CR><CR>
  • xnoremap: mapping will work in visual mode only
  • <silent>: mapping which will not be echoed on the command line
  • <C-@>: desired key combination
  • :w !{cmd}: write the range to the stdin of cmd
  • <CR><CR>: two enters are needed otherwise command line waits for another command
Planoconvex answered 23/4, 2020 at 4:20 Comment(0)
P
3

Install the wl-clipboard package.

Then, adding this to ~/.config/nvim/init.vim (NeoVIM) works well for me:

set clipboard=unnamed

let g:clipboard = {
    \   'copy': {
    \       '+': ['wl-copy', '--trim-newline'],
    \       '*': ['wl-copy', '--trim-newline'],
    \   },
    \   'paste': {
    \       '+': ['wl-paste', '--no-newline'],
    \       '*': ['wl-paste', '--no-newline'],
    \   },
    \ }
Percheron answered 14/12, 2023 at 21:55 Comment(1)
In neovim (at least in version 0.9.5, but I suspect even in much older versions) if wl-clipboard is installed then it is automagically selected. Make sure to restart neovim after installing.Barocchio
R
2

The feature the author is asking for will be hopefully implemented in vim. See for details: https://github.com/vim/vim/issues/5157

There is also a workaround plugin: https://github.com/jasonccox/vim-wayland-clipboard

As for me, I'm using my own workaround:

" Yank into all these at once:
"     vim y/p register
"     wayland primary
"     wayland clipboard
xnoremap <silent> <leader>y y:call system("wl-copy --trim-newline", @*)<cr>:call system("wl-copy -p --trim-newline", @*)<cr>
Repulsion answered 30/7, 2021 at 23:43 Comment(0)
E
1

If you are using neovim, it automatically uses wl-copy under wayland, so after wl-copy is installed everything shall work without additional cofigs.

Elegant answered 23/4, 2020 at 4:21 Comment(0)
S
0

Still no working solution here https://github.com/vim/vim/issues/5157

Here is my workaround:

set clipboard=unnamed,unnamedplus

augroup wl-clipboard
    autocmd!
    autocmd FocusLost * :call system('wl-copy --trim-newline', @+)
    autocmd FocusGained * :let @+ = system('wl-paste -n')
augroup END
Schmuck answered 14/5, 2024 at 16:36 Comment(0)
D
0

I do prefer keeping the default mappings and make then use the wl-copy and wl-paster commands:

" Copy with Wayland
xnoremap <silent> y :w !wl-copy<CR><CR>
" Paste from Wayland
noremap <silent> p :r !wl-paste<CR><CR>
Dominoes answered 24/8, 2024 at 18:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.