Markdown and code snippet management
I’ve been trying out and letting go of code snippet managers since the early days of Code Collector Pro. I think I’ve tried just about every solution out there (I think Quiver and SnippetsLab are excellent applications), but I always come back to keeping all of my snippets in a folder of Markdown files.
Portability, Portability, Portability
Code is always plain text, why would I want to put it into a database format to work with it?
Here’s the thing: this folder full of Markdown files has been ported to every snippet manager I’ve tried, either by direct import or via a script I create to read them in and write them to the snippet app’s format. As plain text, they’re portable and highly flexible.
Storing everything in Markdown has never bitten me. Storing everything in a snippet app’s database format has bitten me almost every time. Rarely do snippet apps offer the ability to import from other snippet managers, and even when their storage format is simple JSON or XML, it still takes some scripting to get field names and formatting switched over. It’s easier to have a simple format like Markdown that I can just write out to the new format when needed.
Granted, most people won’t switch apps as often as I have, but the fact that it’s a pain in the butt to do so illustrates the benefit of keeping everything in as close to a universal format as possible. Which, for me, is always plain text.
By the way, if you ever need to convert a bunch of Markdown files to Quiver’s JSON format, let me know, I have just the script for you.
nvALT/Ultra
The whole point of nvALT (and nvUltra, if you’re lucky enough to be in the beta) is that you have all of your notes stored as plain text, easily searched and edited. It’s always been as fast or faster for me to search my snippets in nvALT than to go through whatever snippet app I’m trying at any given time. With a little tagging and consistent naming conventions I never can’t find the snippet I’m looking for. And with Markdown formatting I can include all the notes I need to right in with the code.
In nvALT I relied entirely on tags and filenames to separate by language and category, and nvALT’s tag search is, well, not great. I also kept all of my snippets in the same folder as my other notes, distinguishing them with a snippet
tag. In nvUltra (which I promise even those not in the beta will see soon) I have a dedicated notebook for snippets, I can use subfolders in addition to tags to group code (both in metadata and macOS tags on the files, synced together), and nvUltra has superior tag searching and filtering (including AND/OR boolean combinations).
A Note on File Names
I tend to give my snippets descriptive file names, and include the language as an additional extension. For example, “Launch App with arguments.m.md” would signify that it’s an Objective-C snippet that, well, you get the idea.
If the snippet came from StackExchange or a blog post, I’ll usually include the surrounding text as context. The code itself gets separated from context using Markdown formatting, either with indentation or backtick fences. Files that contain multiple snippets get ATX (#) headers that describe each snippet. This is useful when parsing the files for import to other apps, or for use with tools like…
Snibbets for your Snippets
The flexibility and scriptability of this system is demonstrated in a tool I created called Snibbets. It’s a CLI (with accompanying LaunchBar Action) that lets me quickly search for code snippets by filename and content. It’s not overly developed at this point, a simple combination of find
and grep
with a parser that will extract code blocks automatically. But it’s a good demo. If you’re interested, I made a repository for it: github.com/ttscoff/Snibbets.
This is an extension to nvUltra for me; I still edit (and search) my snippets in nvUltra. The nvUltra beta even has a new feature that allows tab completion of subfolders when creating a new file, so my snippets notebook can have language-specific subfolders to which I can easily assign new snippets. But outside of nvUltra I still have easy access, with the ability to redirect snippets and clipboard contents straight from the terminal to text files, and to write as many little scripts as I want to work with them.
As much as I adore well-written apps that serve a purpose perfectly (and again, I hold many of the existing dedicated snippet managers in high regard), I love plain text more. And nvALT/Ultra make working with them a breeze (with room for DIY extensions!).