Supercharge Your Web Dev Workflow With Emmet
Tags: productivity, tooling, tutorial
Published: 2022-12-29 10:00:00

[Emmet](https://emmet.io/) is a tool that allows you to quickly generate HTML and CSS code by using abbreviations and expanding them into full code.

It is built into many popular code editors, including [Visual Studio Code](https://code.visualstudio.com/docs/editor/emmet), [Sublime Text](https://github.com/emmetio/sublime-text-plugin) and [Atom](https://github.com/emmetio/emmet-atom).

Today, let's take a look at how to use Emmet to boost your web development speed. I'll use VSCode as my editor, but feel free to pick up your favorite. You'll need to check out editor-specific docs of Emmet for the configuration part of the post if you choose something other than VSCode, but that should not be a problem for you to follow along.

## Table of Contents

- [Example Usage for HTML](#example-usage-for-html)
  * [Represent relationships](#represent-relationships)
  * [Represent attributes](#represent-attributes)
  * [Represent Repeating Snippets](#represent-repeating-snippets)
- [Example Usage for CSS](#example-usage-for-css)
- [Advanced Configuration](#advanced-configuration)
- [Conclusion](#conclusion)

---

## Example Usage for HTML

The best part of coding HTML involves writing (deeply) nested structures with attributes such as classes and ids attached to tags. Emmet can help us generating highly complicated HTML structures with a single line of code.

To achieve that, Emmet use concise syntax to represent:

- Relationships between tags
- Attributes attached to tags
- Repeating snippets

### Represent relationships

One of the most common use cases of Emmet is to generate HTML tag hierarchy. Here is a quick example:


![Basic example of using Emmet to generate HTML tag hierarchy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93zpq68f23qsheevceg5.gif)

<figcaption>Emmet generating nested tags with > mark</figcaption>

We use `>` to represent parent-child relationship in Emmet.  To represent sibling-sibling relationship, we use `+`:

![Use + to generate sibling tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vju11hn86hkiy9elij2.gif)

<figcaption>Emmet generating sibling tags with + mark</figcaption>

We can even _climb up_ from current level to upper level using `^`:


![Climb up to upper level with ^ mark](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xw9m9r2txh3av4hbmmbw.gif)

<figcaption>Climb up to upper level with ^ mark</figcaption>

Here, we generated a `p` with two `span`s inside and a `ul` at the same level as the `p`.

Please note we can also achieve the same result by using _grouping_:


![Same result by using grouping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qq6kmgszrgv1o4pk1mc.gif)

<figcaption>Same result by using grouping</figcaption>

We grouped the `p` and its children in a pair of parentheses and replaced climb-up mark (`^`) with sibling mark (`+`) for `ul` (it seems same structure can still be correctly generated even if you don't replace `^` with `+` here, but for semantic reasons, I think `+` is more appropriate in this case).

### Represent attributes

HTML tags usually have attributes such as classes and ids. In Emmet, we use `.classname` and `#id` to represent classes and ids respectively:

![Specify class with dot and id with sharp mark](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a734pjsz92zijhczqjb0.gif)

<figcaption>Specify class with dot and id with sharp mark</figcaption>

It's as easy as that. For abbrevations for other attributes, check out the [official Emmet cheat sheet](https://docs.emmet.io/cheat-sheet/).

### Represent Repeating Snippets

Repeating structures usually take the most time to write and fortunately with Emmet, such snippets cannot be generated more easily:

![Generate repeating tags with asterisk mark](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po662zotaavu70rvf8hj.gif)

<figcaption>Generate repeating tags with asterisk mark</figcaption>

To generate auto-incrementing ids, for example, we can combine asterisk with `$` (dollar sign):

![Generate auto-incrementing ids with * and $](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8dw0j3c1o7eyde5x64j6.gif)

<figcaption>Generate auto-incrementing ids with * and $</figcaption>

Here is a more advanced example demonstrating auto-incrementing ids along with auto-incrementing _text_:

![Generate auto-incrementing ids and text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44jx7wfbvhk2bktc766j.gif)

<figcaption>Generate auto-incrementing ids and text</figcaption>

---

## Example Usage for CSS

Emmet makes coding with CSS just as easy as HTML. Each CSS rule has corresponding abbreviations and once you understand the patterns of abbreviations, you don't need to remember them:

![Generating CSS code is as easy using Emmet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pal2vwb7ke4z3xkg4ax6.gif)

<figcaption>Generating CSS code is as easy using Emmet</figcaption>

Please note that,

- I wrote `db`, for example, instead of `d:b` as the [cheat sheet](https://docs.emmet.io/cheat-sheet/) suggests. It seems either way works, although it's not clear why. So choose the convention that suits you best.
- While the cheat sheet does not mention, you can expand shorthand CSS rules, e.g. `margin: 10px auto` with `m10:a` or `m:10:a` as I did in the screenshot above.

---

## Advanced Configuration

While Emmet is available by default for common file types such as html, slim, css and sass, you'll need to manually enable it if you're working on a different file type where Emmet is not activated by default.

For example, Emmet is not enabled by default for Ruby on Rails view files with the `.erb` extension. If I want to write HTML code in these files, I can update the `emmet.includeLanguages` setting in my VSCode's `settings.json` file like this:

```json
{
  "emmet.includeLanguages": {
        "erb": "html",
  },
}
```

The key (`"erb"`) on the left side is the **target file type** where I want Emmet to be available while the value (`"html"`) is a **file type that Emmet already supports**. See [VSCode docs on this](https://code.visualstudio.com/docs/editor/emmet#_emmet-abbreviations-in-other-file-types) for more information.  

---

## Conclusion

In this post, we took a quick look at some of the most common scenarios where Emmet can help us write HTML and CSS code times faster.

As I said, there's more Emmet can do for us than I demonstrated here. It has an amazing, super comprehensive [cheat sheet](https://docs.emmet.io/cheat-sheet/) which you can use as a reference.