echo Impossible|sed 's/Im/To be /'

July 25, 2015

Emacs Copy/Paste work with clipboard of X Server.

Emacs Copy/Paste work with clipboard of X Server

工作上常需要 Emacs 與 X Server 相互 Copy/Paste 文字,為了暸解這功能搞了幾天。

xsel man page

The X server maintains three selections, called PRIMARY, SECONDARY and CLIPBOARD. The PRIMARY selection is conventionally used to implement copying and pasting via the middle mouse button. The SECONDARY and CLIPBOARD selections are less frequently used by application programs. This program operates on the PRIMARY selection unless otherwise specified.

By default, this program outputs the selection without modification if both standard input and standard output are terminals (ttys). Otherwise, the current selection is output if standard output is not a terminal (tty), and the selection is set from standard input if standard input is not a terminal (tty). If any input or output options are given then the program behaves only in the requested mode.

If both input and output is required then the previous selection is output before being replaced by the contents of standard input.

實際上 X server 有三個 selections, 名稱為 PRIMARY, SECONDRY, CLIPBOARD. 用在滑鼠中鍵的稱為 PRIMARY,應用程式用多為 CLIPBOARD 與 SECONDARY。

Copy from X-window, Paste to Emacs

Emacs paste text from Clipboard

透過 X-window 的剪貼簿貼文字:

(insert (x-get-selection 'CLIPBOARD))


(global-set-key (kbd "C-x C-y 1") 'x-clipboard-yank)

Emacs paste text from PRIMARY


(mouse-yank-primary 1)
(insert (x-get-selection 'PRIMARY))


(global-set-key (kbd "C-x C-y 2") (lambda () (interactive) (mouse-yank-primary 1)))

Copy from Emacs, Paste to X-window

Emacs copy text to Clipboard

請參考以下兩個 URL

xclip-mode 用了二年多快,近來常用 TRAMP mode (Transparent Remote Access, Multiple Protocols) 遠端 edit/coding。 但 xclip-mode 與 TRAMP mode 是互斥的,後改用 yank-to-x-clipboard 在有需要時在 copy 到 Clipboard 後使用。

Emacs copy text to PRIMARY

X-window 下的 emacs 只要 mark 標註一段文字,就會自動複製到 PRIMARY。其中的文字之後用滑鼠中鍵就可以貼上。