The letter A styled as Alchemists logo. lchemists Syndication Icon

Putin's War on Ukraine - Watch President Zelenskyy's speech and help Ukraine fight against the senseless cruelty of a dictator!

Published December 1, 2021 Updated May 22, 2022
Cover
Alfred Snippets

Alfred is one of those must have tools every engineer needs for improved productivity. There is so much this one application provides that I definitely don’t have time to explain in one article alone. Instead, I want to focus on Alfred’s Snippets feature.

To provide more context, Alfred Snippets allow you to expand common text, phrases, templates, and so forth with only a few keystrokes. If you use Sublime Text Snippets or even the custom code snippets in the Sublime Text Setup project, you’ll be at home with Alfred Snippets. Alfred Snippets is one my most heavily used features and even my usage statistic support this claim. The following was captured just before the Native American Heritage Day holiday:

Usage

The blue line, regarding expansion usage, is what you want to pay attention to. I definitely use Alfred a lot. So let’s dive into how you can leverage this feature and improve your workflow too! 🎉

Setup

To get started, you’ll want to follow these steps:

  1. Download and install Alfred.

  2. Buy the Powerpack to enable the Snippets feature.

  3. Launch and open Alfred by navigating to Features → Snippets.

💡 Alternatively — also my favorite way — you can access your snippets by popping up Alfred via your global hotkey (i.e. COMMAND + SPACE), typing ?snippets, and ENTER.

Usage

While viewing your snippet collections, you can add new collections or delete existing collections at any time by clicking on either the plus (+) or minus (-) symbols at the bottom of the Snippets window. Here’s a screenshot of what creating a new collection looks like:

Cover

…​and here’s what an existing collection looks like:

Cover

In my case, you are looking at my Markdown collection which has a semicolon prefix and icon. The icon is important for visually distinguishing between all of your collections. As for the prefix, I’ll expand upon this shortly.

Next, I will provide some customization tips which might be of interest. You can also read the official documentation for further details.

Customization

Out of the box, the defaults are pretty good but you might want to make a few tweaks. For example, here is a screenshot of what my snippets configuration looks like:

Cover

I’ll use on my Markdown collection for demonstration purposes to keep things simple. I’ve also added numbered stamps (blue background with white numbers) to walk you through each section of the above screenshot.

(1) Snippet Keyword

The default snippets keyword is snip but I like to use s to search through my snippets. The s also doubles as my default Web Search trigger which happens to be Duck Duck Go and is why Duck Duck Go appears at the top of the list. By using s, especially for searching snippets, this allows me to type COMMAND + SPACE to launch Alfred and then type s + SPACE to search. Example:

Cover

All of my snippets use a semicolon prefix. I’ll explain in more detail later but, for now, notice how nice it is to search through existing snippets. Hitting the ENTER key on any selected snippet will trigger the snippet.

(2) Matching

When not using the above snippet keyword, I use the Name, Keyword, or Snippet option to trigger a snippet. This is how I almost always trigger a snippet. The most important part is ensuring this selection is word based so Alfred expands the snippet once the full word-based macro is typed out to properly trigger the corresponding snippet.

(3) Automatically Expand Snippets By Keyword

You definitely want this option checked in order to teach Alfred to trigger snippets within any macOS application. This is unchecked by default because you need to enable macOS Accessibility for Alfred first but is definitely worth enabling. You can do this by launching System Preferences, clicking on Security & Privacy, and then clicking Accessibility. You’ll want to make sure Alfred is checked as shown below:

Usage

(4) Collections

My Markdown collection — including all other collections — uses a semicolon prefix (;) to trigger each snippet. I keep dancing around why the semicolon is important and promise to explain why shortly.

(5) Snippets

Here we can see the details of my Markdown collection in terms of snippet name, whether snippets are enabled or not, the keyword macro used to trigger each snippet, and what the snippet will output when triggered. If we double click on one of these, like my Markdown Link for instance, we can then view the details of this particular snippet:

Usage

I use the {cursor} placeholder often because it’s quite handy to have my cursor placed exactly where I need it to fill in additional information. Should you want something more than just a cursor placeholder, you can insert dynamic placeholders within your snippets too. Here’s a brief look at what is possible:

Usage

Guidelines

Now that you’ve got a rough sense of how to get setup and customized with Alfred, let’s detail a few guidelines worth following when building your own custom snippets. The trick to making Alfred Snippets powerful is to:

  • Use semicolons as your prefix for all snippets to prevent accidental snippet expansion.

  • Use single or double (rare) characters for namespaces.

  • Use mnemonic macros with as few characters as possible while remaining memorable, much like you’d do with your Dotfiles aliases and functions.

Let’s break the above down further.

Semicolons

For example purposes, I’ll focus on the English language but the pattern can be applied to any language as best makes sense. With English, at least, there is never a sentence that begins with a semicolon: ;. Even better, a semicolon doesn’t require a modifier key (i.e. COMMAND, CONTROL, OPTION, etc.) to type. So you can use your right pinky finger to type the semicolon with minimal effort.

For example, let’s say you are writing a Markdown document and want to include a Ruby code block. I would then type following key sequence:

; + m + d + b

…​to yield the following snippet:

``` ruby
```

What’s nice about the above is that I’m using the semicolon (;) to denote the start of my snippet followed by the m and d keys to denote the [m]ark[d]own namespace finally followed by the b key which is short for [code] block. When all of these keys are used in combination, my snippet is triggered which results in my Markdown code block being expanded. I can then fill in actual Ruby code for documentation purposes to complete my snippet.

The important aspect of this design is that using a semicolon to denote a snippet macro (i.e. ;mdb) is invalid English. The possibility of ;mdb ever showing up as a word or naturally in a sentence is zero. This also means you won’t cause a snippet to accidentally trigger and expand.

Namespaces

Now that you know to use semicolons as snippet prefixes, the next thing you want to do is namespace your snippet collections. With my Markdown collection, I use md as the namespace. This helps organize and ensure each snippet collection is unique to prevent any namespace collisions. The good news is when Alfred detects a snippet collision, you’ll see the errant snippet displayed as red text which is helpful for debugging purposes.

Not all snippets need to be namespaced, though. For example, when it comes to symbols, I’ll use words instead of namespacing:

  • ;apple - 

  • ;copyright - ©

  • ;trademark - ™

As mentioned earlier, the semicolon prefix and unique trigger is all that matters. If you can use a namespace, then do it. Otherwise, it can make sense to use only unique words too.

Mnemonics

By this point you are starting to see how I build mnemonics into all snippets. You can put a lot of power into triggering your snippets with only a few key strokes. While I’ve only shown the use of ;md for Markdown snippets, here are a few more examples:

  • ;g - Git.

  • ;h - HTML.

  • ;sr - Slack Reactions.

  • ;ss - Slack Statuses.

Returning to Markdown, this means you can leverage the ;md namespace to start organizing your snippets as follows:

  • ;mda - Markdown Action. Expands to - [] to create a next action (i.e. TODO) list.

  • ;mdc - Markdown Code. Expands to `{cursor}` to create inline code.

Hopefully this helps demonstrate how one can quickly build up simple mnemonics that are easy to remember and use with minimal keystrokes.

Downloads

The following are several of my custom snippets which you can download and install for yourself. Each demonstrates what I’ve discussed above. Plus you can use these as examples to incorporate within your workflow directly or experiment further. Follow these steps to use any of these snippets:

  1. Click on each link to download or open within Alfred.

  2. Once downloaded, double click on the file to install into Alfred.

  3. You can delete the downloaded file once installed.

You can always view any installed snippet by popping up Alfred and typing: ?snippets plus ENTER. Feel free pick and choose or download and use all of the following snippets:

  • ASCII Doc - Useful when writing ASCII Doc documents.

  • Code Review - Provides code review templates and utilities.

  • Credit Cards - Useful when filling in valid or invalid VISA cards for testing purposes.

  • DateTime - Useful when needing different date/time formats.

  • Emoji - Useful with services that can translate text to emoji.

  • Git - Provides common Git related messages.

  • HTML - Useful when working in HTML.

  • Markdown - Useful when writing Markdown documents.

  • Slack - Useful when communicating with others in Slack.

  • Symbols - Useful when needing common symbols in chat or documentation.

  • Text - Useful when dealing with text.

Conclusion

I hope the following has been of help to you and will level you up further. Whenever you find yourself repeating the same text over and over, think about crafting a snippet and save yourself some time. You’ll be glad you did.