Defining scope for custom Sublime Text 2 snippets
Asked Answered
L

5

64

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>
    
Locoweed answered 17/12, 2012 at 21:5 Comment(0)
C
79

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.

Cleanup answered 9/4, 2013 at 19:49 Comment(5)
I don't see such a value in the status bar. Could you provide a screen shot?Rubidium
Neither do I. This must be dependent on some package.Cataplasia
It is package dependent; the package is called Scope Hunter, and there's a link in my answer below.Gorge
It's ctrl+shift+p on OS X.Diverticulum
For those not seeing the value in the status bar, look at the bottom left. You should see, for example, source.jsLondalondon
C
146

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.

Cataplasia answered 4/2, 2013 at 7:39 Comment(3)
When I click on Tools -> New Snippet, a file opens, I want to display this list on that file. Because I don't go often there but I also don't want to Google again for this.Lippold
Bookmark this page. It's going to be much less work than going through a huge list and then deleting all the other lines.Cataplasia
You mean to say that there is no such thing as default snippet code file?Lippold
C
79

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.

Cleanup answered 9/4, 2013 at 19:49 Comment(5)
I don't see such a value in the status bar. Could you provide a screen shot?Rubidium
Neither do I. This must be dependent on some package.Cataplasia
It is package dependent; the package is called Scope Hunter, and there's a link in my answer below.Gorge
It's ctrl+shift+p on OS X.Diverticulum
For those not seeing the value in the status bar, look at the bottom left. You should see, for example, source.jsLondalondon
G
8

There's a package called Scope Hunter, by Isaac Muse, which is really helpful for this.

It can show you the scope under any cursor in a document, which I've found really helpful when debugging my own snippets. Sometimes it's very detailed; a sample scope from my frontmost document:

Scope: text.tex.latex
       meta.function.environment.list.latex
       meta.function.environment.general.latex
       meta.function.environment.math.latex
       string.other.math.block.environment.latex
       meta.group.braces.tex
       meta.space-after-command.latex

(Wrapped for ease of reading)

I wouldn't have been able to find that if I spent a week picking SL2 apart, but this package gets it in seconds. Highly recommended.

This level of detail also means that you can define snippets in a very granular way, if you want. For example, the meta.function.environment.list.latex corresponds broadly to lists in LaTeX, so I have a snippet that inserts a new \item when I press super+enter in a list environment, but nobody else. I can target snippets much more effectively than with blind guesswork.

The source code is in Github, or you can install it through Package Control.

Gorge answered 24/5, 2013 at 10:36 Comment(2)
I just uninstalled it since I can just get that info with a keystroke (as specified in the accepted answer)Deliverance
thanks for the note on the usefulness of highly specific scopes!Blakeslee
R
7

Actually, you can use the Ctrl+Alt+Shift+P (without using Scope Hunter) and it will show you the scope on the bottom bar on the left side right after the Col/Line information. It's pretty small print but it's there.

Recant answered 25/12, 2013 at 8:52 Comment(0)
H
5

To answer, #1, look in the syntax's .tmLanguage file, look for the key: scopeName. This is what the syntax uses for the snippet's scope value.

For example, an excerpt from nathos / sass-textmate-bundle

<key>scopeName</key>
<string>source.sass</string>

So you would use source.sass in your snippet.

Here is more info on defining a syntax

Hukill answered 8/2, 2013 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.