1

Background

I use wayland and neovim and when pasting external text it would add ^M to the end of each line. This is a known "bug" (https://github.com/neovim/neovim/issues/10223) and there exists a workaround: Using sed to trim the ^M :

let g:clipboard = {
            \   'name': 'WL-Clipboard with ^M Trim',
            \   'copy': {
            \      '+': 'wl-copy --foreground --type text/plain',
            \      '*': 'wl-copy --foreground --type text/plain --primary',
            \    },
            \   'paste': {
            \      '+': {-> systemlist('wl-paste --no-newline | sed -e "s/\r$//"')},
            \      '*': {-> systemlist('wl-paste --no-newline --primary | sed -e "s/\r$//"')},
            \   },
            \   'cache_enabled': 1,
            \ }

Neovim defaults to wl-paste on my system, when g:clipboard is not set.

Problem

With the workaround in place, neovim does not paste linewise copies in a newline.

  • Expected Behaviour: Go to a line -> press Y -> go to the middle of another line -> press p -> The copied line is pasted in a new line. This is what happens when no g:clipboard is set.

  • Observed Behaviour: Go to a line -> press Y -> go to the middle of another line -> press p -> The copied line is pasted into the current line. This is what happens when g:clipboard is set.

When observing the content of the wayland clipboard, (wl-paste --no-newline | cat -A) they are the same. Both end with a LF character.

Question:

Why is this? What does neovim do to distinguish between a paste into a new line and why does neovim not paste into a new line when having the custom g:clipboard in place?

3

By default systemlist() doesn't preserve a final newline (i.e. it drops the last "empty string" item from the resulting List). That makes Neovim think that clipboard's content is of type "char", not "line".

Hence you must do the following instead (see :h systemlist(), note that Neovim version differs from Vim's one):

{-> systemlist('wl-paste...', '', 1)}

Another option, if you're using a GUI version of Neovim exclusively, you may prefer simply to send RPC request to your GUI frontend application which may be able to serve it in a platform-independent manner (through Qt, Gtk, etc.etc.).

For example, nvim-qt is shipped with a standard "nvim_gui_shim" plugin which provides GuiClipboard() function. And so executing simply :call GuiClipboard() will automatically setup for you a "custom" g:clipboard which already has those rpcrequest() and rpcnotify() calls.

However, besides it won't work under plain console nvim, I discovered another nasty problem: at least in nvim-qt it requires that UI has been entered and fully prepared. And so it could be inconvenient to setup it upon Neovim startup. Of course, you can provide your own g:clipboard with RPC calls instead (note: RPC parameters may differ between different GUI frontends; also some applicaitons may choose not to implement such requests at all).

Yet another option is to implement clipboard access yourself. Here is my own Neovim clipboard provider which can do this without external dependencies. However, so far I've only implemented Xlib version, and so there's no native module for Wayland.

1
  • 1
    Wow, this answer exceeded my expectations by far! Thank you, I'll link it in the github issue!
    – Jounathaen
    Aug 19 '20 at 21:24

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.