LoginSignup
1
2

More than 3 years have passed since last update.

WSL上のemacs向けclipboard-copy関数を作成した

Last updated at Posted at 2019-06-25

皆さんは、どうやって clipboard に copy していますか?

僕は、 clipboard-copy 関数を init-loader を使って 環境別でそれぞれ定義していました。
ただ WSL では GNU/Linux として認識しつつ、コマンド xsel では copy できず、
clip.exe を使う必要がありました。

愚直に Linux 側の関数で判定するしか思いつかなかったんですが、紹介します。
もっといい方法があれば、教えていただけると幸いです \(^o^)/

環境

  • WSL 1
  • GNU Emacs 25.2.2
  • Linux XX 4.4.0-17763-Microsoft #379-Microsoft Wed Mar 06 19:16:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux

実装

WSL 1上であれば、 uname -a の結果に Microsoft が入るので、これを用います。
(これが悪用している感じあるので、変えたい)

以下関数を linux-custom.el (init-loaderを使っているので)に記述し、
Linux上での起動時に定義しつつ、実行時になりますが...良しなに切り替えました。

(defun isWSL ()
  "run emacs on windows subsystem linux ?"
  (if (string= "" (shell-command-to-string "uname -r |grep Microsoft"))
    nil
    t))

(defun clipboard-copy ()
  (interactive)
  (when (region-active-p)
    (if (isWSL)
      (shell-command-on-region (region-beginning) (region-end) "clip.exe" nil nil)
      (shell-command-on-region (region-beginning) (region-end) "xsel --clipboard --input" nil nil)
      )
   )
)

1
2
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
1
2