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.

Recommended Reading: Top
10 Free Source Code Editors – Reviewed

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.

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.

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.

时间: 2024-11-05 22:25:18

Working With Code Snippets In Sublime Text的相关文章

A Set of HTML Snippets for Sublime Text 2

DOWNLOAD Considering how pop­u­lar my CSS snip­pets for Sublime Text have been, I thought I'd bet­ter col­late and release some of the HTML snip­pets I use reg­u­larly. Put simply, they allow you to turn this: doctype Into this: <!doctype html> <

Sublime Text 全程指引 by Lucida

原文:Sublime Text 全程指引 by Lucida 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 博客园:@figure9 原文链接:http://zh.lucida.me/blog/sublime-text-complete-guide/博客园链接:http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 摘要(Abstract) 本文

Sublime Text 全程指引

来源:http://zh.lucida.me/blog/sublime-text-complete-guide/ 前言(Prologue) Sublime Text是一款跨平台代码编辑器(Code Editor),从最初的Sublime Text 1.0,到现在的Sublime Text 3.0,Sublime Text从一个不知名的编辑器演变到现在几乎是各平台首选的GUI编辑器.而这样优秀的编辑器却没有一个靠谱的中文教程,所以我试图通过本文弥补这个缺陷. 编辑器的选择(Editor Choic

转 Sublime Text 全程指南

安装(Installation) Sublime Text 官方网站 提供了Sublime Text各系统各版本的下载,目前Sublime Text的最新版本是 Sublime Text 3 .这里以Windows版本的Sublime Text安装为例.  注意在安装时勾选 Add to explorer context menu ,这样在右键单击文件时就可以直接使用Sublime Text打开.  添加Sublime Text到环境变量 使用 Win + R 运行 sysdm.cpl 打开"系

Sublime Text 2 设置文件详解

Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己需要的.而且 Linux 支持也做的非常好. 之前想设置什么都是直接在网上搜,但最近想调行距,这个把我给难住了,软件上的设置没找到,网上搜也没有,最后的最后在Sublime的官方论坛找到了,个人觉得行距还是很影响视觉体验的,看看下面的对比图就知道了: 看来想驾驭好这软件不弄清楚配置文件是不行了,周末

Mac OS X系统搭建谷歌Go语言开发工具 Sublime Text 2环境配置

  一.安装Golang的SDK 在官网http://golang.org/ 直接下载安装包安装即可.下载pkg格式的最新安装包,直接双击运行,一路按照提示操作即可完成安装. 安装完成后,打开终端,输入go,检测Golang SDK是否安装成功,如下显示,则表示安装成功: 开发工具 Sublime Text 2环境配置-mac os 搭建ftp服务器"> 你也可以输入go version 查看你所安装的Golang的版本 二.环境变量配置(GOPATH) GOPATH是用来告诉Golang

Ubuntu下Sublime Text 3解决无法输入中文的方法

安装GTK库 首先确保您的电脑已经安装了GTK库 执行和如下命令可以查看电脑上是否安装了GTK pkg-config --modversion gtk+ (查看1.2.x版本) pkg-config --modversion gtk+-2.0 (查看 2.x 版本) pkg-config --version (查看pkg-config的版本) pkg-config --list-all grep gtk (查看是否安装了gtk) 1 2 3 4 1 2 3 4 如果没有 请参照Ubuntu下安装

推荐几款用 Sublime Text 开发 Laravel 所用到的插件_php实例

我目前主要使用 Sublime Text 2 做 Laravel 开发,主要插件如下: 1.Blade Snippets:提供 Blade 模板自动补全 2.Laravel 4 Blade Highlighter:提供 Blade 关键字高亮 3.Laravel Generator:提供超高效率 Laravel 工作流 这就是之前教程里面说过的way/generators 的 Composer 包的另一个版本,是运行在编辑器里的.不过还是推荐使用命令行版,因为可以方便地读取历史记录,并加以修改.

比NotePad++更好的文本代码(C#)编辑器Sublime Text

原文:比NotePad++更好的文本代码(C#)编辑器Sublime Text 前言  前两天在博客园看到@晴天猪的博客发表的关于他使用的代码编辑器,自己索性试了一下,果断好用,自己也来记录一下.以便以后配置使用.接下来我配置的主要是简单的编译C#代码的. 配置一调用C#编译器  我现在电脑的系统为Win7哦.我要将C#编译器的csc.exe文件添加到环境变量中. 首先我的电脑==右键属性==高级系统设置==环境变量==系统变量==变量Path双击==在变量值中将路径添加到后面添加前用;分隔C: