Emacsで編集中のファイルのディレクトリをtmuxの新しいウィンドウで開く

前回に引き続きターミナルとEmacs間の連携の話。
今回はEmacsで編集中のファイルのディレクトリを、ターミナル(tmux)の新しいウィンドウで開く。

設定

(defun open-current-directory-in-tmux-new-window ()
  "カレントディレクトリをtmuxの新しいwindowで開く."
  (interactive)
  (let* ((dir (if buffer-file-name
                  (file-name-directory buffer-file-name)
                (expand-file-name "~/")))
         (cmd (concat "tmux new-window \"cd " dir "; exec $SHELL\"")))
    (cond ((eq (shell-command cmd) 0)
           (message "Open directory %s in tmux new window." dir))
          (t
           (message "Failed to create new window in tmux.")))))

(global-set-key (kbd "C-x C-t") 'open-current-directory-in-tmux-new-window)

これでtmuxを起動した状態で、Emacs上からC-x C-tすると、tmuxの新しいウィンドウに編集中のファイルのディレクトリが開けるようになった。