While trying to write my own snippets for Sublime Text 2, I ran into the following two problems:
Finding scope keys. I figured out that I can look through my packages one by one and find references to a declared "scope" property. For example in ~/Library/Application Support/Sublime Text 2/Packages/JavaScript/Comments.tmPreferences
(a file in my HTML package) there's these two lines:
<key>scope</key> <string>source.js</string>
So if I want my current snippet to work on javascript files, I define my scope like:
<scope>source.js</scope>
I'm assuming all these scope keys are defined on-the-fly based on what Packages I have installed. Does Sublime Text build a list anywhere that I can more easily reference? Perusing through a bunch of package files seems overly tedious.
Defining multiple scope properties. This I've figured out, and the following line allows my snippet to work in both HTML and JavaScript files.
<scope>text.html, source.js</scope>
Sublime text provides a default template in XML format when we create a new snippet. To create the template got to SUBLIME TEXT → TOOLS → DEVELOPER → NEW SNIPPET. Let's understand the template definition and modify the parameters. The actual content or block of code to be inserted should be placed within <content><!
Sublime Text will present you with an skeleton for a new snippet. Snippets can be stored under any package's folder, but to keep it simple while you're learning, you can save them to your Packages/User folder.
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.
Use this keyboard-shortcut:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With