Zen Coding in Visual Studio 2012

Zen Coding

Zen Coding is a faster way to write HTML using a CSS style selector syntax and you can now use Zen Coding in Visual Studio via the Web Essentials 2012 plug in (v1.7). Zen Coding was introduced by Sergey Chikuyonok in 2009 (according to Smashing Magazine) and has been updated over time to become a great way to write monotonous HTML much more efficiently.

Special thanks to Mads Kristensen and his team at Microsoft for adding Zen Coding to Visual Studio 2012 via Web Essentials 2012 (along with many other great features).

Quick Reference

Here is a quick list of the Zen Coding features that are now supported in Visual Studio 2012 via the Web Essentials 2012 plug in.

  • # creates an id attribute
  • . creates a class attribute
  • [ ] creates a custom attribute
  • > creates a child element
  • + creates a sibling element
  • ^ climbs up
  • * is element multiplication. This creates the same thing n number of times.
  • $ is replaced with an incremental number
  • $$ is used for numbers with padding
  • { } creates text in an element

What can you do? Here is an example:

  1. <!-- Type this -->  
  2. ul[data-bind="foreach:customers"]>li*4>span{Caption $$}+input[type=text data-bind="value:$$"]  
  3. <!-- Creates this -->  
  4. <ul data-bind="foreach:customers">  
  5.    <li><span>Caption 01</span><input type="text" value="" data-bind="value:01" /></li>  
  6.    <li><span>Caption 02</span><input type="text" value="" data-bind="value:02" /></li>  
  7.    <li><span>Caption 03</span><input type="text" value="" data-bind="value:03" /></li>  
  8.    <li><span>Caption 04</span><input type="text" value="" data-bind="value:04" /></li>  
  9. </ul> 

Let's take a closer look at the various symbols used.

ID and Class Attributes: # and .

You can create an element and assign it an id or class attribute using CSS-style syntax.

  1. <!-- Type this -->  
  2. div#contentRegion.address  
  3.   
  4. <!-- Creates this -->  
  5. <div id="contentRegion" class="address"></div> 

Custom Attributes: [ ]

You can create any attribute using the square bracket syntax.

  1. <!-- Type this -->  
  2. div[title]  
  3.   
  4. <!-- Creates this -->  
  5. <div title=""></div> 

Or create multiple attributes and fill in the values as in the following:

  1. <!-- Type this -->  
  2. input[placeholder="Name" type="text"]  
  3.   
  4. <!-- Creates this -->  
  5. <input type="text" value="" placeholder="Name" /> 

Child Elements: >

Create an element and then a child element inside of it. In this example I create a div with the id=menu that contains a span with a class=item and a blank title attribute.

  1. <!-- Type this -->  
  2. div#menu>span.item[title]  
  3.   
  4. <!-- Creates this -->  
  5. <div id="menu">  
  6.    <span class="item" title=""></span>  
  7. </div> 

Sibling Elements: +

You can create a sibling element easily too.

  1. <!-- Type this -->  
  2. footer>div>a+input  
  3.   
  4. <!-- Creates this -->  
  5. <footer>  
  6.    <div>  
  7.       <a href=""></a>  
  8.       <input type value="" />  
  9.    </div>  
  10. </footer> 

Climbing Elements: ^

The > operator descends into element hierarchy whereas the ^ climbs up the hierarchy. You can also climb multiple levels. For example, use 1 ^ to climb 1 level or use 4 ^ to climb 4 levels.

  1. <!-- Type this -->  
  2. footer>div>a+input^^p  
  3.   
  4. <!-- Creates this -->  
  5. <footer>  
  6.    <div>  
  7.       <a href=""></a>  
  8.       <input type value="" />  
  9.    </div>  
  10.    <p></p>  
  11. </footer> 

Multiplication: *

Create n number of elements.

  1. <!-- Type this -->  
  2. ul>li*4>span  
  3.   
  4. <!-- Creates this -->  
  5. <ul>  
  6.    <li><span></span></li>  
  7.    <li><span></span></li>  
  8.    <li><span></span></li>  
  9.    <li><span></span></li>  
  10. </ul> 

Item Numbering: $

When using multiplication to create n number of elements, you can add an incremental number to them using the $. Notice that using multiple $ operators (for example: $$) pads the numbers with 0's.

  1. <!-- Type this -->  
  2. section>article.item$$*4  
  3.   
  4. <!-- Creates this -->  
  5. <section>  
  6.    <article class="item01"></article>  
  7.    <article class="item02"></article>  
  8.    <article class="item03"></article>  
  9.    <article class="item04"></article>  
  10. </section> 

Text: } {

You can enter text values inside of elements, without changing the parent context.

  1. <!-- Type this -->  
  2. ul>li*4>span{Caption $$}  
  3.   
  4. <!-- Creates this -->  
  5. <ul>  
  6.    <li><span>Caption 01</span></li>  
  7.    <li><span>Caption 02</span></li>  
  8.    <li><span>Caption 03</span></li>  
  9.    <li><span>Caption 04</span></li>  
  10. </ul> 

This does not change the parent context, so when specifying the sibling to follow the text, the sibling element will actually follow the element prior to the text. That's why the example below creates an anchor tag next to the span tag.

  1. <!-- Type this -->  
  2. ul>li*4>span{Caption $$}+a{click me}  
  3.   
  4. <!-- Creates this -->  
  5. <ul>  
  6.    <li><span>Caption 01</span><a href="">click me</a></li>  
  7.    <li><span>Caption 02</span><a href="">click me</a></li>  
  8.    <li><span>Caption 03</span><a href="">click me</a></li>  
  9.    <li><span>Caption 04</span><a href="">click me</a></li>  
  10. </ul> 

Combining Them all

You can combine multiple features together that allow you to write some pretty cool HTML much faster. You can even use this to create some Knockout.js bindings for templates and then just change the property names.

  1. <!-- Type this -->  
  2. section[data-bind="foreach:customers"]>div*4>input[type="text" data-bind="text:$$"]  
  3. <!-- Creates this →  
  4.   
  5. <section data-bind="foreach:customers">  
  6.    <div>  
  7.       <input type="text" value="" data-bind="text:01" />  
  8.    </div>  
  9.    <div>  
  10.       <input type="text" value="" data-bind="text:02" />  
  11.    </div>  
  12.    <div>  
  13.       <input type="text" value="" data-bind="text:03" />  
  14.    </div>  
  15.    <div>  
  16.       <input type="text" value="" data-bind="text:04" />  
  17.    </div>  
  18. </section> 

Grouping: ( )

Grouping is a powerful feature of Zen Coding that allows you to create complex expressions. It is not yet in Web Essentials 2012, but I assume it will be in the near future. If it does arrive, you will be able to create entire sections of a DOM very easily.

  1. <!-- Type this -->  
  2. div>(header>div)+section>(ul>li*2>a)+footer>(div>span)  
  3.   
  4. <!-- WOULD create this (not yet supported in Web Essentials 2012)-->  
  5. <div>  
  6.    <header>  
  7.       <div></div>  
  8.    </header>  
  9.    <section>  
  10.       <ul>  
  11.          <li><a href=""></a></li>  
  12.          <li><a href=""></a></li>  
  13.       </ul>  
  14.    </section>  
  15.    <footer>  
  16.       <div>  
  17.          <span></span>  
  18.       </div>  
  19.    </footer>  
  20. </div> 

As you can see, this would make it quite simple to create large sections of HTML with just a few keystrokes.

Lorem Ipsum Generator


You can now generate Lorem Ipsum directly in the HTML editor. Type “lorem” and hit TAB and a 30 word Lorem Ipsum text is inserted. Type “lorem10″ and a 10 word Lorem Ipsum text is inserted.

ul>li*5>lorem3

Up Next
    Ebook Download
    View all
    Learn
    View all