Sublime Text 3 Snippet 快捷字符段输出设置

1. 自定义sublime代码片段

原文

sublime text 已经有一些他们内置的一些代码片段,但是有时候,这些并不能满足我们,这就需要我们自定义一些代码片段。步骤如下:

1、打开sublime text

2、选择 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> --><br> <!-- <description>my snippet</description>
</snippet>

  解释如下:

<![CDATA[]> 中间则是要输出的代码片段,中间的 ${1:this}中的this是当前${1}位置默认用this,按tab键会切换到 ${2}处,依次类推。同一个${num}可以出现多次。

  tabTrigger 可选,触发该片段的字符串,比如下面用hello触发该片段,在页面输入hello按tab就会输出 <![CDATA[]]>中间的字符串

  scope 可选,设置该代码片段对什么类型的文件生效,不设置,则对所有类型生效。比如下面设置对html和js生效,在css下面输入 hello按tab不会有其他任何输出。过个代码片段用逗号(,)隔开。

  description 可选,设置该片段的说明,不定义,则显示文件名称。PS:Sublime Text 3没有。

现在我们来创建一个自己的代码片段吧:

<snippet>
     <content>
     <![CDATA[
     <footer>
          <p>Copyright © 2012 ${1:feige}.com</p>
          <p>增值电信业务经营许可证 赣A2-${2} <a href="#">赣ICP备号${3}</a></p>
     </footer>
     ]]>
     </content>
     <tabTrigger>cft</tabTrigger>
     <description>custom-footer</description>
     <scope>text.html,source.js</scope>
</snippet>

  创建完毕以后,保存在\Packages\User目录下(例C:\Users[用户]\AppData\Roaming\Sublime Text 2\Packages\User\),后缀名.sublime-snippet。

  重启sublime text 2,该snippet即可使用了。

2. Scope 索引及查询方式

原文

  1. Place your cursor in the file where you wish to know the scope.

  2. Use this keyboard-shortcut:

  3. Windows: ctrl+shift+alt+p
    Mac: ctrl+shift+p

The current scope will be displayed in the left side of the status bar on Windows, or in a popup window on Mac.

Use these as the <scope> key in your foo.sublime-snippet file.

The returned scopes are listed generic to specific. Choose the scope(s) which best “scoped” the snippet to where it should be available to tab trigger.

注意,就算是同一篇文档,不同位置出来的scope也是不一样的,所以定位之精确到某一种文档的某一种位置的。

对于markdown只要text.html.markdown 就可以全篇通用。

Here is a list of scopes to use in Sublime Text 2 snippets -

ActionScript: source.actionscript.2
AppleScript: source.applescript
ASP: source.asp
Batch FIle: source.dosbatch
C#: source.cs
C++: source.c++
Clojure: source.clojure
CoffeeScript: source.coffee
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
LESS: source.css.less
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
SASS: source.sass
Scala: source.scala
Shell Script: source.shell
SQL: source.sql
Stylus: source.stylus
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

If anything is missing, add it in this gist https://gist.github.com/4705378.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值