A code snippet library
A library of code snippets, each a code block with its language, tagged #snippet and listed in a table. Find one again by any word in its code.
- Features
- Code blocks, Code Language…, the #snippet supertag, an Option field for Language, the Table view, and the palette
- Keys
- ⌥⌘C makes a code block. ⇧⌘T tags a note. ⌘K finds a snippet.
- Time
- About 15 minutes, in a new workspace
Start from an empty workspace
New workspace
Choose File ▸ New Workspace… (⇧⌘N) and make a new workspace in an empty folder.
Make a code block
In the Inbox, press Return and type three backticks followed by python, then press Space. The note becomes a Python code block.
Type the snippet
Type # Parse the export, press Return, and then type the code on the next lines. Return starts a new line inside the block. Keep the first line as a comment, so the note has a name the palette and the table can show.
Set the language
Choose the highlight
Click the language label on the block, or choose Format ▸ Code Language…. Type py in Find a language, then choose Python.
Keep a Language field too
The block's highlight is separate from a field. Use the field when you want to filter and sort snippets by language.
Tag the snippet
Make the supertag
Click the code block, then press ⇧⌘T, or choose Note ▸ Tags and Fields…. Choose Add supertag…, type snippet and choose Create #snippet. The block gets a # snippet chip.
Add a Language field
Choose Sidebar ▸ Supertags, then click snippet. Choose Note ▸ Add Field…, type Language, choose Option in the Type menu, and click Apply. Click Language on the Fields card and add the options Python, Swift, Shell and SQL, each followed by Return.
Set the value
Open the snippet, click its Language value, and choose Python.
The snippet after these steps
See the library as a table
Show the table
On the snippet page, choose View ▸ Show as Table. Each snippet is a row, with its first line as the name and a Language column.
Sort or filter
Click the Language header to group snippets by language. Type a word in the filter box to narrow the rows.
Find a snippet again
Search by a word
Press ⌘K and type a word from the code, such as rows. The palette lists the snippets that contain it, each with its location.
Narrow to snippets
Type #snippet and a space before the word, so the palette searches only the notes with that supertag.
Open it
Press Return to open the snippet as a page. Press ⌘Return in the palette to open it beside the page you are reading.
Copy the code
Click Copy on a code block. The code is copied as plain text, without colours or formatting, and the button reads Copied for a moment. The first line, your comment, is copied too.
In any note, type \code and press Space to make a code block there, which is useful for a snippet you write on the spot. See Insert commands.
What each feature does here
| Feature | Where you set it | What it does in this library |
|---|---|---|
| Code block | ⌥⌘C, or the typed fence | Keeps the lines, colours the code, and adds the Copy button. |
| Code Language… | Format menu | Sets the highlight for the block's language. |
| Supertag (#snippet) | ⇧⌘T, Add supertag… | Marks a note as a snippet, so the library and the palette chip find it. |
| Option field (Language) | Note ▸ Add Field…, Type Option | Holds the language, for sorting and filtering in the table. |
| Table view | View ▸ Show as Table | Lists every snippet, with its Language column. |
| Palette | ⌘K, with a #snippet chip | Finds a snippet by any word in its code. |
Make it yours
- Snippets by projectAdd a Project field to #snippet, with the projects as options, to group the library by where each snippet is used.
- A saved search for one languageMake a saved search with Supertag is snippet and Field Language is Python.
- Pinned libraryPin the snippet supertag, so its page is one click away in the sidebar.
- Snippets in code notesCopy a snippet with its Copy button, then paste it into another code block with ⌘V. Every line stays.