How-to: highlight shortcodes in Emacs with markdown-mode



I’m pretty sure there are other Emacs users around. If you’re tired of shortcodes being quite ugly in markdown-mode, here is a simple way to improve this:

It allows you to fontify shortcodes, just like here:

You just need to add a few lines of Emacs Lisp to your Emacs config:

(defun schnouki/markdown-maybe-add-shortcode-keyword ()
  "Enable fontifying Hugo shortcodes if in the ~/blog/ directory."
  (when (string= (projectile-project-root) (expand-file-name "~/blog/"))
    (let ((shortcode-regexp
           (rx (group "{{" (or "<" "%") (1+ space))            ; opening {{< or {{%
               (group (1+ (not space)))                        ; shortcode name
               (group (*? any))                                ; parameters
               (group (1+ space) (? "/") (or ">" "%") "}}")))) ; closing >}}, %}}, />}} or /%}}
      (font-lock-add-keywords nil `((,shortcode-regexp . ((1 'markdown-markup-face)
                                                          (2 'markdown-metadata-key-face)
                                                          (3 'markdown-metadata-value-face)
                                                          (4 'markdown-markup-face))))))))

(add-hook 'markdown-mode-hook #'schnouki/markdown-maybe-add-shortcode-keyword)

If your blog is not in ~/blog/, or if you’re not using Projectile, you’ll need to edit the 3rd line so that it matches your install. Or you can just remove that line (and 1 parenthesis at the end of the function) to always enable that in markdown-mode :slight_smile:

Feel free to tell me if it helps, or if it doesn’t work, or to ask if you have any question!


I guess that makes us two Emacs users here :stuck_out_tongue:

My question though is that if you are an Emacs user, why would you edit in Markdown directly (when Org can export to Markdown).

Org natively supports below and much more!

#+include: "sim_root/tb/patterns/basic/" :src systemverilog :lines "14-37"


You don’t have to rely on Projectile. It should be safe to assume that the site is a git repo. So I do this in such cases:

(require 'vc-git) ;I think emacs 24+ is needed for this which isn't a difficult constraint
(vc-git-root default-directory)

If you are using emacs 25+, you can just use (project-current).


Because I’m more used to writing Markdown everywhere (including on my phone) than Org :slight_smile: Otherwise yeah it’s pretty obvious that Org can do much more…


Didn’t know about that, thanks for the tip!