Sublime Text号称最性感的编辑器, 并且越来越多人使用, 美观, 高效

关于如何使用Sublime text可以参考我的另一篇文章, 相信你会喜欢上的..
Sublime Text 2使用心得

现在介绍一下Snippet, Snippets are smart templates that will insert text for you and adapt it to their context. Snippet 是插入到文本中的智能模板并使这段文本适当当前代码环境. 程序员总是会不断的重写一些简单的代码片段, 这种工作乏味/无聊, 而Snippet的出现会让Code更加高效.

1. Snippe创建,存储和格式


(这里snippet称作代码片段)

Snippet可以存储在任何的文件夹中, 并且以.sublime-snippet为文件扩展名, 默认是存储在.sublime-snippet文件夹下.

Snippet文件是以.sublime-snippet为扩展的XML文件, 可以命名为XXX.sublime-snippet, 创建自己的snippet的方式为菜单栏Tools | 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>

为了方便理解简化以上代码:

<snippet>
<content><![CDATA[Type your snippet here]]></content>
<!-- Optional: Tab trigger to activate the snippet -->
<tabTrigger>hello</tabTrigger>
<!-- Optional: Scope the tab trigger will be active in -->
<scope>source.python</scope>
<!-- Optional: Description to show in the menu -->
<description>My Fancy Snippet</description>
</snippet>

简要介绍一下snippet四个组成部分:

  • content:其中必须包含<![CDATA[…]]>,否则无法工作, Type your snippet here用来写你自己的代码片段
  • tabTrigger:用来引发代码片段的字符或者字符串, 比如在以上例子上, 在编辑窗口输入hello然后按下tab就会在编辑器输出Type your snippet here这段代码片段
  • scope: 表示你的代码片段会在那种语言环境下激活, 比如上面代码定义了source.python, 意思是这段代码片段会在python语言环境下激活.
  • description :展示代码片段的描述, 如果不写的话, 默认使用代码片段的文件名作为描述

2. snippet环境变量


列举一下可能用到的环境变量, 这些环境变量是在Sublime中已经预定义的.

环境变量名 描述
$TM_FILENAME 用户文件名
$TM_FILEPATH 用户文件全路径
$TM_FULLNAME 用户的用户名
$TM_LINE_INDEX 插入多少列, 默认为0
$TM_LINE_NUMBER 一个snippet插入多少行
$TM_SOFT_TABS 如果设置translate_tabs_to_spaces : true 则为Yes
$TM_TAB_SIZE 每个Tab包含几个空格

同一通过下面的代码片段进行验证:

<snippet>
<content><![CDATA[
=================================
$TM_FILENAME 用户文件名
$TM_FILEPATH 用户文件全路径
$TM_FULLNAME 用户的用户名
$TM_LINE_INDEX 插入多少列, 默认为0
$TM_LINE_NUMBER 一个snippet插入多少行
$TM_SOFT_TABS 如果设置translate_tabs_to_spaces : true 则为Yes
$TM_TAB_SIZE 每个Tab包含几个空格
=================================
]]></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>

验证方式 : 保存自定义snippet,在python文件夹下输入hello按下tab

3. snippet Fields


设置Fields, 可以通过tab键循环的改变代码片段的一些值

<snippet>
<content><![CDATA[
=================================
First Name: $1
Second Name: $2
Address: $3
=================================
]]></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>

验证方式, 在python文件夹下, 输入hello按下tab, 会出现已经定义的代码片段, 不停的按下tab会发现输入光标在$1, $2, $3的位置跳转, 跳转顺序由数字由小到大决定, Shift+Tab可以进行向上跳转, 可以通过Esc结束跳转

4. snippet Mirrored Fields


设置snippet镜像区域,会使相同编号的位置同时进行编辑

<snippet>
<content><![CDATA[
=================================
First Name: $1
Second Name: $1
Address: $1
=================================
]]></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>

验证方法: 在python文件中, 输入hello按下tab,出现代码片段,会出现三行同行编辑的光标, 这时进行编辑可以同时进行三行相同的编辑

5. snippet Placeholders


snippet 占位符含义类似于python的默认参数, 通过对Field做出一点修改, 可以定义Field的默认值, 并且可以通过tab键可以对不同的默认值进行修改

<snippet>
<content><![CDATA[
=================================
First Name: ${1:Guillermo}
Second Name: ${2:López}
Address: ${3:Main Street 1234}
User name: $1
Environment Variable : ${4:$TM_FILEPATH } #可以设置默认占位符为环境变量
Test: ${5:Nested ${6:Placeholder}}
=================================
]]></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>

验证方式: 在pyton文件中输入hello,然后按下tab, 输入代码片段后, 两个$1的field可以同时修改默认值, 然后继续按下tab键可以修改$2的默认值..., 还可以占位符设置嵌套

写到这里基本上大家都应该可以根据需求编写简单的snippet了, 恭喜你..

6. snippet Substitutions


高级应用可以使用Perl的正则表达式

最后送上简单的python的snippet

<snippet>
<content><![CDATA[
""" 文档注释 Args :
${1}: Returns :
${2}: Raises :
${3}: """
]]></content>
<tabTrigger>"""</tabTrigger>
<scope>source.python</scope>
<description>Documentation Comments</description>
</snippet> ###
<snippet>
<content><![CDATA[def ${1:foo}():
doc = "${2:The $1 property.}"
def fget(self):
${3:return self._$1}
def fset(self, value):
${4:self._$1 = value}
def fdel(self):
${5:del self._$1}
return locals()
$1 = property(**$1())$0]]></content>
<tabTrigger>property</tabTrigger>
<scope>source.python</scope>
<description>New Property</description>
</snippet>

sublime中Snippe的使用的更多相关文章

  1. 手把手教你写Sublime中的Snippet

    手把手教你写Sublime中的Snippet Sublime Text号称最性感的编辑器, 并且越来越多人使用, 美观, 高效 关于如何使用Sublime text可以参考我的另一篇文章, 相信你会喜 ...

  2. python 在sublime 中的配置

    首先可以先装package control 方法——按ctrl+`,然后在命令行里复制粘贴以下代码, import urllib2,os;pf='Package Control.sublime-pac ...

  3. 在sublime中使用less

    高亮显示: 可以在Less文件中显示语法高亮,这样看起来会更舒服一些. 按下Ctrl+Shift+P调出命令面板:输入install调出Install Package选项并回车:输入less,选中并安 ...

  4. 已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

    [问题] 在折腾: [已解决]Sublime Text 2中运行Python程序出错:The system cannot find the file specified 的过程中,虽然解决了找不到py ...

  5. 解决Ubuntu下sublime中不能输入中文的问题

    解决Ubuntu下sublime中不能输入中文的问题 Ubuntu下安装sublime后,不能输入中文,而在其他软件中能正常输入,这是sublime的bug,解决方案是在通过shell在每次运行sub ...

  6. windows下node.js+sublime中安装coffeescript

    node.js中安装Coffeescript 1.我的node.js安装目录 2.node.js 全局模块所在目录   3.node.js安装coffeescript npm install -g c ...

  7. Sublime中开发Ruby

    Ruby:Sublime中开发Ruby需要注意的Encoding事项 目录 背景Sublime相关默认的文件存储编码:UTF8默认的输出控制台编码:UTF8修改默认的输出控制台编码Ruby相关默认的代 ...

  8. 如何在sublime中使用sass

    搞了好久,终于把sass搞定了. 最开始,我是想使用koala来实现对sass的实时编译的,但是每当我保存的时候,总是弹出erro错误,即无法编译生成css文件,百度了半天,问了好久,这个问题还是没能 ...

  9. 如何在sublime中安装使用eslint

    1:首先你需要全局安装eslint npm install -g eslint 安装完成后在控制台 输入 eslint -v 有版本号说明就可以在npm中使用了,可以检查语法的错误处,但还不能在sub ...

随机推荐

  1. 记录一下取ul中li的值

    //#id 事件 $('#onLineUser').on('click','li',function () { $(".list-group-item").removeClass( ...

  2. Postman Interceptor和postman更改id仍然无法使用的,从这里下载相同版本的postman和interceptor插件

    1.postman安装: chrome://extensions/打开,把下载好的postman插件拖到里面就可以了. 2.Postman interceptor安装: chrome://extens ...

  3. java静态代理及动态代理(学习示例)

    1.接口 public interface Channel { void send(); } 2.实现类(可以为各种不同实现) public class ChannelImpl implements ...

  4. 【leetcode】951. Flip Equivalent Binary Trees

    题目如下: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the ...

  5. 自定义checkbox,radio样式

    input[type=radio] { margin-right: 5px; cursor: pointer; font-size: 14px; width: 15px; height: 15px; ...

  6. Django 自定义 admin

    为 model 自定义显示 label是这个Field如果在form中的话会显示的.而verbose_name在form中不会显示.只是作为一种说明而已 callable()   -->  是否 ...

  7. APICloud框架--sublime使用自定义loader

    官方的apploader调试器,只是有官方的一些模块,如果我们使用非官方的模块就要使用自定义loader进行调试.接下来就走一边sublime设置自定义loader的步骤 修改config.xml 打 ...

  8. Ubuntu管理员密码设置

    最近学习嵌入式编程,首先准备搭建一个嵌入式开发环境. 由于想省钱,就准备搭建一个虚拟的arm系统用于测试学习. 虚拟系统搭建与linux系统上,暂定使用Ubuntu+qemu进行环境搭建. 在进行Ub ...

  9. ()centos7 安装mysql8.0

    一.下载mysql 1 .下载 https://dev.mysql.com/downloads/repo/yum/ wget http://repo.mysql.com/mysql80-communi ...

  10. 剑指offer第二版面试题6:重建二叉树(JAVA版)

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重新构造出该二叉树.假设输入的前序遍历和中序遍历的结果中不包含重复的数字.例如输入的前序遍历序列为{1,2,4,7,3,5,6,8}和中序遍历为{4,7 ...