Keep a code snippet library in Loam
Most developers keep snippets in a folder of gists, a pinned chat with themselves or a file called misc.txt. A Loam note does the job better. Code blocks are coloured for the language, tags group snippets by language or project, and search finds the one you wrote last spring. Here is a setup that takes a few minutes.
One snippet per note
Give each snippet its own note. The first line is the title, so name the snippet for what it does. Add a tag on the next line, a sentence about when you use it, and the code:
# Debounce in TypeScript
#snippets/typescript
Delay a call until the user stops typing.
```ts
export function debounce(fn: (...args: unknown[]) => void, wait = 250) {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: unknown[]) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), wait);
};
}
```
A code block starts with three backticks and a language word, and ends with three more backticks. Once the cursor leaves the block, the fence lines tuck away and a small label shows the language word you typed.
On a computer, you can also choose Code block in the Format menu, which shows its shortcut. For a short piece of code inside a sentence, wrap it in single backticks, or press ⌘E (Ctrl+E on Linux). On iPhone, iPad and Android, the Code button in the format bar above the keyboard makes inline code, and a code block is typed with the backticks.
Pick the language word
Loam reads the first word after the opening backticks and ignores its case. It matches that word against language names, common aliases and file extensions, so the spelling you already use usually works:
| You type | Loam colours it as |
|---|---|
py, python3 |
Python |
ts |
TypeScript |
go, golang |
Go |
sh, bash, zsh, console |
Shell |
rs |
Rust |
yml |
YAML |
jsonc |
JSON |
docker |
Dockerfile |
pwsh |
PowerShell |
psql |
PostgreSQL |
Write text, txt or output when a block holds a command’s output or anything else that shouldn’t be coloured. A word Loam doesn’t recognise also leaves the block plain. Extra words after the language, such as the title in a fence that reads ts title, are ignored.
What each device colours
The desktop and web apps colour more than 140 languages, and each grammar loads the first time a note uses it. The iPhone, iPad and Android apps carry a core set of 28:
C, C++, C#, CSS, Dart, diff, Dockerfile, Go, HTML, Java, JavaScript, JSON, JSX, Kotlin, Lua, Markdown, Python, Ruby, Rust, Scala, Shell, SQL, Swift, TOML, TSX, TypeScript, XML and YAML.
A block in another language, such as PHP or Haskell, shows as plain code on a phone and in colour on a computer. The text is the same on every device, so nothing is lost.
Paste code safely
Pasting into a code block keeps your text as it is. Loam changes only the line endings and removes invisible characters, such as the zero-width spaces that web pages sometimes hide inside code. Indentation and spacing are untouched.
Outside a code block, a paste gets light cleanup, such as turning • bullets into Markdown list items, so paste code into the block.
Group and find snippets
Tags do the sorting. Type #snippets/typescript and the note sits under snippets/typescript. The tag list lets you browse each level, so snippets shows every snippet and snippets/typescript only the TypeScript ones. Use the same idea for projects, such as #snippets/api.
Search looks through the text of every note, code included. Searching for debounce, a function name or a flag finds the note that contains it.
Link related snippets with [[ ]]. Type [[, keep typing a note’s name and press Return. A Docker cheatsheet note can link to [[Compose healthcheck]], and when you’re online the Compose healthcheck note’s Linked from list shows the cheatsheet.
Share a snippet
With Loam Pro on your account, you can invite someone to a note so they can read and comment on your snippets, and you can publish a note as a read-only web page. A published page shows the code coloured for its language. Pages are read-only snapshots, so publish again after you change the snippet.
Keep passwords, tokens and API keys out of snippet notes, especially notes you share or publish.
Everything else here is free, including code blocks, tags and search. Loam Pro adds sync across devices, collaboration, full version history, AI agents and publishing.
Related
Get new posts in your feed reader. Subscribe with RSS