Minimal Clipboard Support in Terminal Emacs

Cover Image for Minimal Clipboard Support in Terminal Emacs
Rahul M. Juliato
Rahul M. Juliato
#emacs#terminal# clipboard# linux# wsl# macos

Terminal Emacs users often face an annoying limitation: copying and pasting between Emacs and the system clipboard doesn’t "just work." While graphical Emacs integrates with the OS clipboard by default, the terminal version leaves that responsibility to you, especially if you're inside tmux, ssh, or simply prefer living in the terminal.

Thankfully, there are excellent packages like: xclip and clipetty, as well as guides like Emacswiki: CopyAndPaste that tackle this. They offer elegant solutions, often with additional features and broader compatibility.

But sometimes, you just want something small and direct, something you can copy-paste into your config and tweak per OS, with no external Emacs dependencies.

That’s what I present here.


✂️ emacs-solo-clipboard: A Minimal Clipboard Bridge

This snippet is part of my emacs-solo configuration. It gives terminal Emacs clipboard support using native system tools (which, of course, you need installed in your system):

pbcopy / pbpaste for macOS

clip.exe / powershell.exe for WSL

wl-copy / wl-paste for Wayland

xclip for X11

It hooks into Emacs' standard interprogram-cut-function and interprogram-paste-function APIs.

;;; EMACS-SOLO-CLIPBOARD
;;
;;  Allows proper copy/pasting on terminals
;;
(use-package emacs-solo-clipboard
  :ensure nil
  :no-require t
  :defer t
  :init
  (cond
   ;; macOS: use pbcopy/pbpaste
   ((eq system-type 'darwin)
	(setq interprogram-cut-function
		  (lambda (text &optional _)
			(let ((process-connection-type nil))
			  (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
				(process-send-string proc text)
				(process-send-eof proc)))))
	(setq interprogram-paste-function
		  (lambda ()
			(shell-command-to-string "pbpaste"))))

   ;; WSL (Windows Subsystem for Linux): Use clip.exe for copy and powershell.exe for paste
   ((and (eq system-type 'gnu/linux)
		 (getenv "WSLENV"))
	(setq interprogram-cut-function
		  (lambda (text &optional _)
			(let ((process-connection-type nil))
			  (let ((proc (start-process "clip.exe" "*Messages*" "clip.exe")))
				(process-send-string proc text)
				(process-send-eof proc)))))
	(setq interprogram-paste-function
		  (lambda ()
			(string-trim (shell-command-to-string "powershell.exe -command Get-Clipboard")))))

   ;; Linux with wl-copy/wl-paste (Wayland)
   ((and (eq system-type 'gnu/linux) (executable-find "wl-copy"))
	(setq interprogram-cut-function
		  (lambda (text &optional _)
			(let ((process-connection-type nil))
			  (let ((proc (start-process "wl-copy" "*Messages*" "wl-copy")))
				(process-send-string proc text)
				(process-send-eof proc)))))
	(setq interprogram-paste-function
		  (lambda ()
			(shell-command-to-string "wl-paste -n"))))

   ;; Linux with xclip (X11)
   ((and (eq system-type 'gnu/linux) (executable-find "xclip"))
	(setq interprogram-cut-function
		  (lambda (text &optional _)
			(let ((process-connection-type nil))
			  (let ((proc (start-process "xclip" "*Messages*" "xclip" "-selection" "clipboard")))
				(process-send-string proc text)
				(process-send-eof proc)))))
	(setq interprogram-paste-function
		  (lambda ()
			(shell-command-to-string "xclip -selection clipboard -o"))))))

⚠️ This Is Hacky, and That's the Point

This is not a library or polished package. It's a minimal Emacs Lisp snippet that works well if your system already provides the right clipboard utilities. It doesn't abstract anything and won't handle edge cases. But that also means:

✅ Easy to audit

✅ Easy to tweak

✅ Easy to share small pieces

You can literally just copy the macOS or Wayland part and ignore the rest.


✅ Wrap-Up

If you’re running Emacs in the terminal and want clipboard support without extra layers or packages, this might be just what you need.

It’s not pretty. It might be not that smart. But it works.

And sometimes, that’s exactly the kind of config you want.


Let me know if you run into clipboard quirks I didn’t cover here, or if you’ve built your own minimalist setup, I’m always curious about tiny hacks that punch above their weight. 🧠🛠️🔥