Emacs Redux

Return to the Essence of Text Editing

Open File in External Program

| Comments

Sometimes it’s useful to be able to open the file you’re editing in Emacs in an external program. For instance - you might be editing some HTML file and you might want to see how is it looking in a browser. I use the following handy command to do so:

1
2
3
4
5
6
7
8
9
10
11
12
13
(defun prelude-open-with (arg)
  "Open visited file in default external program.

With a prefix ARG always prompt for command to use."
  (interactive "P")
  (when buffer-file-name
    (shell-command (concat
                    (cond
                     ((and (not arg) (eq system-type 'darwin)) "open")
                     ((and (not arg) (member system-type '(gnu gnu/linux gnu/kfreebsd))) "xdg-open")
                     (t (read-shell-command "Open current file with: ")))
                    " "
                    (shell-quote-argument buffer-file-name)))))

On OSX it will use the built-in open program to decide which program to open the file with. On Linux & *BSD it will use xdg-open. On all other operating systems you’ll be prompted to enter the name of the program in the minibuffer (with autocompletion for the program name). With a prefix argument you’ll always be prompted for the name of the program to use.

I find it convenient to bind the command to C-c o:

1
(global-set-key (kbd "C-c o") 'open-with)

This command is naturally part of Prelude(but is named prelude-open-with).

Comments