Zen Coding for Visual Studio 2012

Don’t wait, head over to VS extensions gallery and grab hold of Web Essentials 2012 by Mads Kristensen. The extension offers good amount of features for VS based developers, of all of them the one I am going to talk about is the Zen Coding style introduced by Sergey Chikuyonok (as mentioned in JohnPapa.net).

Before I proceed further Zen Coding style has been implemented across various text editors, and if you are a full time or regular web developer you should have a look into it. For all the supported text editors check out the Wikipedia entry at http://en.wikipedia.org/wiki/Zen_Coding#Text_editors.

Once you have installed Web Essentials 2012, you will have support for coding in Zen Coding style development inside of Visual Studio 2012. The main idea behind the coding style is to reduce the manual typing process of creating the mundane html code for your web application. It is kind of text expansion mechanism, but it is much more than that.

Lets start with few examples:

1. Create a normal div tag

<!-- When you type -->
div <!-- Press TAB -->

<!-- created content is -->

as simple as that. In the above example itself you have skipped over typing all the angle brackets and the end tag of the div.

2. Create a list of items / multiplication * and item numbering $

<!-- When you type -->
ul>li{Item $$}*3

<!-- created content is -->
	<li>Item 01</li>
	<li>Item 02</li>
	<li>Item 03</li>

Check out that you can repeat items using the syntax li*3 which simply creates that many items and you can specify what you would want to go inside the tag by using the curly braces. So li{hi} would create:


Also note that you can access the current index of the element using $ symbol, and when you use $$ it does the padding for you.

3. Provide id #, class . and custom attributes []

<!-- When you type -->
div.row>div.twelve.columns>div#header>h1[style="text-decoration: underline"]{User Management}

<!-- created content is -->
<div class="row">
	<div class="twelve columns">
		<div id="header">
			<h1 style="text-decoration: underline">User Management</h1>

4. Sibling elements: +

I am sure by now you understand that > creates child elements, similarly for creating siblings you can use the + element

<!-- When you type -->

<!-- created content is -->
<a href="mycompany.com">
	<img src="logo.png" alt="" /><div>

5. Climbing elements: ^

<!-- When you type -->
p>div.header{Header goes here}+div.body{Body text}>div.footnote^a[href="link"]{myLink}

<!-- created content is -->
	<div class="header">
		Header goes here
	<div class="body">
		Body text<div class="footnote"></div>
	<a href="link">myLink</a>

6. Lorem Ipsum generator lorem3

<!-- When you type -->

<!-- created content is -->
	<li>Lorem ipsum dolor.</li>
	<li>Sit amet, consectetur.</li>
	<li>Adipiscing elit fusce.</li>
	<li>Vel sapien elit.</li>
	<li>In malesuada semper.</li>

Unfortunately, as of Web Essentials 2012 v2.3.1 grouping is not supported yet. So you may want to write to Kris about the same and request for the feature.

Overall Zen Coding style saves a lot of mundane typing that comes with html file development, and I hope this helps you as much as it has helped me.

Do check out Web Essentials as it is much more than an extension which supports Zen Coding style.

Published by

Vijay shankar ganesh K

Analyst, developer and all around code tweaker from the sunny side of the world. Checkout my Google plus profile and get in touch.

Leave a Reply