;;; xsel --- use X clipboard to/from terminal/tty Emacs ;;; Commentary: ;; ;; This file: http://user.it.uu.se/~embe8573/conf/emacs-init/xsel.el ;; ;; .-----------------------------------------. ;; | xsel - faster, keyboard-only, | ;; | more intuitive X clipboard, | ;; | from a Linux VT/console/tty | ;; | or X terminal emulator | ;; | by Emanuel Berg | ;; | embe8573 AT&T student DOS uu DOS se | ;; | first created: way back in 2013-14? | ;; | updated: 05:43, 13/05/2015 | ;; '-----------------------------------------' ;; ;; This gives you to gain access to the X clipboard ;; from a terminal, or a Linux VT/console/tty Emacs ;; instance. (In X: faster keyboard access, ;; I suppose.) ;; ;; Possible: Insert the X clipboard at point and ;; forward point accordingly. ;; ;; DWIM: If there is a region, replace it with the ;; X clipboard. ;; ;; Also possible: set the X clipboard programmatically ;; in Elisp or set it interactively to the current ;; region (if there is one), otherwise set it to the ;; most recent Emacs kill (without popping the kill ;; ring). ;; ;; If DISPLAY is set, it is used; otherwise, use ":0". ;;; Code: (defvar xsel-X-display (or (getenv "DISPLAY") ":0")) (defun insert-X-clipboard () "Insert the X clipboard at point using the X tool xsel. For it to work, X needs to run. After that, move point to the end of the inserted clipboard. If there is a region at the time of invocation, first delete the region - i.e., replace it with the X clipboard. See `set-X-clipboard-to-string'." (interactive) (if mark-active (delete-region (mark) (point))) (shell-command (format "xsel --display \"%s\" -b -o" xsel-X-display) 1) (goto-char (mark)) ) (defun set-X-clipboard-to-string (s) "Set the X clipboard to S. See `set-X-clipboard' and `insert-X-clipboard' for details." (shell-command (format "echo -n %s | xsel --display %s -b -i" s xsel-X-display) )) (defun set-X-clipboard () "If there is a region, set the X clipboard to the contents of that region. If there isn't a region, set the X clipboard to the most recent Emacs kill, without popping it from the kill ring. See `insert-X-clipboard'." (interactive) (let ((input (if mark-active (buffer-substring-no-properties (mark) (point)) (string-make-unibyte (current-kill 0 t)) ))) (set-X-clipboard-to-string (shell-quote-argument input)) )) ;; This package can be used to get complete ;; transparency - but it is probably over"kill" to ;; have it enabled for every single kill. ;; ;; (setq interprogram-cut-function ...) ;; misc shorthands: (defun x-copy-buffer () "Copy the buffer's contents to the X clipboard." (interactive) (save-excursion (mark-whole-buffer) (set-X-clipboard) (keyboard-quit) )) (defalias 'xb 'x-copy-buffer) (defalias 'pst 'insert-X-clipboard) (defalias 'xo 'insert-X-clipboard) (defalias 'cpy' 'set-X-clipboard) (defalias 'xi 'set-X-clipboard) (provide 'xsel) ;;; xsel.el ends here