Shammer's Philosophy

My private adversaria

Emacsでkill-ringとClipboardを分離-Linux編

SimpleClipを使用してkill-ringとClipboardを分離する方法を以前見つけたが、Debian環境でこれをやるとCopy & Paste が文字化けするようになった。英数字は問題ないけれども日本語とかのマルチバイト文字はNG。これでは困るのでどうしようかと思っていたが、Emacsのマニュアルに答えが書いてあった。

 Clipboard - GNU Emacs Manual

具体的には一番最後の段。

Prior to Emacs 24, the kill and yank commands used the primary selection (see Primary Selection), not the clipboard. If you prefer this behavior, change x-select-enable-clipboard to nil, x-select-enable-primary to t, and mouse-drag-copy-region to t. In this case, you can use the following commands to act explicitly on the clipboard: clipboard-kill-region kills the region and saves it to the clipboard; clipboard-kill-ring-save copies the region to the kill ring and saves it to the clipboard; and clipboard-yank yanks the contents of the clipboard at point.

このページを参考に設定をしてみた。Emacs 23(デフォルト状態のapt-get install emacsでインストールされるバージョン)での動作確認結果は以下のとおり。

  • 別アプリで Ctrl-C や Ctrl-X したものを Emacs に Paste 可能
  • Emacs 内で clipboard-kill-ring-save(copy) したものや clipboard-kill-region(cut) した文字を他のアプリへ Paste 可能
  • Emacs 内で Ctrl-w や Alt-w で kill-ring に保存した文字は他のアプリへコピーされず、Emacs のみで使い回しされる

自分の理想とする動作になった。Linux の場合はこの設定をやればいい。実際の設定は以下。

;;; Copy and Paste parameters
;;; http://www.gnu.org/software/emacs/manual/html_node/emacs/Clipboard.html
;(setq interprogram-cut-function nil)
;(setq interprogram-paste-function nil)
(setq x-select-enable-clipboard nil)
(setq save-interprogram-paste-before-kill nil)
(setq yank-pop-change-selection nil)
(setq x-select-enable-clipboard-manager nil)
(setq x-select-enable-primary t)
(setq mouse-drag-copy-region t)

;; Key binding
(global-set-key [f5] 'clipboard-kill-region)
(global-set-key [f6] 'clipboard-kill-ring-save)
(global-set-key [f7] 'clipboard-yank)