Emacs Lisp: opening media files straight from GNU Emacs Dired mode by using xdg-open

Emacs Lisp: opening media files straight from GNU Emacs Dired mode by using xdg-open

This is my personal GNU Emacs Dired setup on the V key that helps me to quickly open media files or external viewers by using xdg-open and EMMS the Emacs Multimedia System.

Simply bind the ā€œVā€ key to this function and then within Dired you use V to view the file. It will remap the Dired-x function which I never used. This way I can easily open videos and other files by using xdg-open and if I wish to use GNU Emacs built-in functions to view the file I still have the lowercase v for that.

(defun rcd/dired-view ()
  "View files, either as HTML or media"
  (interactive)
  (let* ((files (dired-get-marked-files))
     (how-many (length files))
     (extensions (mapcar 'file-name-extension files))
     (extensions (mapcar 'downcase extensions)))
    (cond ((member "html" extensions) (eww-open-file (car files)))
      ((member "mp4" extensions) (emms-play-dired))
      ((member "mp3" extensions) (emms-play-dired))
      ((member "ogg" extensions) (emms-play-dired))
      (t (if (> how-many 1) (xdg-open-files files)
           (xdg-open (car files) t))))))

(defun xdg-open (file &optional async)
  "Opens file with xdg-open. Without optional argument ASYNC, it will wait for the file to finish playing or review."
  (let ((command (format "xdg-open '%s'" file)))
    (if async
    (async-shell-command command)
      (shell-command command))))

(defun xdg-open-files (files)
  "Opens list of files with xdg-open one by one, waiting for each to finish."
  (dolist (file files)
    (xdg-open file)))

;; Finally mapping the key V to dired-mode-map
(define-key dired-mode-map "V" 'rcd/dired-view)

There are many ways to customize GNU Emacs and dired.

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: