LoginSignup
11
7

More than 5 years have passed since last update.

EmacsからFinderを開く方法

Last updated at Posted at 2018-03-28

Emacsを使っていると,編集しているfileのdirectoryをFinder上に表示したいときがあります.
そんな時用のTipsです.

shell-commandを使おう

Emacsにはshell-commandという便利な関数があります.
これは引数に文字列を取り,shell上で実行してくれる関数です.

M-x shell-command RET "open ."

これでshell上でopen .と入力した時と同じ結果が得られます.

out.gif

自分で関数を定義しよう

いちいちM-x shell-commandなどするのは面倒ですので,これを1つのユーザ定義関数にしましょう.

;; current directory open
(defun finder-current-dir-open()
  (interactive)
  (shell-command "open ."))

;; directory open
(defun finder-open(dirname)
  (interactive "DDirectoryName:")
  (shell-command (concat "open " dirname)))

;; set the keybind
(global-set-key (kbd "M-f") 'finder-current-dir-open)

これでM-fと入力するだけで,現在のdirectoryがFinderで開けます.

補足1 : interactiveとは?

(defun finder-current-dir-open()
  (interactive) <---- これ!!
  (shell-command "open ."))

ここに書いてあるinteractiveの意味についてちょっとだけ補足します.

定義した関数の中(今回だったらfinder-current-dir-open)にinteractiveと記述することによって,M-xでその関数を呼び出すことができます.
(なので,もし上記の関数にinteractiveが記述されていないと,M-xしても関数を呼べません)

補足2 : interactive "D"とは?

(defun finder-open(dirname)
  (interactive "DDirectoryName:") <---- これ!
  (shell-command (concat "open " dirname)))

ここではinteractive "D"ではなくDDirectoryName:となっていますが,最初の文字"D"以降はMinibufferに表示される文字なので無視して問題ないです.

Screen Shot 2018-03-28 at 17.35.13.png

さて,ここで言う"D"とは,DirectoryのDですね.
そして"D"が記述されることで,EmacsはdirectoryのリストをMinibufferに表示してくれます.
その後,開きたいdirectoryを入力することで,Finder上でopenできます.

out.gif


その他にもinteractive "P"とかinteractive "n"とかあります.
詳しくはこちらを参考ください!

参考

Emacsでマイナーモードを定義してみたり、 M-x で動くインタラクティブ関数を定義してみたりする
Emacs Interactive Codes

11
7
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
11
7