Obsidian is quickly becoming the note manager of choice for many people. It’s ease of use and rich support for plugins makes it one of the best options for building a “second brain”.
As a developer, I often find neat code snippets that I can use in other projects. But as a human with the memory of a gold fish, I usually forget those cool code snippets. That’s where Obsidian comes in.
The Plugins
I use three main plugins for this system; Dataview, Templater, and QuickAdd.
I use Dataview to query data stored in the frontmatter of the file. Frontmatter is written in YAML format. It is where you can store metadata about the file such as tags, date created, and anything else you want.
Templater, as the name suggests, helps create templates for you. In the front matter of my template note I have specific syntax that will become the current date and the “last updated” date.
And finally I have QuickAdd that can create a macro that goes into your command palette. It will take your template file and then create note based off of it in a separate folder.
The Workflow
The entire process starts with the template note. This is what mine looks like:
It starts with a list of tags. There is only one tag in most, but it’s easy to add extra tags like this:
tags: ["cards/snippets", "anotherTag", "yetAnotherTag"]
Tip: if you add a /
like I did in mine, Obsidian will treat it kind of like a folder structure. So there is the parent tag cards
and then there is the child tag snippets
. You can query all the cards
tags or limit the search to cards/snippets
.
The next section is where you put the language of your snippet. It can be whatever. I have a snippet meant for the Minecraft command line. For that language I just put “Minecraft”.
After the language section is the two date sections with the Templater syntax that will output the creation date and the last modified date.
Below the front matter is a link to a file I call Code Snippets. We will get to that in a bit.
The file it self is pretty empty and is open to how you want to configure it.
For the QuickAdd plugin, go to your settings and scroll down until you see the QuickAdd secition under Community Plugins (if you have it installed). You should see an input box that says “Name”. Enter in the name you want to give your command. I just used “Add Code Snippet”.
Make sure the setting next to it is on “Template” and then click “Add Choice”. You will see your new template added to your macro list. On the right side you should see a lightning bolt. Click that. With that enabled you will be able to use “ctrl + p” (or whatever hotkey you have for the command palette) to open the command palette and search for the macro.
But before you do that, click the gear icon next to the lightning bolt to open up the macro configuration.
Where ever you placed your template file, add that path to the “Template Path”. It should auto suggest a path if you added a template path in the main QuickAdd settings.
After that you can select the settings that make sense to you and your workflow. I have mine set up so that it will create the new file in a separate folder called Cards/Code Snippets
. It will also open the file and focus on the new pane.
When you use the command palette to generate a new file like this it will prompt you for a file name unless you specify a file name format. After choosing a name you can begin editing your new code snippet file and adding all the useful information that you need.
It’s pretty easy to keep track of a few of these code snippets, but if you’re like me then you’ll start building a large collection. This is where Dataview comes in.
Dataview allows you to query files based on their content, oftentimes the frontmatter. I will only be going over some basic configuration of Dataview in this post, but I highly suggest learning more about it.
If you remember from before, the template had a link to a file called “Code Snippets”. This is where I placed my Dataview query. This is what I use:
```dataview
TABLE WITHOUT ID
file.link AS "Snippet",
language AS "Language"
FROM #cards/snippets AND -"Extras"
```
This will create a table without the file name (`WITHOUT ID`). Instead, we list out the file links under the column named “Snippet”. Then the language is listed.
After defining the table layout, we have to specify where we are grabbing the files. In this case we are telling Dataview to display every file tagged “Cards/snippets” minus any files from the “Extras” folder, which is where my template is stored.
This way Dataview will make a table of all the code snippets you have without displaying the template we made beforehand.
You can further narrow this query down by telling it to only display snippets from a certain language. Of course this method of managing snippets is highly customizable. You can add more front matter details and change the query type.
I hope this post helps you with your second brain journey!