Skip to content

Instantly share code, notes, and snippets.

@yorickvP
Created March 14, 2019 11:02
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yorickvP/6132f237fbc289a45c808d8d75e0e1fb to your computer and use it in GitHub Desktop.
Save yorickvP/6132f237fbc289a45c808d8d75e0e1fb to your computer and use it in GitHub Desktop.
teach emacs to use wl-copy
(setq wl-copy-process nil)
(defun wl-copy (text)
(setq wl-copy-process (make-process :name "wl-copy"
:buffer nil
:command '("wl-copy" "-f" "-n")
:connection-type 'pipe))
(process-send-string wl-copy-process text)
(process-send-eof wl-copy-process))
(defun wl-paste ()
(if (and wl-copy-process (process-live-p wl-copy-process))
nil ; should return nil if we're the current paste owner
(shell-command-to-string "wl-paste -n | tr -d \r")))
(setq interprogram-cut-function 'wl-copy)
(setq interprogram-paste-function 'wl-paste)
@be-neth
Copy link

be-neth commented Apr 22, 2021

Hi, thanks for that!

From my emacs (using emacsclient in alacritty terminal) I need to set WAYLAND_DISPLAY variable to get this working.

M-x setenv

@yorickvP
Copy link
Author

I'm now using emacs-pgtk, which has native wayland support. It's in https://github.com/nix-community/emacs-overlay

@croissong
Copy link

croissong commented Oct 24, 2021

@yorickvP I am using emacs-pgtk, but pasting into emacs still doesn't work a lot of the time.
The value of (gui-selection-value) is different from the wl-paste output.

Your snippet works 👍

@sochotnicky
Copy link

@yorickvP It seems like you don't need this anymore, however I still find wl-copy/wl-paste useful when running emacsclient over waypipe (since I want to have copy/paste between host where the emacsclient is displaying & emacs). So I reused bits of this in https://gitlab.freedesktop.org/sochotnicky/waypipe-mode.el

@hrldcpr
Copy link

hrldcpr commented Jan 4, 2024

Thanks for this!

I only wanted copying (since my terminal emulator already does pasting just fine), so I simplified the code a bit, for anyone who's interested:

(defun wl-copy (text)
  (let ((p (make-process :name "wl-copy"
                         :command '("wl-copy")
                         :connection-type 'pipe)))
    (process-send-string p text)
    (process-send-eof p)))
(setq interprogram-cut-function 'wl-copy)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment