Skip to content

Instantly share code, notes, and snippets.

@pkkm
Created May 5, 2013 20:53
Show Gist options
  • Save pkkm/5522129 to your computer and use it in GitHub Desktop.
Save pkkm/5522129 to your computer and use it in GitHub Desktop.
When running in a terminal, use the `xclip' utility for copying from and pasting to the X clipboard.
;; Terminal (with XClip): Use CLIPBOARD for explicit cuts. Don't use PRIMARY.
(when (and (not window-system)
(getenv "DISPLAY")
(executable-find "xclip"))
;; On explicit yank, copy to CLIPBOARD.
(defun xclip-copy-to-clipboard (text)
"Copy TEXT to X's clipboard, using the `xclip` utility."
(let* ((process-connection-type nil)
(proc (start-process "xclip" nil "xclip" "-selection" "clipboard")))
(process-send-string proc text)
(process-send-eof proc)))
(setq interprogram-cut-function 'xclip-copy-to-clipboard)
;; When pasting, check the CLIPBOARD and if there's something new, insert it into the kill-ring.
(defun xclip-paste-from-clipboard ()
"If another program provided text for pasting, return it (as a string).
If the last paste was from Emacs or is identical to the last one from Emacs, return nil.
Uses the `xclip` utility."
(let ((clipboard-contents (shell-command-to-string "xclip -o -selection clipboard")))
(if (not (string= (car kill-ring) clipboard-contents))
clipboard-contents
nil)))
(setq interprogram-paste-function 'xclip-paste-from-clipboard))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment