1、在网上广为流传的一种让emacs和系统剪切板共享的方法是在.emacs文件中加入
(setq x-select-enable-clipboard t)
这种方法仅对图形化emacs有效,如果用 emacs -nw 命令打开emacs的话,在命令行中是无效的,因为在命令行下没有权限访问 X 的剪切板(http://unix.stackexchange.com/questions/72605/emacs-copy-and-paste)。
2、正因为第1点,所以网上同时流传着另一种方法:
;;start 设置剪切板共享
(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))))
(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)
;;end 设置剪切板共享
这种方法确实可用,但是里面的pbpaste和pbcopy命令根本不是linux下的,而是mac下的。所以在linux下应该找到等效的命令替换它们。
3、linux下的剪切板操作命令
找到了两种:xclip和xsel
这两个命令linux不自带,需要安装。两种命令的具体使用方法不做介绍,总之nicek在网上找到了一个适用于linux下的配置(http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/):
;; http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/ ;; I prefer using the "clipboard" selection (the one the ;; typically is used by c-c/c-v) before the primary selection ;; (that uses mouse-select/middle-button-click) (setq x-select-enable-clipboard t) ;; If emacs is run in a terminal, the clipboard- functions have no ;; effect. Instead, we use of xsel, see ;; http://www.vergenet.net/~conrad/software/xsel/ -- "a command-line ;; program for getting and setting the contents of the X selection" (unless window-system (when (getenv "DISPLAY") ;; Callback for when user cuts (defun xsel-cut-function (text &optional push) ;; Insert text to temp-buffer, and "send" content to xsel stdin (with-temp-buffer (insert text) ;; I prefer using the "clipboard" selection (the one the ;; typically is used by c-c/c-v) before the primary selection ;; (that uses mouse-select/middle-button-click) (call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input"))) ;; Call back for when user pastes (defun xsel-paste-function() ;; Find out what is current selection by xsel. If it is different ;; from the top of the kill-ring (car kill-ring), then return ;; it. Else, nil is returned, so whatever is in the top of the ;; kill-ring will be used. (let ((xsel-output (shell-command-to-string "xsel --clipboard --output"))) (unless (string= (car kill-ring) xsel-output) xsel-output ))) ;; Attach callbacks to hooks (setq interprogram-cut-function 'xsel-cut-function) (setq interprogram-paste-function 'xsel-paste-function) ;; Idea from ;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/ ;; http://www.mail-archive.com/help-gnu-emacs@gnu.org/msg03577.html ))
下面是对ubuntu下剪贴板结构的猜想: