Boosting your VSCode productivity using Emmet Expressions
What’s one of the most common things every developer does? Yeah, you got it, writing code. We spend most of our time typing; we write "poems" to our machines so they can run and execute programmed tasks for our application users; we make our user's life easier by automating repetitive tasks or processes, sometimes at the cost of developer happiness or comfort to create such applications.
IDEs are one of the main tools developers use at a daily basis, and it offers powerful features that increase their productivity; from IntelliSense providing code completion, parameter information, inline documentation, code snippets, and usage examples. This code completion is based on language semantics, and analysis coming from the source code.
VSCode is one of the most popular IDEs, it’s free and provides an amazing development environment experience, let's learn how you can be more productive writing HTML and CSS using an amazing tool called Emmet. If you're a front end developer or just hack your own language with Emmet, you will find this article really useful.
Usually, when writing repetitive code, the underlying programming language offers built-in mechanisms, like functions, so you can encapsulate them and call them every time you need them, from the tooling perspective, IDE offers code blocks in the form of snippets, so you don’t need to write over and over again the same things, let’s see an example: In VSCode, there are built-in code snippets you can use to prevent typing blocks of code (conditionals, loops, try-catch statements); they’re pretty common expressions. You can use them by typing and pressing the tab key or using light bulb suggestions.
There are more productivity tools available, but most are around code suggestions in their different flavors: refactoring, code completion, AI suggestions, etc.
One of the most underrated features VSCode provides is Emmet expressions, they are like the missing piece in terms of developer productivity, and as you will see, it can save you tons of time when writing code.
Emmet expressions take Web developers' code writing experience to the next level. Stop for a minute reading this article, and write the following markup in VSCode: Five siblings divs, each of them with a child span with the value Item 1, Item 2, and so, where the div number corresponds to the corresponding number of the div, in the end, you will end up with something like:
Now you did it. How did you do it? You probably defined the first div, copy-pasted the other items, and then replaced the corresponding item numbers. Did you ever wonder if there is a better way to do such a mundane task? Entering Emmet Expressions to the rescue!
In the same HTML file you created the previous example, type the following:
Awesome, right? That’s an Emmet expression, already supported by VSCode by default. You don’t need to install any extension. Think about them as dynamic snippets defined at runtime instead of having them previously defined
Syntax
Writing Emmet expressions will feel similar to how CSS selectors work in the context of the position of the elements and their attributes.
Elements are usually HTML tags, but you can even define your tags with Emmet.
If you have the suggestion documentation fly-out enabled, you will see a preview of the result of the Emmet expression. To enable it, just click the information icon. The preview will be enabled by default after that
Nesting operators
Nesting operators are used to position abbreviation elements inside generated tree: whether it should be placed inside or near the context element.
Read more at the official docsChild
Nest elements inside each other
Sibling
Place elements near each other, on the same level
Climb-up
Climb one level up the tree and change context where following elements should appear
Climb-up n levels
Climb n levels up the tree and change context where following elements should appear
Multiplication
Define how many times element should be outputted
Multiplication (define digits)
Define how many digits and times element should be outputted
Multiplication (define start number)
Define starting number and times element should be outputted
Multiplication (descending order)
Define times element should be outputted in descending order
Numbering
With multiplication * operator you can repeat elements, but with $ you can number them. Place $ operator inside element’s name, attribute’s name or attribute’s value to output current number of repeated element
Numbering (define text placeholder)
With multiplication * operator you can repeat elements, but with $ you can number them. Place $ operator inside element’s name, attribute’s name or attribute’s value to output current number of repeated element
Text
Use curly braces to add text to element
Grouping
Grouping subtrees in complex abbreviations
Grouping
Multiple grouping subtrees in complex abbreviations
HTML5
Alias of html:5