Sublime Forum

Feed snippet a parameter

#1

I’ve got a snippet working great using regular variables. Using my tabTrigger “testTrigger”, my snippet is inserted and I have $1 to change the value that’s going to be different each time I use it.

I’m wondering though if I could set my tabTrigger as the variable so that what I type as my tabTrigger is used for my variable $1?

Hopefully this isn’t something I just overlooked in the docs.

Thanks!
Ken

0 Likes

#2

Setting your tabTrigger as the variable will be a little difficult because the tabTrigger determines which snippet will be loaded.
How can ST know which snippet to load when the tabTrigger is different every time?

However i do something similar by feeding the selection to the snippet.
You can do this by using $SELECTION in your snippet.
And you can combine this with the $1 like this:
${1:$SELECTION}

Finally you could set a key binding for the snippet like so:
{ “keys”: “alt+p”], “command”: “insert_snippet”, “args”: { “name”: “Packages/User/printr.sublime-snippet” } },
In the above example a printr(); will be placed around the array i selected when i press alt+p.
The snippet looks like this:
print_r(${1:$SELECTION},true)

0 Likes