另一个解决方案是,如果可能的话,只需使用cli工具,
此解决方案的优点是剪贴板始终可用(例如,在远程ssh时)。
我的回答分为两个部分。第一部分介绍了一些方便的工具来操作剪贴板。第二部分将回答您的原始问题(将剪贴板存储到kill ring中)。
第一部分
将以下代码插入您的〜/ .emacs中:
(setq *is-a-mac* (eq system-type 'darwin))
(setq *cygwin* (eq system-type 'cygwin) )
(setq *linux* (or (eq system-type 'gnu/linux) (eq system-type 'linux)) )
(defun copy-to-x-clipboard ()
(interactive)
(if (region-active-p)
(progn
(cond
((and (display-graphic-p) x-select-enable-clipboard)
(x-set-selection 'CLIPBOARD (buffer-substring (region-beginning) (region-end))))
(t (shell-command-on-region (region-beginning) (region-end)
(cond
(*cygwin* "putclip")
(*is-a-mac* "pbcopy")
(*linux* "xsel -ib")))
))
(message "Yanked region to clipboard!")
(deactivate-mark))
(message "No region active; can't yank to clipboard!")))
(defun paste-from-x-clipboard()
(interactive)
(cond
((and (display-graphic-p) x-select-enable-clipboard)
(insert (x-selection 'CLIPBOARD)))
(t (shell-command
(cond
(*cygwin* "getclip")
(*is-a-mac* "pbpaste")
(t "xsel -ob"))
1))
))
(defun my/paste-in-minibuffer ()
(local-set-key (kbd "M-y") 'paste-from-x-clipboard)
)
(add-hook 'minibuffer-setup-hook 'my/paste-in-minibuffer)
第二部分
在您的〜/ .emacs中插入以下代码,然后从现在开始,使用“ Mx从剪贴板粘贴到CC-kill-ring”粘贴:
(defun paste-from-clipboard-and-cc-kill-ring ()
"paste from clipboard and cc the content into kill ring"
(interactive)
(let (str)
(with-temp-buffer
(paste-from-x-clipboard)
(setq str (buffer-string)))
;; finish the paste
(insert str)
;; cc the content into kill ring at the same time
(kill-new str)
))