Clicky

Integrating OS X and Emacs Dired

June 15, 2015

I use Dired mode (directory editor) in Emacs rather often. It allows you to manage files quickly—opening files for editing, renaming files, or marking several files for deletion. It is powerful on its own, but sometimes it’s still useful to open a Finder window for a directory or open a file in the default OS X application. For those cases, I use the z key with the following keybinding:

;; Open files in dired mode using 'open'
(eval-after-load "dired"
  '(progn
     (define-key dired-mode-map (kbd "z")
       (lambda () (interactive)
         (let ((fn (dired-get-file-for-visit)))
           (start-process "default-app" nil "open" fn))))))

When Dired mode loads, this snippet of code adds a keybinding for the previously unused z key. The open command attempts to “do what you mean.” By default it opens Finder for directories, Preview for images or PDF files, Safari for HTML documents, and so on.