Skip to main content

Zen Coding: Using Sublime Text (emmet plug-in)


Assuming that all of us installed zen code plugin if not please install it.
Here we're going to show you some frequently used useful tips & tricks of zen-coding with respect to HTML

type html:5 in your editor & hit tab  (@HIT-TAB@)
Let's see the different basic rules to create well formatted html one by one...

-- 1. To create the element
Write the element name and hit "TAB"
e.g. (div & hit TAB) and emmet will create this for you
   div@HIT-TAB@ => <div></div>
   p@HIT-TAB@ => <p></p>



-- 2. To create element with identifier
  Let's say we want to create "div having id as myId"
  We can use "#" sign like this

 div#myId@HIT-TAB@  => <div id="myId"></div>
  
-- 3. To create element with class name
  If we want to create "div with class name myClass"
  we can use "." sign like this

  div.myClass@HIT-TAB@   => <div class="myClass"></div>

  div.myClass1.myClass2.myClass3@HIT-TAB@  => <div class="myClass1 myClass2 myClass3"></div>

  we can combine identifier with class name like this

  div#myId.myClass1.myClass2@HIT-TAB@  => <div id="myId" class="myClass1 myClass2"></div>

-- 4. To create child element
   We can use ">" between the elements
   Let's say we want to create "li" inside "ul" tag
 --------------------------------------------------------------
Desired html structure
   <ul>
                <li></li>
    </ul>

ZenCode: ul>li@HIT-TAB@
  --------------------------------------------------------------
   if we want 5 "li" tag inside "ul" just use "*" sign

Desired html structure
   <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
    </ul>
ZenCode: ul>li*5@HIT-TAB@

  Some more examples:
    div>p@HIT-TAB@
    div#footer>p>span@HIT-TAB@



-- 5 For sibling element
  We can use "+" sign for sibling elements

Desired html structure:
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div> 

ZenCode: div#header+div#content+div#footer@HIT-TAB@

-- 6. If you want to dynamically number the item
 take the help of "$" sign

 "element$*count"

Desired html structure
 <ul id="nav">
                <li class="item-1"></li>
                <li class="item-2"></li>
                <li class="item-3"></li>
                <li class="item-4"></li>
                <li class="item-5"></li>
 </ul>

 Zencode: ul#nav>li.item-$*5@HIT-TAB@;


-- 7 To move one element up   (^) use caret sign

  Desired html structure

  <div><span id="spanId" class="spanClass"></span></div>
  <div><span></span></div>

  ZenCode: div>span#spanId.spanClass^div>span@HIT-TAB@


-- 8. If we're not writing any "html element" then emmet will treat it as "div"

 .myClass>header>li   <=> div.myClass>header>li

 if we're creating "li" tag inside "ul"
 and in this case we can ignore writing "li" tag, emmet will create it for you
  ---------------------------------------------------------------------
Desired html code
 <ul>
                <li class="myLiClass-1"></li>
                <li class="myLiClass-2"></li>
 </ul>

 ZenCode:  ul>.myLiClass-$*2@HIT-TAB@
 ---------------------------------------------------------------------
Desired html structure
<div class="myClass">
                <header>
                                <h1></h1>
                </header>
                <div class="main"></div>
                <footer></footer>
</div>

ZenCode: .myClass>header>h1^.main+footer
 ---------------------------------------------------------------------
   
-- 9. To set attributes [attr="attrValue"]
     To write some text inside tab {some text}

Desired html
     <ul>
                <li class="foo"><a href="#">some text</a></li>
                <li class="foo"><a href="#">some text</a></li>
                <li class="foo"><a href="#">some text</a></li>
     </ul>
ZenCode: ul>.foo*3>a[href="#"]{some text}
  
-- 10. To get text on demand

Type "lorem" and put the number just after it, it will create 5 words for you

lorem5  => Lorem ipsum dolor sit amet.

if we're using "*N" sign then it’ll create N number of <div>s with some dummy records

Below line will create 5 div with some dummy text

ZenCode
lorem*3  

Html structure
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere, animi illo aliquam debitis culpa aspernatur ratione similique repellendus velit facilis dolor molestiae quasi quaerat quam at sed sit minima porro.</div>
                                <div>Ab, ipsum, nostrum, sapiente illum commodi sit nesciunt blanditiis maiores harum doloremque maxime deleniti dicta nihil. Omnis, corporis, nostrum commodi eaque doloribus itaque dolorum ipsam dignissimos laboriosam quisquam iusto amet.</div>
                                <div>Distinctio, quidem, architecto, rerum, illum debitis provident fugiat numquam voluptate laboriosam assumenda explicabo delectus veniam adipisci repellat labore cupiditate sed. Distinctio, quos, rerum sunt unde officiis et repudiandae voluptatem nihil!</div>

  
11. Use of ZenCode as on time calculator

We can use ZenCode to do dome simple mathematical calculation

12/3*4  “now press “Ctrl + Shift + Y” and it will get converted into 16



Now we've good understanding of zen coding let’s try this zen code and see the result

html>head>title^body>div.header>ul#nav>li.item-$*4>a[href="#"]{Item $}^^^div.main>div(lorem*3)^div#footer>p>span


NOTE: All of the above code tested in "Sublime Text 3"
             Similar tips & tricks are available for writing "CSS" using zen-coding.

References:
1.       http://docs.emmet.io/cheat-sheet/

Comments

Popular posts from this blog

EFCore - Collate function

Search in SQL server query is generally case insensitive (by default or based on database level collation). Suppose we have an employees table with a row having first-name column value as "My-First-Name", so if we want to do the case-sensitive search we have to explicitly use the related collate: In EF 5 (currently in Release Candidate version [RC.2.20475.6]) Collate function got introduced which helps us to use our specific collation based search.  C# with EF5 code sample: var employeeCaseSensitiveSearch = _dbContext.Employees .Where(x => EF.Functions.Collate(x.FirstName, "Latin1_General_CS_AS") == "my-first-name") .FirstOrDefault(); A related database query will be something like this: T-SQL: Case sensitive search (use specific collation e.g.: Latin1_General_CS_AS) SELECT * FROM dbo.Employees AS e WHERE e.FirstName Collate Latin1_General_CS_AS = 'my-first-name' Some of the useful CSharp function which g...

EFCore - Parallel db call on same dbContext

Practically there are multiple instances when we want to do parallel DB call which will save us some time. Like if we want to get a few employees details as well as users detail and if both entities are independent of each other better to go with a parallel call to fetch the details.  With the growing ORM usage and enhanced & optimized framework of like EFCore, in most of the application (related to C# development), we generally prefer to use EFCore. With EFCore if we try to use the same DB context to do parallel db call we are getting an error (... different thread using the same DbContext....). Because dbContext call is not thread-safe Parallel DB call on the same dbContext:  Code snipped: which will throw an error private void DbCallWithParallelCallOnSameDbContext() { try { var employeesTask = _dbContext.Employees.ToListAsync(); var usersTask = _dbContext.Users.ToListAsync(); ...

How to install Zen-coding plugin

As a web-developer, irrespective of the technologies (java, c-sharp, python, php e.t.c.…), we used to write CSS code to make our web-pages looks good if not at least we’re involved in write html codes. What if there is some tool to whom you give some instruction and that tool generates a good, well formatted html tags for you. These kind of coding is possible and known as “ Zen coding ” and there are lots of plug-in available from different vendors. We’re going to  discuss the steps to install this “zen-coding” plugin for “visual studio, eclipse, sublime-text & notepad++ in next few lines. Follow the below steps to install "zen-coding" plugin based on your editor.  Steps to install zen-coding plugin for (visual studio, eclipse, sublime-text,notepad++) -- -- For Visual Studio 1. Go to "Tools" -> "Extensions and Updates" 2. It'll open the "Extensions and Updates windows"    Select online form Left hand menu items ent...