Sublime Forum

How to bind snippets to key combo

#1

I’m trying to figure out how to bind snippets in Sublime Text 2 to a key combo. Not having any luck looking at the default key bindings file. I have the following snippet:

<snippet>
    <content><![CDATA[<p>${0:${TM_SELECTED_TEXT:Paragraph}}</p>]]></content>
    <scope>source.php, text.html</scope>
    <description>HTML "p" Tag</description>
</snippet>

This is what i am trying to use to bind it:

{ "keys": "alt+shift+p"], "command": "insert_snippet", "args": {"file": "Packages/User/ptag.sublime-snippet"}}
0 Likes

#2

Looks like you’ve got it, just need to use “name” rather than “file” for the insert_snippet command.

0 Likes

#3

awesome. thanks.

0 Likes

#4

With a scope (or set of scopes) listed in the snipped (XML), what happens to scopes applied also to the keybinding? Are they intersected or unioned? (Or maybe something else happens?)

Also, anyone have pointers about putting snippet content directly into a keybinding, vs in a snippet referenced by path? I’m interested in whether putting snippet content into a keybinding is a good or bad idea and how to do it.

0 Likes

#5

In a selector, scope names separated by a comma are ORed together.

If you’re making a key binding, feel free to either reference a snippet file by name (the “name” parameter), or include one inline (the “contents” parameter), it’s just a question of convenience. I tend to put short snippets inline, and long ones in files.

0 Likes

#6

Thanks!

0 Likes

#7

I need some help guys.
I’ve made a inline snippet with keybinding:

{ "keys": "p", "tab"], "command": "insert_snippet", "args": { "contents": "<p>${0:${TM_SELECTED_TEXT}}</p>"}}

and im trying to set scope inline but with no luck so far, can any1 help please? :smile:

{ "keys": "p", "tab"], "command": "insert_snippet", "args": {"scope": "text.html", "contents": "<p>${0:${TM_SELECTED_TEXT}}</p>"}}

do not work, still get

in tag, same with:
{ "keys": "p", "tab"], "command": "insert_snippet", "args": {"contents": "<p>${0:${TM_SELECTED_TEXT}}</p>"}, "context":
	
		{ "key": "selector", "operator": "equal", "operand": "text.html" }
	]
}
0 Likes

#8

The last form, with the context, is the one that you need to use.

If you press Ctrl+Alt+P (Windows/Linux), you’ll see the current scope printed to the status bar. Note that the text inside the script tag has ‘text.html’ as part of its scope, but it also has ‘source.js’. To make a selector that triggers in html, but not within embedded javascript, try using “text.html - source.js” for the selector

0 Likes

#9

[quote=“jps”]The last form, with the context, is the one that you need to use.

If you press Ctrl+Alt+P (Windows/Linux), you’ll see the current scope printed to the status bar. Note that the text inside the script tag has ‘text.html’ as part of its scope, but it also has ‘source.js’. To make a selector that triggers in html, but not within embedded javascript, try using “text.html - source.js” for the selector[/quote]

Thanx for explanation, now it works like a charm.

PS
Thats incredible how much patience you have to code such a awesome app and explain all these noob questions same time :smiley:
I appreciate it, thanks again.

0 Likes