Search code examples
scopesublimetext2code-snippets

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>
    

Solution

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