Home Download Buy Blog Forum Support

how to bind snippets to key combo

how to bind snippets to key combo

Postby mbond on Thu Feb 24, 2011 1:02 pm

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:

Code: Select all
<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:

Code: Select all
{ "keys": ["alt+shift+p"], "command": "insert_snippet", "args": {"file": "Packages/User/ptag.sublime-snippet"}}
mbond
 
Posts: 35
Joined: Thu Feb 03, 2011 3:49 pm

Re: how to bind snippets to key combo

Postby jps on Thu Feb 24, 2011 1:29 pm

Looks like you've got it, just need to use "name" rather than "file" for the insert_snippet command.
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: how to bind snippets to key combo

Postby mbond on Fri Feb 25, 2011 3:09 pm

awesome. thanks.
mbond
 
Posts: 35
Joined: Thu Feb 03, 2011 3:49 pm

Re: how to bind snippets to key combo

Postby emmanuel on Sat Mar 05, 2011 5:15 am

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.
emmanuel
 
Posts: 2
Joined: Sat Mar 05, 2011 5:07 am

Re: how to bind snippets to key combo

Postby jps on Sat Mar 05, 2011 5:29 am

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.
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: how to bind snippets to key combo

Postby emmanuel on Sun Mar 06, 2011 9:23 am

Thanks!
emmanuel
 
Posts: 2
Joined: Sat Mar 05, 2011 5:07 am

Re: how to bind snippets to key combo

Postby szameks on Sun Mar 06, 2011 2:23 pm

I need some help guys.
I've made a inline snippet with keybinding:
Code: Select all
{ "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? :)
Code: Select all
{ "keys": ["p", "tab"], "command": "insert_snippet", "args": {"scope": "text.html", "contents": "<p>${0:${TM_SELECTED_TEXT}}</p>"}}

do not work, still get <p></p> in <script> tag, same with:
Code: Select all
{ "keys": ["p", "tab"], "command": "insert_snippet", "args": {"contents": "<p>${0:${TM_SELECTED_TEXT}}</p>"}, "context":
   [
      { "key": "selector", "operator": "equal", "operand": "text.html" }
   ]
}
szameks
 
Posts: 10
Joined: Fri Nov 27, 2009 1:12 pm

Re: how to bind snippets to key combo

Postby jps on Sun Mar 06, 2011 11:11 pm

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
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: how to bind snippets to key combo

Postby szameks on Mon Mar 07, 2011 9:46 am

jps wrote: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


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 :D
I appreciate it, thanks again.
szameks
 
Posts: 10
Joined: Fri Nov 27, 2009 1:12 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 25 guests