您的位置:首页 > 编程语言

sublime text 2:创建可复用的代码片段(snippet)

2014-04-21 15:10 435 查看
转载自:http://www.blogjava.net/Hafeyang/archive/2012/08/17/how_to_create_code_snippet_in_subline_text_2.html

对于前端工程师来讲,写一个html页面的基本结构是体力活,每次去拷贝一个也麻烦,sublime text 2 提供了一个很好的复用代码片段。下面介绍一下创建一个html5的代码片段的过程。

在菜单上点击Tools -> New Snippet,会新建一个xml文件页签:

<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>

注释已经说的比较详细了。

content 里面就是代码模版:${序号:默认值} ,序号相同的地方光标会同时停在那可以多处同时编辑。序号大小就是tabindex。在实际使用代码的时候,可以使用tab切换光标位置。

tabTrigger是指输入该字符串后tab就是在光标插入content中的内容。

scope是指在何种文件类型中使用。

下面是html5代码片段的定义:

<snippet>

<content><![CDATA[

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>${1}</title>

</head>

<body>

<h1>${1}</h1>

Hello, ${2:this} is a ${3:snippet}.

</body>

</html>

]]></content>

<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->

<tabTrigger>html5</tabTrigger>

<!-- Optional: Set a scope to limit where the snippet will trigger -->

<!-- <scope>source.python</scope> -->

</snippet>

编辑完之后保存为 C:\Users\[用户]\AppData\Roaming\Sublime Text 2\Packages\User\html5.sublime-snippet (Win7下) 默认的保存路径就行。后缀必须是.sublime-snippet

保存完重启Sublime text 2,新建文件:输入html5,tab会出现如下效果:



${1}出现了两次,所以光标同时编辑图中两处。

${2:this},所以在2处出现this默认值。${1}处编辑完按tab就到${2}处。

OK, That's all。

————————————————————华丽的分割线——————————————————————————

下面来自:http://www.360doc.com/content/13/0520/16/9437165_286806535.shtml

“snippet”在英语里面是“片段”的意思。当我们编码时候,通常想要打几个简略的字符串,就出来一些固定的模板,这个就类似于前端的zen coding。

不过,目前,sublime text2 的zen coding 不支持了,不知道sublime自己内置了什么默认的sinppet,只能实现一些简单的类似zen coding的功能。

定义很简单,先通过tools->new snippets就进入了新建文件。

文件包括以下内容:

复制代码

<snippet>

<content><![CDATA[

]]></content>

<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->

<tabTrigger></tabTrigger>

<!-- Optional: Set a scope to limit where the snippet will trigger -->

<scope></scope>

</snippet>

复制代码

现在对以上的参数进行说明:

content:是你要定义的模板内容

tabTrigger:是你按tab键之前需要输入的快捷字符串,输了该字符串后,按tab键,就能生成你需要的模板内容啦

scope:是对文件格式的限制,比如,html格式的要设置为text.html

这里给个示例,我做的html5文档声明,code如下:

复制代码

<snippet>

<content><![CDATA[

<!DOCTYPE HTML>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

</body>

</html>

]]></content>

<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->

<tabTrigger>html5</tabTrigger>

<!-- Optional: Set a scope to limit where the snippet will trigger -->

<scope>text.html</scope>

</snippet>

复制代码

然后,保存文件,新建html文件,输入html5,按ta键,就能显示出如下代码了:

复制代码

<!DOCTYPE HTML>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

</body>

</html>

复制代码

这里需要注意的是scope,因为你不知道如何设置,才会让sinppet正常响应。设置出错,自然是无效啦。

列出scope列表,参考自:https://gist.github.com/iambibhas/4705378:

复制代码

ActionScript: source.actionscript.2

AppleScript: source.applescript

ASP: source.asp

Batch FIle: source.dosbatch

C#: source.cs

C++: source.c++

Clojure: source.clojure

CSS: source.css

D: source.d

Diff: source.diff

Erlang: source.erlang

Go: source.go

GraphViz: source.dot

Groovy: source.groovy

Haskell: source.haskell

HTML: text.html(.basic)

JSP: text.html.jsp

Java: source.java

Java Properties: source.java-props

Java Doc: text.html.javadoc

JSON: source.json

Javascript: source.js

BibTex: source.bibtex

Latex Log: text.log.latex

Latex Memoir: text.tex.latex.memoir

Latex: text.tex.latex

TeX: text.tex

Lisp: source.lisp

Lua: source.lua

MakeFile: source.makefile

Markdown: text.html.markdown

Multi Markdown: text.html.markdown.multimarkdown

Matlab: source.matlab

Objective-C: source.objc

Objective-C++: source.objc++

OCaml campl4: source.camlp4.ocaml

OCaml: source.ocaml

OCamllex: source.ocamllex

Perl: source.perl

PHP: source.php

Regular Expression(python): source.regexp.python

Python: source.python

R Console: source.r-console

R: source.r

Ruby on Rails: source.ruby.rails

Ruby HAML: text.haml

SQL(Ruby): source.sql.ruby

Regular Expression: source.regexp

RestructuredText: text.restructuredtext

Ruby: source.ruby

Scala: source.scala

Shell Script: source.shell

SQL: source.sql

TCL: source.tcl

HTML(TCL): text.html.tcl

Plain text: text.plain

Textile: text.html.textile

XML: text.xml

XSL: text.xml.xsl

YAML: source.yaml

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: