What do you guys use to store and retrieve Snippets of code?

I am really curious about this because scripting often involves keeping track of various small pieces of code that do one thing that can be composed to make something more interesting. I know that @JMichaelTX uses Quiver and quite likes it but not really sure about others.

I myself still use SnippetsLab and I wrote about how I use the app here. In short I just create snippets for everything I do and prefix the names of the snippets according to language/framework of the snippet. And then I can access any snippet from my library in literally seconds.

I am really curious what @ccstone workflow is though because all of his scripts come with so much metadata, it's crazy. Like this one:

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/19 07:25
# dMod: 2017/11/19 07:30
# Appl: AppleScript + Satimage.osax
# Task: Extract values from text and sum them
# Libs: None
# Osax: Satimage.osax (MUST BE INSTALLED OR THE SCRIPT WON'T WORK!)
# Tags: @Applescript, @Script, @SIO, @Satimage.osax, @Extract, @Values, @Text, @Sum, @Them, @ccstone
------------------------------------------------------------------------------

No doubt that the creation of most of these entries is automated but how do you then query it?

I am curious about all other workflows and ideas how you guys maintain your own library of snippets though. Because maintaining a library of personal macros/actions is super easy with:

I personally like to able to paste several snippets at a time (a selection of generic functions, for example), which generally needs some sort of scriptability.

I happen to be using Quiver at the moment – its files have a very accessible JSON structure.

1 Like

Your thread is much like the one I started a couple of years ago:
What Do You Use for a Code Snippet Tool?

My last post there was:

While I still like Quiver, the limitations of its search (primarily no Spotlight search), have led me to another approach.

TLDR:
So I have evolved to now primarily using macOS files with Spotlight search. After more than a year with many requests by many users, the Quiver developer has not provided Spotlight search. So, I'm moving away from Quiver.


I have scripts stored in multiple locations:

1. My AppleScript Script Library (all of my core AS handlers)
2. My JXA Script Library (all of my core JS functions)
3. macOS Files
  * Scripts used with KM
  * "How To" and text scripts
4. Quiver (moving to files or text expander)
5. TextExpander (moving to Typinator)
6. Evernote (when I need extensive documentation to go with the script)
  * Often I will have a link to the script file / library
  * In script file header comments, I will have a link to the Evernote Note
  * Evernote is also searchable by Spotlight

Fortunately, `#1-3` and `#6` are searchable by Spotlight.  With these I make good use of text tags (like "@KM"), an idea I got from @ccstone.  These are simply one-word text keywords with a prefix of "@", put in both the file name, and in the script file as comments.  Using Spotlight to search for "@TagName" is very fast and effective, with very few (if any false positives).

I am also experimenting with use of actual macOS Tags.

I view snippets in two categories:

1. Complete handler/function
  * For scripts for my use, I use a ref to my script library
  * For scripts posted for others to use, I usually extract the handler and put in the same file as the main script.
2. Short blocks (often 1-line) of code to be inserted (with placeholders) into the script.
  * Stored in TextExpander (moving to Typinator)
  * Typinator has some great search and organizational tools.

I have an ongoing project to build a handler/function tool in KM, that will allow me to search my script libraries, and then extract either the call to the handler/function, OR the complete handler/function, to be inserted into my open script file.  This will use the great tool by @DanThomas [MACRO: Spotlight Search Prompt](https://forum.keyboardmaestro.com/t/macro-spotlight-search-prompt/4665) 

What I have learned over the last few years is that there is no one snippet manager that will meet all of my needs.
2 Likes

I use Bear.
Works well, syncs, supports tagging, etc.
It’s my go-to app for digital note taking, so it’s a natural fit for me.

The Bear web site promotes it as writing tool. Doesn't say anything about code snippets. Would you mind describing how your use Bear to manage code snippets? I'm particularly interested in how you search for snippets. Does it support Spotlight?

This screenshot probably best sums up how I use it.
I put a block of code (in this case creating a side-by-side figure in LaTeX), then the visualization or output for what the code does.


On the left is the hierarchical tag structure, which I really like. For instance, I have notes about the F2 area in monkey brains tagged with #monkey/f2, as seen in the screenshot.
To the right of the tags list is the notes list. As shown, I have four notes tagged #statistics.
The Bear developers are working on spotlight integration. Since my code snippets are mostly in Bear, I use its search feature. If they aren't there, I try spotlight or grep.

It occurred to me that people probably have different concepts of what constitutes a snippet. These notes are my conception and serve me well.

Thanks. That really helps.

That's good. I'll probably wait until the Spotlight integration is available.

So, if you need a snippet in your code editor, how do you find it and get it?
Is there any way to use Bear like a text expander, or a KM macro, that would let you search for the snippet, and then paste the selected snippet into your code editor?

Thanks for sharing. I still have not found my ultimate snippet manager, and I'm always on the lookout for something new.