How to copy and paste in Wayland?

What happened?

Copying from Emacs (select a line, y) to the system clipboard or pasting (p) from the clipboard to Emacs does not work. I have wl-clipboard installed and copying between Neovim works with some configuration.

The code at the bottom of How to copy in wayland? - #17 by jman applied to config.el had no effect.

What did you expect to happen?

Copy from Emacs to system clipboard or paste from clipboard to work.

Steps to reproduce

  1. Start Fedora 39 with GNOME Wayland
  2. Select some text in Emacs and copy with y. Try to paste into another text editor. Vice versa with copying text from text editor to Emacs with p.

System information

Can not copy. I do have a picture of the system output.


I’ll just point out that that code snippet works (I’m still using it). I assume it must be something related to your Emacs/Doom config. Unsure how to debug though.

Copy and pasting to and from Emacs is configured also by other variables. Here they are (with the values I see in my config):

select-enable-primary                      nil
select-enable-clipboard                    t
x-select-enable-primary                    nil
x-select-enable-clipboard                  t
xclip-select-enable-clipboard              t

What are your values for them?

More info at EmacsWiki: Copy And Paste

Hope it helps

Thank you,

@jman I am new to Emacs. How do I get the values for them?

You can see my config here (without the any of the wayland copy and paste code currently):

I tried the Wayland snippet from the EmacsWiki. It had no effect at least in pasting in text from the clipboard. Are you sure you do not have something else? My config is pretty minimal as you can see above.

To get the value of any variable in Emacs you can run M-x describe-variable, so for example you would run M-x describe-variable RET select-enable-primary (RET means press Return). You will get the current value and a bit of documentation.

These variables I mention are not Doom specific, it’s standard Emacs stuff and as far as I know that snippet is all there is to enable copy&paste on Wayland (I’m also not an Emacs guru, so maybe I’m missing something else).

Also ensure to reload your Doom configuration after changing it. Either with M-x doom/reload or restarting Doom.

Related: Emacs is completely introspectable, at this link a list of describe-* commands I find useful.

Thank you @jman. All values above match yours except for the xclip. It is not set at all.

I later added back the xclip code from your forum post and the variable still does not show up.

Not sure what is missing.

That could hint at a problem when you try to evaluate (i.e. run) that configuration snippet. Every variable in the config: block should be assigned a value:

  • xclip-program
  • xclip-select-enable-clipboard
  • xclip-mode
  • xclip-method

Please try to manually assign a value to xclip-select-enable-clipboard by evaluating this snippet: (setq xclip-select-enable-clipboard t).

(you can quickly evaluate any lisp code snippet by positioning the cursor after the last closing parenthesis and then run C-x C-e), then check the result with M-x describe-variable as described above. Evaluating code like this is ephemeral and will only last until the next Emacs restart.

(btw apologies for my overly didactical instructions, they are not meant to be offensive, just trying to keep them as easy as possible since you mention you’re new to Emacs).

Thank you @jman. I appreciate the overly didactical instructions. They are useful for a newcomer.

That last line + your code in the previous post got pasting into Emacs working for both GUI and terminal mode Emacs. I use alias e='emacs -nw' in my dotfiles.

However, when I try to copy (yank) out of terminal Emacs it does not copy to the clipboard. GUI Emacs, with copying out to the clipboard, does work.

My changed code is listed as below for my config.el

(use-package! xclip
  (setq xclip-program "wl-copy")
  (setq xclip-select-enable-clipboard t)
  (setq xclip-mode t)
  (setq xclip-method (quote wl-copy)))

(setq xclip-select-enable-clipboard t)

cool, we’re getting there :)

First, a nit about vocabulary:

  • “yank” in Emacs lingo means “paste”
  • “kill” means “cut”
  • kill-ring-save is instead what you do for “copy” (Emacs copies the text in the kill-ring but does not “kill” it from the buffer)

More info here.

Now, why yank&kill work in GUI mode but not in text mode for you? :thinking: I’m pretty sure that also the variable xclip-mode in your config is not set to t but it’s nil. Please check if that’s the case and if yes evaluate the snippet (setq xclip-mode t). It should make copying work also in emacs no-GUI.

If that solves, now you will notice how strange is that you have to define these variables again outside of the :config block of the xclip package. This should not be needed. Unsure though why the xclip configuration snippet doesn’t work in your config :thinking:

Hi @jman,

Thank you for your help. I am not sure exactly what I did, but I reinstalled and I got a few errors that made more sense (missing xclip).

Added to packages.el

(package! xclip)

Final code added to config.el

(use-package! xclip
  (setq xclip-program "wl-copy")
  (setq xclip-select-enable-clipboard t)
  (setq xclip-mode t)
  (setq xclip-method (quote wl-copy)))

Copy and paste now works perfectly in both. Thank you again for your help.

1 Like