LoginSignup
7
14

More than 5 years have passed since last update.

Spacemacsでクリップボードにコピー&ペースト

Last updated at Posted at 2017-05-20

背景

Emacsは独自バッファでヤンク(コピー)・ペーストを行っています。
これ自体は便利なのですが、Emacsの環境外とのやり取りについて考慮する必要があります。

Emacsでは強引にドラッグしてクリップボードにコピーするなどしていました(恥ずかしい)。

そして最近Spacemacsに移行したのですが、なんとそれが出来なくなっていました。
うまくドラッグできないし、何よりOSのコピーコマンドが無効になっています。

応急処置的にorg-modeからhtmlを出力・ブラウザに吐き出してコピペしていたのですが、
文字コードや改行コードの都合で求めるビューが出なかったりするので、
いよいよ重い腰を上げてクリップボード操作をEmacsから出来るようにしようと思い立ちました。

解決法

Spacemacsのissueで上がってました。
https://github.com/syl20bnr/spacemacs/issues/2222

そもそもspacemacsのカスタマイズを何処に記述すべきか分かっていなかったのですが、ここなのですね。

.spacemacs
(defun dotspacemacs/user-config ()
;;ここに設定を書く
)

Macの場合

以下を.spacemacs(defun dotspacemacs/user-config ()の下に追加。
spc o y / spc o pでコピー/ペーストができます。

.spacemacs
(defun copy-to-clipboard ()
  "Copies selection to x-clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (message "Yanked region to x-clipboard!")
        (call-interactively 'clipboard-kill-ring-save)
        )
    (if (region-active-p)
        (progn
          (shell-command-on-region (region-beginning) (region-end) "pbcopy")
          (message "Yanked region to clipboard!")
          (deactivate-mark))
      (message "No region active; can't yank to clipboard!")))
  )

(defun paste-from-clipboard ()
  "Pastes from x-clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (clipboard-yank)
        (message "graphics active")
        )
    (insert (shell-command-to-string "pbpaste"))
    )
  )
(evil-leader/set-key "o y" 'copy-to-clipboard)
(evil-leader/set-key "o p" 'paste-from-clipboard)

Linuxの場合

はじめに、xselのインストールが必要です。

$ apt install xsel

続いて以下を.spacemacs(defun dotspacemacs/user-config ()の下に追加。

spc o y / spc o pでコピー/ペーストができます。

.spacemacs
(defun copy-to-clipboard ()
  "Copies selection to x-clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (message "Yanked region to x-clipboard!")
        (call-interactively 'clipboard-kill-ring-save)
        )
    (if (region-active-p)
        (progn
          (shell-command-on-region (region-beginning) (region-end) "xsel --clipboard --input")
          (message "Yanked region to clipboard!")
          (deactivate-mark))
      (message "No region active; can't yank to clipboard!")))
  )

(defun paste-from-clipboard ()
  "Pastes from x-clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (clipboard-yank)
        (message "graphics active")
        )
    (insert (shell-command-to-string "xsel --clipboard --output"))
    )
  )
(evil-leader/set-key "o y" 'copy-to-clipboard)
(evil-leader/set-key "o p" 'paste-from-clipboard)

pbcopy / pbpaste がないのでクリップボードを操作するコマンド xsel --clipboard --input / xsel --clipboard --output を使用します。

コピペは SPC o y SPC o p

Spaceキー操作は病みつきになりますね。
それだけでもSpacemacsにして良かったです。

7
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
14