Thursday, July 16, 2009

Opening Files from the Command Line via the Default Application

Files can be opened from the command line as if they were double clicked.

  1. On Windows, simply typing the name of the file should work IIRC.
  2. On OS X, there is the open command.
  3. On KDE and Gnome, there are kde-open and gnome-open respectively.

I have created a function in my .bashrc, so that I can open files by the command o. Just add it to ~/.bashrc and source .bashrc or reboot(!)

function o
{
  kde-open "$@" &>/dev/null &             
}

This sends all stdout and stderr to outer space in order to save you an Enter press, so beware.

Bonus: Actually, all this was done so that I can open the files in dired mode in emacs by their default application, pdf, jpg, mp3 files etc.

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

Now in dired mode, pressing 'o' will open the file in the default application.

See here if that doesn't work.

No comments: