Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renatocassino/1e418a02b5ba0cc12220b230754bb4c9 to your computer and use it in GitHub Desktop.
Save renatocassino/1e418a02b5ba0cc12220b230754bb4c9 to your computer and use it in GitHub Desktop.
Using the same clipboard (x-clipboard) for ubuntu and mac os X in the same script for emacs.
;;;;; Add in your ~/.emacs.d/init.el
;;;; Clipboard for ubuntu
(defun copy-from-ubuntu (text &optional push)
(interactive)
(if (display-graphic-p)
(progn
(message "Yanked region to x-clipboard!")
(call-interactively 'clipboard-kill-ring-save)
)
(if (region-active-p)
(progn
(shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
(message "Yanked region to clipboard!")
(deactivate-mark))
(message "No region active; can't yank to clipboard!")))
)
(defun paste-to-ubuntu ()
(interactive)
(if (display-graphic-p)
(progn
(clipboard-yank)
(message "graphics active")
)
(insert (shell-command-to-string "xsel -o -b"))
)
)
;;;;; Clipboard Mac OS X
(defun copy-from-osx ()
(shell-command-to-string "pbpaste"))
(defun paste-to-osx (text &optional push)
(let ((process-connection-type nil))
(let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
(process-send-string proc text)
(process-send-eof proc))))
(if (equal (shell-command-to-string "uname") "Darwin\n")
(progn
;;;;; Clipboard Mac OS X
(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx))
(progn
;;;;; Clipboard UBUNTU
(setq interprogram-cut-function 'copy-from-ubuntu)
(setq interprogram-paste-function 'paste-to-ubuntu)))
@brunomerod
Copy link

It works but doesn't disable the "kill ring".

Debugger entered--Lisp error: (error "Kill ring is empty")
  signal(error ("Kill ring is empty"))
  error("Kill ring is empty")
  current-kill(0)
  yank(nil)
   funcall-interactively(yank nil)
  call-interactively(yank nil nil)
  command-execute(yank)

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