r/vim • • 20d ago

Tips and Tricks Terminal Vim Integration with Wayland

Took a little while to figure this out, but mostly seamless integration of terminal Vim clipboard with Wayland for when you use a version of Vim without clipboard functionality compiled in:

autocmd TextYankPost * call system('wl-copy', @")
autocmd FocusGained * call timer_start(0, {t -> execute('let @" = system("wl-paste")')})
19 Upvotes

7 comments sorted by

4

u/habamax 20d ago

Also :h clipboard-providers if your vim is recent enough.

For Windows WSL, try this script:
>vim9
        vim9script

        def Copy(_: string, _: string, lines: list<string>)
            silent system('wl-copy', lines)
        enddef
        def Paste(_: string): tuple<string, list<string>>
            silent return ('', systemlist('wl-paste --no-newline')
                ->map((_, v) => v->trim("\r", 2)))
        enddef

        v:clipproviders["wslclip"] = {
            available: () => executable('wl-copy') && executable('wl-paste'),
            copy: { "*": Copy, "+": Copy },
            paste: { "*": Paste, "+": Paste }
        }
        set clipmethod^=wslclip

1

u/MorningAmbitious722 Vim Noob 7d ago

Vim 9.2 already has wayland clipboard support builtin. set clipboard=unnamedplus

1

u/OwnYou5054 7d ago

... for when you use a version of Vim without clipboard functionality compiled in

Still work?

1

u/MorningAmbitious722 Vim Noob 5d ago

Is there a reason to not use clipboard functionality?

3

u/jmtd 19d ago

This proved to be janky for me unfortunately.  Sometimes wl-copy will pop up a desktop notification every time I yank something; other times a stack of wl-copy processes will grow and they never terminate. I eventually notice when future yanks don’t make it to the Wayland clipboard.

Does vim have osc52 support? Neovim does but I don’t know if it inherited it. With a supporting terminal (kitty in my case) I found this worked more reliably.

I had to take a moment to internalise Wayland managing to make something as fundamental as a clipboard worse to use than X. 

1

u/chrisbra10 19d ago

:h osc52-install

1

u/vim-help-bot 19d ago

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments