How I Maintain a Code Snippet Library in Obsidian

Streamline your coding workflow by efficiently managing and storing code snippets for various programming languages within your Vault.

How I Maintain a Code Snippet Library in Obsidian

Since my Quick Capture article, I’ve slowly been expanding my Commander shortcuts to support repetitive tasks, one of which is quickly becoming increasingly more common: saving code snippets within my Vault.

In my day-to-day I’m often rotating between SQL, Python, Swift, and any other number of languages. There are always occasions where I’m either using previously written code, or reviewing code for a similar task, but to date haven't had a great system to store or recall them, so I created a template to do all that for me– and a little bit more.

Now, there are a ton of paid snippet managers out there (and of course, dozens of free alternatives), but I personally don't see a reason to use them– there simply isn't any functionality that would warrant the cost for me. In comparison, my desired workflow is quite simple:

  1. Use Commander to prompt me for the snippet.
  2. Capture additional metadata as Obsidian Properties using the same prompt.
  3. Store the newly generated note in a language specific folder. (i.e., SQL goes in SQL)
  4. Store the snippet within the note in a code block with the correct syntax highlighting, based on the language.

Let's break down each of these in turn to see how I achieved all that. You can also download the template below.

Getting Started

Configuring Templates and Commander

To ensure that the template functions correctly, create a Snippets folder in the root of your Vault. The naming convention and hierarchy is similar to my other templates: Quick Captures are stored in /Notes, and Syncs are stored in /Syncs. I'm not a complicated man.

Download the template below, and add the file to your Vault under /Templates

If you're going the Commander route (recommended) to trigger the template to run, you can follow along with the Quick Capture guide for how to add the button for the trigger onto your status bar. Once done, carry on below.

Now, regardless if you run the Template via the Command Palette or Commander, the template will still prompt you for a subject, the language of the code, the snippet itself, and any additional tags. This is because we're using Templater's built-in tp.system.prompt to have the prompt displayed.

Everything except the snippet are appended to Obsidian Properties. If you'd like to change the name of the properties, you can alter them within the frontmatter section of the template. Outside of the three mentioned above, we also store the Date and Time.

Change These to Alter the Properties

Generally, I recommend using the subject prompt to describe what the snippet does, and the tags for enhanced recall and filtering when searching for snippets within your vault. By default, the tag #snippet and the language selected will be prepended to any tags you provide yourself. Note that as of the current template, the subject is also used as the filename.

As for the language prompt, if your favorite language is missing, you can adjust the popularLanguages variable within the Template to include them. Make sure to use the same language format as what is listed here.

Add Languages Here!

The benefit of using this specific format is that the code block that contains the snippet within the note will be displayed along with the appropriate syntax highlighting. This is because the template will add the language from the prompt to the beginning of the code block, which enables it.

As for where the snippets are stored, we use the selected language once again to create the appropriate sub-folders (i.e., Snippets/python, Snippets/sql, etc).

If along the way you've made any changes to the template provided above, make sure to test it before moving on.

👉
Quick Tip: If an error occurs while testing, use the Inspector (⌘⌥+I) to display the Console logs.

Customization

Migrating My Preferred VSCode Icon Pack

For added customization, I’d recommend using a combination of the Iconize plugin in tandem with a custom icon pack in order to add language specific icons for the language sub-folders as defined above.

Iconize has some built-in repos, including those from Lucide, but those are monochromatic. Instead, I use Material Icons, which you can download from this GitHub Repo.

If successful, these icons will be imported into Iconize. After that it’s just a matter of right-clicking the individual folders in your sidebar and selecting your desired icon.

I'm Using Material Icons for CLI, Python, and SQL.

Choosing a Great Monospaced Font

All code blocks within Obsidian use a monospaced font by default. For added customization, I wanted to call attention to some new, and arguably, beautiful monospaced fonts made by GitHub Next.

Currently, I'm enjoying Monaspace Krypton quite a bit. It reminds me somewhat of vanilla World of Warcraft fonts and while it is definitely a bit “gamified”, it also just looks great:

Monaspace Krypton in Action

Regardless if you decide to give these fonts a go or not, you can always change the monospaced font under Settings → Appearance → Monospaced Font. You can even choose something distinctly not monospaced if you so desire. Keep in mind that this will apply to all of your code blocks and other in-line code throughout your entire Vault.

Sharing Snippets

Most snippet apps out there provide an out-of-the-box experience for sharing snippets. To do so in Obsidian, click the language on the top right of the code block. This will copy the snippet to your clipboard. Then it’s just a matter of pasting it somewhere. A useful, and perhaps not obvious implementation– but it gets the job done.

Click This to Copy

Note that this won't work if your cursor is actively within the code block, so make sure to click out of it for the language to appear.

Searching Snippets

Because of the structure I use to store snippets, searching through them is straightforward. I prepend any search with path:Snippets in order to narrow the search, then add additional strings or tags for further refinement.

For example: path:"Snippets", tag:#sql tag:#stakeholder which will provide me with all my SQL snippets for a specific stakeholder.

There are times where this filtering isn't necessary though, and just searching for a specific line of code is more than sufficient.

Discussion