Saturday, May 17, 2008

Useful Emacs Dired launch hack

[leavening some technical stuff into the usual political/philosophical content. I'm curious to see if search engines pick it up.]

I live my computational life inside Emacs, which is the best user interface for almost everything; but it's sometimes frustrating that you can't escape Emacs' text-only world. Well, here is a little hack that lets you launch arbitrary files from a Dired view of a directory; using the operating system's registered application; ie, if you navigate to a .pdf file and hit 'l', Acrobat (or whatever) will launch and show you the file. Now I never have to use the accursed Finder. Works on Mac OS X and Ubuntu; might need modifcations for other systems.


;;; Hack dired to launch files with 'l' key. Put this in your ~/.emacs file

(defun dired-launch-command ()
(interactive)
(dired-do-shell-command
(case system-type
(gnu/linux "gnome-open") ;right for gnome (ubuntu), not for other systems
(darwin "open"))
nil
(dired-get-marked-files t current-prefix-arg)))

(setq dired-load-hook
(lambda (&rest ignore)
(define-key dired-mode-map
"l" 'dired-launch-command)))

6 comments:

Sam said...

fantastic (why do you call it a hack?)
thanks

Anonymous said...

The word hack has multiple meanings:

- Refers to a bad author/writer.

- Breaking into computer networks.

- Something neat and clever. Usually (but not necessarily) programming related.

Alrabo said...

Hi, Do you have any suggestions on how to make it work for windows? I unfortunately find myself needing to open ".doc" MS files ... and would love to do it from dired. Thanks in advance if anyone has a tip on what to try.

vinhdizzo said...

I had something similar to this when on a Mac and it worked great! I used emacs as my file manager. Now that I'm on Linux, I would like to do the same but there is one issue. When we open a file (or multiple files) using your method, emacs is put on hold until we close the file (on my ubuntu). Anyway around this? I tried "see" but it doesn't always use the gnome default app. If emacs is put on hold then we can't use it as a file manager.

vinhdizzo said...

this function from my mac config works with see but does not with gnome-open:

(defun dired-do-shell-mac-open-vqn ()
(interactive)
(save-window-excursion
(dired-do-async-shell-command
"see" 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)


it has to do with gnome-open and xdg-open forking (or not forking) the process.

Koushik MS said...

Dude ! This is simply awesome. *You* are awesome. Thanks for this. I think this needs an entry in emacswiki.org