It’s time to revisit a subject I covered in 2014, using Emacs to open a Markdown file in Marked, an excellent Markdown previewer for OS X.1 I have used two different methods of opening files in Marked from Emacs, both of which have respective advantages.
The first is a function expressly for opening files in Marked. Add the following code to your emacs init file, and then press “Control-c m” to open the current file in Marked.
(defun markdown-preview-file ()n "run Marked on the current file and revert the buffer" (interactive)n (shell-commandn (format "open -a /Applications/Marked 2.app %s" (shell-quote-argument (buffer-file-name))))n )nn(global-set-key "C-cm" 'markdown-preview-file)
The second is a function for opening files in the default application for that type of file. This is part of Bozhidar Batsov’s Emacs Prelude starter kit, which I highly recommend. Again, add the following to your init file:
(defun open-with (arg)n "Open visited file in default external program.When in dired mode, open file under the cursor.With a prefix ARG always prompt for command to use." (interactive "P")n (let* ((current-file-namen (if (eq major-mode 'dired-mode)n (dired-get-file-for-visit)n buffer-file-name))n (open (pcase system-typen (`darwin "open")n ((or `gnu `gnu/linux `gnu/kfreebsd) "xdg-open")))n (program (if (or arg (not open))n (read-shell-command "Open current file with: ")n open)))n (start-process "prelude-open-with-process" nil program current-file-name)))nn(global-set-key (kbd "C-c o") 'open-with)
The second method is what I currently use. “Control-c o” opens the current file in the default application, which is set in the system. In order for it to work with Markdown files, you’ll need to make Marked the default application for Markdown files, which means that to open a file for editing from the Finder, you’ll need to right-click and select “Open With.” The advantage is that you can use the same command from Dired to open PDF’s in your preferred PDF reader, Word docs in Word, etc.
- Its excellence lies partly in the fact that it’s not simply a previewer, it also does many other things, like word count, reading analyses, and link validation. [return]