Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining scope for custom Sublime Text 2 snippets

While trying to write my own snippets for Sublime Text 2, I ran into the following two problems:

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

  2. 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> 
like image 656
James Heston Avatar asked Dec 17 '12 21:12

James Heston


People also ask

How do I use snippets in Sublime Text?

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

Where are sublime text snippets stored?

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.


2 Answers

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.

like image 83
Bibhas Debnath Avatar answered Sep 19 '22 15:09

Bibhas Debnath


View Current Scope of Cursor Position

  1. Place your cursor in the file where you wish to know the scope.
  2. Use this keyboard-shortcut:

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

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

like image 34
Joseph Knight Avatar answered Sep 21 '22 15:09

Joseph Knight