Introduction

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

The front matter for a post is something 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 is in IOS date format, and you can only specify year-month-day.

Initial snippet

It is 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 the following:

---
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 in the beginning of a line1 and press Tab, it will be expanded. You can jump forward and backward among the tabstops to fill the text.

This snippet is working great, but for the date field, we still need to fill the current date manually, which is not convenient enough.

Ultisnips command interpolation

Ultisnips also provides a powerful feature called interpolation, which means that you can run shell, Vim or Python command inside the snippet and use the returned output in snippet. The interpolation code are placed inside two backticks (`CODE`) and opens up many possibilities. For shell command interpolation, you can just write the shell command inside the backticks. For Vim and Python 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 interpolation, 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
    snip.rv=datetime.now().strftime("%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 command does not exist on Windows.

Vim interpolation

With Vim interpolation, the original meta snippet for Markdown 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 not desired in this case. 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 is 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 for once.

The new meta snippet for Markdown now becomes:

snippet "meta" "Markdown metadata front matter" b
---
title: "$1"
date: `!p from datetime import datetime
if not snip.c:
    snip.rv=datetime.now().strftime("%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.

References


  1. The b flag says that this snippet can only be triggered at the begining 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. ↩︎