Snippets can add a touch of magic to your editor. It’s like an incantation. Utter a short phrase (type a prefix), wave your wand (press Enter
or Tab
), and presto! A wonderful event unfolds in front of you. ✨
Most code editors support snippets out of the box. The code editor I will use to showcase snippets is Visual Studio Code (VS Code), the most popular editor of the day.
Also, there are some “text expander” apps that allow you to use snippets globally (across all apps). I will briefly cover how you can utilise these apps to get even more out of snippets.
Let’s dive into all things snippets. 🤿
A snippet is a template that can be inserted into a document. It is inserted by a command or through some trigger text.
With snippets, you can create a boilerplate file, and insert commonly used blocks of text. The general idea is to save you typing out the same things completely again and again, and again. 🤖
I’m not going to shock you with this statement: the internet is home to a lot conflicting opinions! Snippets do not escape this ignominy, but I don’t think it is a topic that make people’s blood pressure soar!
For the sake of balance, I will present a cross-section of those opinions here.
You don’t have to pick a camp and be all-for or all-against snippets. I suggest you adopt them to a degree that serves you best.
Donald Knuth, one of the grand-wizards of computer science, said “premature optimization [of code] is the root of all evil”.
I think this is relevant to snippets also. Snippets are an optimization of code production. If you don’t know a language or framework very well, implementing a slew of snippets for that language or framework is likely to be a premature move.
If you are comfortable, then try some out!
Personally, I use snippets often but judiciously. I use a set of snippets for Markdown and most of the languages I work with.
I haven’t used snippets much for frameworks. I started using some snippets for Vue recently, but I only use the boilerplate snippet. I will probably adopt more of them once my Vue IQ has risen.
I haven’t used snippets for algorithms.
Snippets can be classified according to the scope of interactivity between the snippet and the editor.
You can think of it as a copy-and-paste of some source text as a single command.
A dynamic snippet can be customised to provide a wizard-like experience for completion of a snippet.
It can include:
Here is an example of a markdown snippet which adds a task list with 2 tasks. It uses tab stops, placeholders, and choices for checking a task.
#vscode #programming #developer