Can VS Code Do Emmet?

As in, does Visual Studio Code, the free code editor from Microsoft, work with Emmet, the free and open source code expansion tool? The answer is of course! In fact, you don't have to do anything at all to get it going. Emmet is built-in to VS Code.

Let's take a look at what Emmet can do and some VS Code specific stuff to make the most of it.

In this article, I'll use ⌘ to denote  the command key on Apple and the control key on Windows. I'll also use ⇧  for the shift key.

What Is Emmet?

Emmet is a code expansion tool that is designed to dramatically speed up the creation of HTML and CSS.

It works like this. Say you wanted to create a div. Normally, you would type out each character:

< ... d ... i ... v ... >

Your text editor might even help autocomplete that tag name for you. Then to close it, you might only have to type a new angle bracket (<) and it will offer to complete the closing tag.

VS code closing the div tag

This is not a bad way to compose HTML, but when you're working in HTML, you're probably writing many more tags than that, which means that you're opening and closing a lot of angle brackets. Those angle brackets can really slow you down and make the creation of HTML tedious.

This is where Emmet comes in.

Emmet allows you to simply type div and hit the tab key. It is automatically expanded out into a full HTML div tag.

Automatically expanded

This works for any HTML tag. You can add an id with a #, a class with a ., and attributes with [foo]. It's happy to do multiple of each!

Using any html tag

In fact, if you are working with a div, you don't even need to specify the div tag. You can just start with the id or class and you will get a div by default. Emmet is even smart enough to change that to a <span> if you are within another inline-by-default element.

Quite complex markup can be composed this way. You can use > to specify going down a level and + to specify items at the same level. Take for example a Bootstrap Navbar. The markup a navbar might look like this:

<nav class="navbar">
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="navbar-nav">
    <a class="nav-item nav-link" href="#">Home</a>
    <a class="nav-item nav-link" href="#">Features</a>
    <a class="nav-item nav-link" href="#">Pricing</a>
  </div>
</nav>

We can generate this whole block with Emmet like this:

There's a lot going on there in terms of Emmet syntax. The best way to learn it is to grab the Emmet Syntax Cheat Sheet.

You can also incorporate Lorem Ipsum into the generated markup as needed.

You probably noticed that I was able to auto-generate 3 anchor tags and that when I expanded all of it out into HTML, I got tab stops on all my anchor tags and href attributes. This allows you to quickly create a block of HTML and then go and add specifics to each item.

There is another  ( dare I say cooler) way to do this though 😎. It's called "Wrap Individual Lines".

Wrap Individual Lines With Abbreviation

If we back up to the example above, what we really want is a navbar with Home, Features, and Pricing links. We can add that text after we generate the markup with Emmet, or we can do it before using the "Wrap Individual Lines With Abbreviation" command.

Invoke the command pallet (⌘ + ⇧ + P) and select "Wrap Individual Lines". We enter the exact same Emmet syntax that we had before, but this time we stop at the * as this is where Emmet will insert our lines.

There is also a "Wrap With Abbreviation" that will wrap a single line without having to select anything. This is particularly useful when wrapping links. Simple paste a URL in, invoke the command, hit "a" and then hit enter.

🔥 Hot Tip

Map a keyboard shortcut to the "Wrap With Abbreviation" command. This makes it super quick to create things like anchors.

Emmet In Other File Types

By default, Emmet works on HTML and CSS — and that includes preprocessors/template languages. Out of the box, you get support in html, haml, jade, slim, jsx, xml, xsl, css, scss, sass, less and stylus. The docs also mention that you get it in any file type that inherits from the above file types — such as php.

CSS

Emmet also works in CSS. It works with a search technique called Fuzzy search which matches what you are trying to express with few characters. For example df expands into display: flex; and w100p expands in width: 100%;.

These work in different CSS files types, like Sass, where it knows not to insert semicolons in the `.sass` syntax.

File type inheritence

Unfortunately, there is no way to know which files inherit from an existing language type. For instance, Emmet does not work in Markdown files because Markdown does not inherit from any of the known types. Even though HTML is perfectly valid in Markdown files.

To fix this, we can add a language mapping so Emmet knows to behave properly in Markdown files. Add the following line to the User Settings file.

To jump to User Settings quickly, press ⌘, (command comma).

"emmet.includeLanguages": {  
    "markdown": "html"
}

Now we've mapped Markdown to HTML so Emmet knows how to treat the file. Well, almost. If we open up a Markdown file, you'll notice that Emmet is working, but not like we would expect. If I type div and press tab, nothing happens. If I type div and then an *, Emmet tries to kick in via the suggestions menu.

That's not good because Markdown uses asterisks for headers. Emmet is going to intercept those every time. The reason this is happening is that VS Code is trying to prevent the tab key from clobbering some other function that the tab key might serve in a document of this type.

To get around this, we can add the following line to our User Settings:

"emmet.triggerExpansionOnTab": true

Now the div expansion will work on tab, but the asterisks still fire the Emmet suggestion window. My suggestion is to turn that window off completely with the following line in User Settings:

"emmet.showExpandedAbbreviation": "never"

This also works for languages like React and Vue. While Emmet works by default in JSX files, we can make it work in JSX in regular JavaScript too. Add this mapping to your User Settings:

"emmet.includeLanguages": {
    "javascript": "javascriptreact"
}

Creating Your Own Snippets

You can also create your own Emmet snippets.

For example, you could expand Emmet's SVG capability by creating a custom snippet like rect that maps to rect[x][y][width][height] and thus expand into:

<rect x="" y="" width="" height=""></rect>

More information about that can be found in the official VS Code documentation. This is how CodePen custom snippets work under the hood as well!

More Emmet Resources

If you'd like to learn more about Emmet, here are some other resources for getting started: