Working With Code Snippets In Sublime Text

简介: <p style="margin-top:0.55em; margin-bottom:1.8em; padding-top:0px; padding-bottom:0px; border:0px; font-size:1.15em; font-family:'Droid Sans',Helvetica,Arial,sans-serif; vertical-align:baseline; l

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.

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

  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:

  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.

  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.

  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.

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.

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.

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.

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.

相关文章
|
7月前
|
前端开发 JavaScript Linux
Sublime Text 3配置Go语言开发环境
【4月更文挑战第13天】本篇文章 Huazie 向大家介绍使用 Sublime Text 3搭建Go语言开发环境,并演示编译运行 Go语言代码
847 8
Sublime Text 3配置Go语言开发环境
|
6月前
lua环境,sublime text 插件安装
lua环境,sublime text 插件安装
|
4月前
|
Python
Sublime Text Python 代码提示插件 Anaconda
Sublime Text Python 代码提示插件 Anaconda
94 1
|
4月前
|
JSON 数据格式
Sublime Text 查找的内容 高亮显示
Sublime Text 查找的内容 高亮显示
548 0
Sublime Text 查找的内容 高亮显示
|
4月前
Sublime Text 16进制显示
Sublime Text 16进制显示
193 0
|
4月前
Sublime Text 汉化 中文
Sublime Text 汉化 中文
59 0
|
7月前
|
前端开发 Unix Linux
Sublime Text 3配置 Python 开发环境
【5月更文挑战第7天】本篇 Huazie 介绍了 Sublime Text 3 配置 Python 开发环境的相关内容,感兴趣的朋友赶紧配置起来,有任何问题可以随时评论区沟通。
169 1
Sublime Text 3配置 Python 开发环境
|
7月前
|
开发框架 前端开发 C#
Sublime Text 3配置 C# 开发环境
【5月更文挑战第2天】本篇 Huazie 介绍了 Sublime Text 3 配置 C# 的相关内容,感兴趣的朋友赶紧配置起来,有任何问题可以随时评论区沟通。
63590 7
Sublime Text 3配置 C# 开发环境
|
7月前
|
C++
Sublime Text 3 解决中文乱码问题
【5月更文挑战第1天】本篇介绍 Sublime Text 3 解决中文乱码问题,不仅仅适用于 C/C++ 代码,也适用于其他任何 Sublime Text 3 集成的开发环境。
1324 5
Sublime Text 3 解决中文乱码问题
|
7月前
|
API 开发工具 C++
【专栏:工具与技巧篇】使用代码编辑器(VS Code/Sublime Text)提升开发效率
【4月更文挑战第30天】在VS Code与Sublime Text两大流行代码编辑器中,开发者可借助其高效特性提升编程效率。VS Code拥有丰富的插件生态、内置Git集成、强大的调试工具、智能提示和多文件导航功能。Sublime Text则以其轻量级、快速响应、多光标编辑及自定义构建系统见长。学习编辑器的键盘快捷键、自定义配置、使用内置终端以及键绑定和宏,将助开发者进一步提高开发效率。选择适合自己的编辑器并不断适应新技术是提升开发工作流的关键。
318 1