$shibayu36->blog;

クラスター株式会社のソフトウェアエンジニアです。エンジニアリングや読書などについて書いています。

EmacsからiTermにコマンドを送る

 EmacsからiTermに対してコマンドが送りたい時がある。例えば

  • 現在Emacsで開いているファイルのディレクトリにiTermで移動したい
  • 現在Emacsで編集中のテストをiTermで実行したい

など。

そういう時には以下の様なユーティリティを定義しておくと便利。AppleScriptを利用して、iTermにコマンドを送る。

(defun execute-on-iterm (command)
  (interactive "MCommand: ")
  (do-applescript
   (format "tell application \"iTerm\"
              activate
              tell current session of current terminal
                write text \"%s\"
              end tell
            end tell"
           command)))

これを利用して「現在Emacsで開いているファイルのディレクトリにiTermで移動したい」を実現するには以下のように書くだけで良い。

(defun cd-on-iterm ()
  (interactive)
  (execute-on-iterm (format "cd %s" default-directory)))