Currently, I write my blog posts in Markdown and build the blog using Hugo. Hugo supports front matter for Markdown so that you can attach metadata for a post, such as title, date, tags, categories, etc.

The front matter for a post looks like the following:

title: "Creating Markdown Front Matter with Ultisnips"
date: 2019-12-22 13:45:25+0800
tags: [Vim, Markdown]
categories: [Nvim]

The date field should be in ISO date format.

Initial snippet

It would be tedious to write the boilerplate text in front matter for every post I create. Ultisnips is good at automating such tasks. If you are not familiar with Ultisnips, you can check my previous post on setting up Ultisnips.

So the initial snippet I wrote looks like this:

snippet meta "Markdown front matter in YAML format" b
title: "${1:TITLE TEXT}"
date: $2
tags: [$3]
categories: [$4]

When we type the trigger word meta at the beginning of a line1 and press Tab, it will be expanded. We can jump forward and backward among the tabstops to fill the text.

This snippet works great. However, for the date field, we still need to fill the current date manually, which is not ideal enough.

Ultisnips command interpolation

Ultisnips also provides a powerful feature called interpolation, which means that you can run shell, Vim or Python command inside a snippet and use the output in snippet. The interpolation code are placed inside two backticks (`CODE`) and opens up many possibilities. For shell command interpolation, you just write the shell command inside the backticks. For Vim and Python code interpolation, you should follow the opening backtick with !v and !p respectively.

For example, to get the current date in ISO format, i.e., 2019-12-22 14:43:43+0800, using the above three interpolations, the code is like:

  • shell interpolation: `date "+%Y-%m-%d %H:%M:%S%z"`
  • Vim interpolation: `!v strftime("%Y-%m-%d %H:%M:%S%z")`
  • Python interpolation:
    `!p from datetime import datetime"%Y-%m-%d %H:%M:%S%z")`

Back to the above snippet, if we want the interpolation code to be cross-platform, we should only consider Vim or Python interpolation since most Linux shell commands are not available on Windows.

Vim interpolation

With Vim interpolation, the original meta snippet becomes:

snippet "meta(data)?" "Markdown metadata front matter" br
title: "$1"
date: `!v strftime("%Y-%m-%d %H:%M:%S%z")`
tags: [$2]
categories: [$3]

One minor issue with this snippet is that the date value keeps changing while we are inside the snippet region2, which is a little annoying. According to tutorial here, we can use Python interpolation to deal with this issue.

Python interpolation

Inside the Python interpolation code, Ultisnips provides the snip object. The attribute snip.c will be empty once the interpolation has finished. snip.rv is the result for the snip interpolation.

We can check if snip.c is empty to fix the date once the interpolation code is run.

The new meta snippet now becomes:

snippet "meta" "Markdown metadata front matter" b
title: "$1"
date: `!p from datetime import datetime
if not snip.c:"%Y-%m-%d %H:%M:%S%z")`
tags: [$2]
categories: [$3]

Now the date field will be fixed while we are editing other parts of the snippet.


  1. The b flag says that this snippet can only be triggered at the beginning of a line. ↩︎

  2. Because inside the snippet region, the interpolation code is run in real time and the result is updated to reflect the change. ↩︎