# In org-mode, a function to export to HTML file AND open?

In org-mode, if I do org-export-dispatch, then choose Export to HTML, I can then choose As HTML file and open. Four keystrokes though.

I'd like to reduce this to a single keybinding. However, I don't see a function to export to HTML file AND open the file in a browser. I see only the function org-html-export-to-html, which does not open the file.

How can I export to HTML and open the file with a single keybinding?

• As you say, C-c C-e h o the first time C-u C-c C-e the next time. You can see the underlying Emacs Lisp by searing for :menu-entry in ox-html.el. – rasmus Sep 14 '17 at 16:25

I don't think there's a direct function for this, but you could achieve what you want using org-export-to-file, the last argument of which is a "post-processing" step:

(org-export-to-file BACKEND FILE &optional ASYNC SUBTREEP VISIBLE-ONLY BODY-ONLY EXT-PLIST POST-PROCESS)


The post-processing step is handed the path to your new file; you could use something as straight-forward as find-file:

(defun org-html-export-to-html-and-open
(&optional async subtreep visible-only body-only ext-plist)
(interactive)
(let ((outfile (org-export-output-file-name ".html" subtreep)))
(org-export-to-file 'html outfile async subtreep visible-only body-only ext-plist #'find-file)))

(define-key org-mode-map (kbd "whatever you fancy") 'org-html-export-to-html-and-open)

• Thanks! This opens the .html file in Emacs. But the desired behavior is to launch it in a browser, which is what happens if I do org-export-dispatch, then choose Export to HTML, then choose As HTML file and open. – incandescentman Sep 13 '17 at 19:54
• Ah! I misunderstood. This part, then, will be somewhat context dependent. My approach would be to shell out to a command which will open the file (since you have the path on hand) -- on OSX/macOS you could use the open utility, for instance. Does that make sense? – Gastove Sep 14 '17 at 3:55
• what would that look like in the function? – incandescentman Sep 14 '17 at 4:35