Working With Code Snippets In Sublime Text

Along the entire process of coding, developers (including me) tend to rewrite or reuse the same pieces of code over and over again. One way to eliminate this repetitive process is to keep codes we frequently use closeby in the form of snippets. This makes it easy to retrieve and access them easily.

To help make your coding more efficient, Sublime Text (code editor) provides developers with the ability to add custom-code snippets in the editor with ease. In this post, we will see how to create, manage, and insert code snippets to significantly streamline our workflow in Sublime Text.

Let’s have a look.

Creating New Snippet

To create a new snippet, we go to Tools > New Snippet.

Working With Code Snippets In Sublime Text_第1张图片

Sublime Text will then deliver the following template to set the new snippet.

view plain copy to clipboard print ?
  1. <snippet>  
  2.     <content><![CDATA[ 
  3. Hello, ${1:this} is a ${2:snippet}. 
  4. ]]></content>  
  5.     <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->  
  6.     <!-- <tabTrigger>hello</tabTrigger> -->  
  7.     <!-- Optional: Set a scope to limit where the snippet will trigger -->  
  8.     <!-- <scope>source.python</scope> -->  
  9. </snippet>  

We simply add the code snippet within <![CDATA[ ]]> in <content> element, for example:

view plain copy to clipboard print ?
  1. <content><![CDATA[ 
  2.     -webkit-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.5); 
  3.     -moz-box-shadow:    1px 1px 1px 0px rgba(0, 0, 0, 0.5); 
  4.     box-shadow:         1px 1px 1px 0px rgba(0, 0, 0, 0.5); 
  5. ]]></content>  

Then, we set the trigger keyword within <tabTrigger> for Sublime Text to autocomplete the snippet. The name of the trigger should be easily to remember, yet closely corresponds to the snippet. In this example, we set the trigger name with the keyword shadow.

view plain copy to clipboard print ?
  1. <tabTrigger>shadow</tabTrigger>  

Lastly, we set the scope in which this snippet can be added. Since we added CSS3 Box Shadow, we might want to limit the usage for this snippet to only in a CSS stylesheet.

view plain copy to clipboard print ?
  1. <scope>source.css</scope>  

At this point, we are all set, and we can save the snippet. To make our snippets organized, we will create a new folder dedicated to saving CSS. Further, you might want to create more folders for saving the other types of codes, such HTML, JavaScripts, PHP, Python, and so on.

Working With Code Snippets In Sublime Text_第2张图片

One thing must be noted here: the files must be saved within a .sublime-snippet extension, otherwise Sublime Text will not recognize it as a snippet.

Insert The Snippet

In the editor, Sublime Text will show the options as we type the trigger keyword. We can insert the snippet by selecting it from the options or completing the trigger keyword and hitting the Tab key.

As we have specified the scope to document with CSS syntax, this snippet will not be triggered and automatically added in other types of document, as shown below.

Tab Key And Fields Markers

Furthermore, there are times when we need to edit particular specifications within the code snippets. Given the example of our CSS3 box shadow above, we might want to change the color of the shadow, the shadow blur, or the horizontal offset.

Sublime Text also provides the ability to highlight specific fields, which we want to edit, after inserting the code snippet. These fields can be specified with $ sign followed with { } and the order of the field, as follows.

  1. -webkit-box-shadow: ${1:1px} ${2:1px} ${3:1px} ${4:0px} ${5:rgba(0, 0, 0, 0.5)};  
  2. -moz-box-shadow: ${1:1px} ${2:1px} ${3:1px} ${4:0px} ${5:rgba(0, 0, 0, 0.5)};  
  3. box-shadow: ${1:1px} ${2:1px} ${3:1px} ${4:0px} ${5:rgba(0, 0, 0, 0.5)};  

In the example above, the first highlighted field would be ${1:1px}, and to the next following few fields within the order as we hit the Tab key. In the following screenshot, notice how the first values of the box-shadow property, including the vendor prefixes, are all highlighted together.

Working With Code Snippets In Sublime Text_第3张图片

Sublime Text has multiple-cursor feature, which allow us to select or highlight several lines at once and make editing much faster. Then, hit the Tab to highlight and edit the field number ${2:1px}and the next following order.

Installing Snippet Package

You don’t have to do everything on your own. There are several packages that we can install for almost any type of code snippets.

Assuming that you have install the Package Control, you can hit Shift + Command + P, then go to Package Control and search for the snippet package.

In the following example, I find that the package for SASS code snippet is available.

Even the snippet for Code License is also available.

Working With Code Snippets In Sublime Text_第4张图片

Find and install the code snippets you need, and you are all set. You don’t have to memorize the entire trigger keyword for the snippets as Sublime Text will show you the options as you are typing in the codes.

Working With Code Snippets In Sublime Text_第5张图片

That’s it for now, we hope this little tip can be useful to enhance your workflow on writing codes. Happy Coding!

Further Resource

For further reference on creating and customizing the code snippet, you can head over to the following page.

你可能感兴趣的:(code,workflow,command,webkit,source,extension)