file management: emacs dired to replace Finder in mac os x (and other OS)

Emacs
Linux
Mac OS X
Author

Vinh Nguyen

Published

December 1, 2009

I hate Finder in Mac OS X. I mean, it looks nice and all, but it is not customizable. I really liked the KDE window management back when I used Kubuntu because I can select/de-select files with the keyboard, cut/paste/copy files with the keyboard, have shortcuts to different locations, etc. Finder has some keyboard shortcuts, but not at all flexible. You can copy and paste files, but you can't cut and paste files. Selecting/de-selecting files is not as flexible (eg, skipping a file). I can't go to the sidebar window where all my bookmarked locations reside using the keyboard. Finder, and in general Mac OS X, is not so flexible. This bugs me a lot. I wanted to get stumpwm working in OS X so that things are more keyboard-oriented. However, it doesnt work with all the native mac apps. The fix is with dired mode in emacs. I spend most of my time in emacs already. With dired, I can even manage files inside emacs (I can do so in the shell, but this makes browsing and executing commands on files a breeze). Use "C-x f" and navigate to a location rather than a file. Learn all the keyboard shortcuts for delete, rename, execute shell command, etc. You can even cut/copy/paste with wuxch-dired-copy-paste. The functions below also allow me to open files the Mac way, with the "open" command. This is cool because files will be opened with the native Mac default application.

;; http://www.emacswiki.org/emacs/DiredPlus
;; copy and paste files in dired
;; needed additional files, such as dired+, dired-details+, find-dired+, and find-dired-
(require 'wuxch-dired)
(require 'wuxch-dired-copy-paste)
;;http://stackoverflow.com/questions/1824696/function-to-call-same-shell-command-in-dired
;; (defun dired-do-shell-mac-open-vqn ()
;; (interactive)
;; (dired-do-async-shell-command
;; "open" current-prefix-arg
;; (dired-get-marked-files t current-prefix-arg)))
(defun dired-do-shell-mac-open-vqn ()
(interactive)
(save-window-excursion
 (dired-do-async-shell-command
 "open" current-prefix-arg
 (dired-get-marked-files t current-prefix-arg))))

(define-key dired-mode-map (kbd "s-o") 'dired-do-shell-mac-open-vqn)

I can use Apple-o/Windows-o to open files with the default mac program for the marked files.I also use emacs' bookmark system ("C-x r b" and "C-x r m") to bookmark locations.Note that all this is not specific to Mac OS X. Dired's been around forever – I just never picked it up really. Now with my "open" trick and the bookmark system, this should really replace my use of Finder in Mac OS X.

Update 11/10/2010 - On Linux

I'm back on Linux now. A few updates on launching files with the default application. Add the following to the emacs init file:

(defun dired-do-shell-launch-file-default ()
 (interactive)
 (save-window-excursion
 (dired-do-async-shell-command
 "$HOME/Documents/bin/open.sh" current-prefix-arg ;; linux;; multiple files
 ;; "nohup xdg-open" current-prefix-arg ;; linux can open multiple files, but one at a time
 ;; "see" current-prefix-arg ;; linux;; can open at most 1 file (being opened)
 ;; "open" current-prefix-arg ;; mac os x
 (dired-get-marked-files t current-prefix-arg))))
(define-key dired-mode-map (kbd "s-o") 'dired-do-shell-launch-file-default)

Need the script open.sh (thank you fledermaus from #emacs):

#! /bin/bash

for file in "$@"
do
nohup xdg-open "$file" &
done
sleep 1

Now, to unmount devices in emacs:

;; unmount disk in dired
;;http://loopkid.net/articles/2008/06/27/force-unmount-on-mac-os-x
(defun dired-do-shell-unmount-device ()
 (interactive)
 (save-window-excursion
 (dired-do-async-shell-command
 "umount" current-prefix-arg ;; linux
 ;; "diskutil unmount" current-prefix-arg ;; mac os x
 (dired-get-marked-files t current-prefix-arg))))
(define-key dired-mode-map (kbd "s-u") 'dired-do-shell-unmount-device)

If an error is returned, make sure the device is not being used anywhere, including opened in nautilus or in some terminal.

The ability to launch files with the default application (look into xdg-open, gnome-open, and see), unmnount devices, and being able to copy/paste files in emacs help makes dired the perfect file manager. I will use this from now on over Nautilus. It'll compliment using stumpwm.