TIL: How to Add Hugo Shortcode Snippets in VSCode
Hugo shortcodes are a great way to add functionality to our Hugo blog. However, I find them fiddly to add.
So let’s see how we can leverage VSCode snippets to make it easier to add shortcodes to our markdown files.
First, open the command palette, then select Snippets: Configure User Snippets
. In my case, I want to add snippets
specific to this project so I select New snippets file for '<project>'
. Give it a name like shortcodes
.
This will create a new file in .vscode/shortcodes.code-snippets
.
Then I will update the file to make it look like this:
{
"invidious": {
"prefix": "invidious",
"body": ["{{< invidious ${1:link} >}}"],
"description": "invidious Hugo shortcode"
},
"notice": {
"prefix": "notice",
"body": [
"{{< notice type=\"${1:type}\" title=\"${3:title}\" >}}",
"${4:content}",
"{{< notice >}}",
"",
"$0"
],
"description": "notice Hugo shortcode"
}
}
Which could then easily be turned into this:
{{< notice type="warning" title="warning" >}}
This is a warning
{{< /notice >}}
{{< invidious rALo_BzGKs8 >}}
To add them to our markdown files we just need to type the prefix i.e. notice
and then press the auto-complete button i.e. tab
.
However, we may need to turn on autocomplete for markdown files in our settings.json
:
{
"[markdown]": {
"editor.quickSuggestions": {
"other": "on",
"comments": "off",
"strings": "off"
}
}
}
π That’s it! You should be able to use the code snippets more easily add shortcodes!
(Optional) Extensions
I also use the following extensions with VSCode (specifically for Hugo):
Hugo Language and Syntax Support
: Adds snippets for builtin Hugo shortcodes i.e.highlight
Hugo Shortcode Syntax Highlighting
: Adds syntax highlighting for shortcodes in markdown files