Streamlining Your Workflow with Code Snippets in Sublime Text

As developers, we often find ourselves reusing the same pieces of code multiple times. One efficient way to avoid this repetition is by storing frequently used code in snippets. This method allows for quick retrieval and easy access.

For those looking to optimize their coding process, Sublime Text, a popular code editor, offers a convenient feature for adding custom code snippets. In this article, we’ll explore how to create, manage, and utilize code snippets to enhance your workflow in Sublime Text.

Let’s dive in.

10 Best Free Source Code Editors

10 Best Free Source Code Editors

With professional code editor like Dreamweaver, Coda, Textmate and others, its no surprise that more and more people... Read more

How to Create a New Snippet in Sublime Text

To kickstart the process of creating a new snippet, navigate to Tools > New Snippet in the menu.

New Snippet in Sublime Text

Sublime Text will present you with a template to configure your new snippet.

<snippet>
<content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!-- <tabTrigger>hello</tabTrigger> -->
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>

Insert your desired code within the <![CDATA[ ]]> tags in the <content> element. For instance:

<content><![CDATA[
-webkit-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.5);
box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.5);
]]></content>

Next, specify the trigger keyword within the <tabTrigger> tag. This keyword should be easy to remember and relevant to the snippet. In this example, we use the keyword shadow.

<tabTrigger>shadow</tabTrigger>

Finally, define the scope for the snippet. For instance, if the snippet is for a CSS3 Box Shadow, you might want to restrict its usage to CSS files only.

<scope>source.css</scope>

Once everything is set up, save your snippet. To keep things organized, consider creating a dedicated folder for CSS snippets. You can also create additional folders for other languages like HTML, JavaScript, PHP, Python, etc.

Save Snippet in Sublime Text

Remember, the snippet files must have a .sublime-snippet extension for Sublime Text to recognize them.

How to Insert Your Snippet

Within the editor, Sublime Text will display snippet options as you type the trigger keyword. You can insert your snippet either by selecting it from the dropdown or by completing the trigger keyword and pressing the Tab key.

Insert Snippet in Sublime Text

As specified, the snippet will only trigger in documents with the defined scope, ensuring it won’t appear in unrelated file types.

Snippet Scope in Sublime Text

Using Tab Keys and Field Markers in Sublime Text Snippets

At times, you may need to modify specific attributes within your code snippets, such as the color or blur of a CSS3 box shadow. Sublime Text offers a feature that highlights these customizable fields after inserting a snippet.

You can specify these fields using a $ sign followed by { } and a field order number, as shown below:

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

For example, the first field to be highlighted would be ${1:1px}. Pressing the Tab key will move the cursor to the next field in the specified order.

Code Fields in Sublime Text

Sublime Text’s multi-cursor feature allows you to select multiple lines simultaneously, making editing faster. Press Tab to highlight and edit the next field in the sequence.

Multi-cursor Feature in Sublime Text

How to Install Snippet Packages in Sublime Text

You don’t have to create every snippet manually. Various packages are available for almost any type of code snippet.

If you’ve installed Package Control, press Shift + Command + P to open it. Then, search for the snippet package you need.

SASS Snippet Package in Sublime Text

Even snippets for Code Licenses are available.

Code License Snippet in Sublime Text

Once you’ve installed the snippets you need, you’re good to go. Sublime Text will display snippet options as you type, so there’s no need to memorize trigger keywords.

SASS Snippet Mixin in Sublime Text

We hope these tips help enhance your coding workflow. Happy coding!

Additional Resources

For more information on creating and customizing snippets, you can visit the following resource:

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail