Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aont/76b6060e592acf076dc3588d6a9afbf1 to your computer and use it in GitHub Desktop.

Select an option

Save aont/76b6060e592acf076dc3588d6a9afbf1 to your computer and use it in GitHub Desktop.

Automatically Copying Killed Text to the System Clipboard via OSC52 in Emacs

Working inside a terminal often comes with one annoyance: copying text between your terminal-based editor and the system clipboard is not seamless. Fortunately, OSC52—a terminal escape sequence—provides a way to copy text from terminal applications (like Emacs running in a terminal) to the system clipboard.

This guide shows how to make Emacs automatically send killed text (copied or cut with M-w or C-w) to the system clipboard via OSC52.


What is OSC52?

OSC52 is a control sequence supported by many modern terminal emulators (xterm, alacritty, wezterm, iTerm2, etc.). It allows applications to write directly to the system clipboard by emitting a properly formatted escape sequence. When combined with Emacs Lisp, we can make Emacs automatically forward text from its kill ring to the system clipboard.


The Emacs Lisp Setup

Add the following snippet to your init.el (or early-init.el):

(defun copy-to-osc52 (text &optional push)
  "Send TEXT to the system clipboard via OSC52."
  (let ((b64 (base64-encode-string (encode-coding-string text 'utf-8) t)))
    (send-string-to-terminal (concat "\e]52;c;" b64 "\a"))))

(defun osc52-copy-last-kill ()
  "Copy last killed text via OSC52."
  (when kill-ring
    (copy-to-osc52 (car kill-ring))))

;; Hook into the standard kill commands
(advice-add 'kill-ring-save :after (lambda (&rest _) (osc52-copy-last-kill)))
(advice-add 'kill-region    :after (lambda (&rest _) (osc52-copy-last-kill)))

How It Works

  • copy-to-osc52:

    • Takes text, encodes it as UTF-8.
    • Base64-encodes the result (OSC52 requires Base64).
    • Wraps it in the escape sequence ESC ] 52 ; c ; <data> BEL.
    • Sends the sequence to the terminal.
  • osc52-copy-last-kill:

    • Fetches the most recent entry in the kill ring (Emacs’s clipboard).
    • Passes it to copy-to-osc52.
  • The advice-add calls hook into kill-ring-save (M-w) and kill-region (C-w) so every time you copy or cut text, Emacs will also send it to your terminal’s clipboard.


Terminal Support

  • Direct Emacs in terminal: Works if your terminal supports OSC52.

  • tmux users: You need to enable clipboard support in .tmux.conf:

    set-option -g set-clipboard on
  • SSH sessions: OSC52 also works over remote connections if both the remote Emacs and your local terminal support it.


Why This Is Useful

  • No more juggling between Emacs’s internal kill ring and your system clipboard.
  • Seamlessly copy text from Emacs running in terminal sessions to your desktop environment.
  • Works equally well inside tmux, SSH, or plain terminals.

✅ With just a few lines of Emacs Lisp, you can bridge the gap between Emacs’s kill ring and your system clipboard—making Emacs in the terminal feel almost as smooth as GUI Emacs.

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