mnqmtq
Last Updated: February 25, 2016
·
3.685K
· mklickman

Sublime Text 2: Show Scope Name of Current Document

TL;DR

In the ST2 console, run:

print(view.syntax_name(view.sel()[0].begin()))

The Whole Enchelada

Snippets in Sublime Text 2 (or any text editor, really) are pretty awesome, especially since you can limit which scope in which you want the snippet to be usable/tab-triggerable.

While I've always loved the idea of limiting the scope of my tab-triggered snippets, it hasn't always been that practical to me, since I don't exactly maintain a mental storehouse of the actual scope names of the different file formats I use, and scope names can't always be easily inferred by their respective file formats (i.e. "text.html" versus "source.css"). Not only that, but Google surprisingly turned up very sparse and non-straight-forward results as far as an easily digestible online reference. You could look in ST2's syntax definition files to find the corresponding scope name, but that is obviously far less than ideal.

I stumbled across a command for the ST2 console that conveniently outputs the scope name of the currently-selected tab, and I turned it into a snippet. You can use it as-is, which simply renders a set of "instructions" (if you can call them that) to wherever you type the snippet's tab trigger, or you can just pull out the command itself. In its current incarnation, it will render with the command itself highlighted for easy copy and pasting into the console.

Without further ado:

<snippet>
  <content><![CDATA[

    1. View > Show Console (^`)
    2. ${1:print(view.syntax_name(view.sel()[0].begin()))}

  ]]></content>
  <description>Display console command for showing scope name of current document</description>
  <tabTrigger>showscope</tabTrigger>
</snippet>

Happy snippet-ing!

4 Responses
Add your response

8295

Or you could just press ctrl+shift+p (OSX) / ctrl+alt+shift+p (Windows) :)

Look for this in the key mapping file (OSX):
{ "keys": ["ctrl+shift+p"], "command": "showscopename" },

over 1 year ago ·
12136

^ AWESOME. Now I just have to remember that shortcut, which is the real problem.

over 1 year ago ·
12728

OMG. Undocumented feature fail. Took ages to hunt this down ("why isnt my snippet working???!!!!" turns out it is source.js, not source.javascript)

over 1 year ago ·
14200

I used this to find the scope name in sublime text 3:

print(view.scope_name(view.sel()[0].begin()))

instead of syntaxname ... scopename ..!

Thx for leading me on track..!! ;)

over 1 year ago ·