Emacs: Open File in External App

By Xah Lee. Date: . Last updated: .

Open File in External App

Here's a emacs command to open the current file or marked dired files in external app. (as if you double-clicked the file on desktop) It's useful for image files, PDF file, video, audio files.

Put this in your emacs init file.

(defun xah-open-in-external-app ()
  "Open the current file or dired marked files in external app.
The app is chosen from your OS's preference.
URL `http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html'
Version 2016-10-15"
  (let* (
          (if (string-equal major-mode "dired-mode")
            (list (buffer-file-name))))
         ($do-it-p (if (<= (length $file-list) 5)
                     (y-or-n-p "Open more than 5 files? "))))
    (when $do-it-p
       ((string-equal system-type "windows-nt")
         (lambda ($fpath)
           (w32-shell-execute "open" (replace-regexp-in-string "/" "\\" $fpath t t))) $file-list))
       ((string-equal system-type "darwin")
         (lambda ($fpath)
            (concat "open " (shell-quote-argument $fpath))))  $file-list))
       ((string-equal system-type "gnu/linux")
         (lambda ($fpath) (let ((process-connection-type nil))
                            (start-process "" nil "xdg-open" $fpath))) $file-list))))))

Open File in Desktop

The following shows the file in desktop folder viewer. (Microsoft Windows Explorer, Mac's Finder, Linux's file manager.)

(defun xah-open-in-desktop ()
  "Show current file in desktop (OS's file manager).
URL `http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html'
Version 2015-11-30"
   ((string-equal system-type "windows-nt")
    (w32-shell-execute "explore" (replace-regexp-in-string "/" "\\" default-directory t t)))
   ((string-equal system-type "darwin") (shell-command "open ."))
   ((string-equal system-type "gnu/linux")
    (let (
          (process-connection-type nil)
          (openFileProgram (if (file-exists-p "/usr/bin/gvfs-open")
      (start-process "" nil openFileProgram "."))
    ;; (shell-command "xdg-open .") ;; 2013-02-10 this sometimes froze emacs till the folder is closed. For example: with nautilus

You can give it a hotkey, see: Emacs: How to Define Keys.

Open in Terminal

Here's a command to open the current file's directory in terminal.

(defun xah-open-in-terminal ()
  "Open the current dir in a new terminal window.
URL `http://ergoemacs.org/emacs/emacs_dired_open_file_in_ext_apps.html'
Version 2015-12-10"
   ((string-equal system-type "windows-nt")
    (message "Microsoft Windows not supported. File a bug report or pull request."))
   ((string-equal system-type "darwin")
    (message "Mac not supported. File a bug report or pull request."))
   ((string-equal system-type "gnu/linux")
    (let ((process-connection-type nil))
      (start-process "" nil "x-terminal-emulator"
                     (concat "--working-directory=" default-directory) )))))

Note: emacs has many shells and terminal emulators. 〔►see Emacs: Run Shell in Emacs〕 But sometimes, it is necessary to run some command in dedicated terminal app.

〔►see Emacs: Difference between shell, term, eshell

Current File Commands

  1. Emacs: Open File Path Under Cursor
  2. Emacs: Copy File Path of Current Buffer
  3. Emacs: Run Current File
  4. Emacs: Delete Current File
  5. Emacs: Backup Current File
  6. Emacs: Batch Convert Line Ending in Dired
  7. Emacs: Open File in External App

Dired topics

  1. Emacs: File Manager, dired
  2. Emacs: Dired Customization
  3. Emacs: Find Replace Text in Directory
  4. Emacs: Batch Rename Files
  5. Emacs: Dired Sort File by Time, Size
  6. Emacs: Open File in External App
  7. Emacs: Rename File from Space to Hyphen/Underscore
  8. Emacs: Convert Image Files in Dired
Like it? Buy Xah Emacs Tutorial. Thanks.