Sublime Forum

ST3: How to use environment variables in snippets?

#1

I’m running ST3 (Build 3114) on a Mac under El Capitan. I’m also using Package Resource Viewer to open and edit Java language snippets.

Although I’m an experienced systems programmer, I’m new to ST3 and snippets. I’ve been experimenting with creating a new snippet, but I cannot get environment variables to substitute when the snippet is triggered.

For example, the snippet is:

<snippet>
	<content><![CDATA[if (foobar)
		{
			$TM_CURRENT_LINE
		}
		else
		{
			$TM_SELECTED_TEXT
		}
		]]></content>
	<tabTrigger>foo</tabTrigger>
	<scope>source.java</scope>
	<description>test snippet</description>
</snippet>

If I have a text line in my file that is selected:

int fooBar = 3.14;

And I then trigger the snippet by typing foo, I was expecting to see the current line (which is the same as the selected text) and the selected text substituted into the snippet when it is inserted into the current file but what I see is this:

if (foobar)
		{
			
		}
		else
		{
			
		}

I also tried using $SELECTION in place of $TM_SELECTED_TEXT but got the same result. It appears that the environment variables are not being set.

Given that I’m an experienced systems programmer but new to snippets, there is a high probability that I’ve forgotten to face north, ritually slaughter a goat, or do some other ritual required to get this to work… so any wisdom that could be offered will be gratefully received!

Thanks in advance
Andy

0 Likes

#2

hmm, I would expect that to work… do other built in variables work? $TM_FILENAME, for instance?
how about specifiying them like ${TM_SELECTED_TEXT}?

or trying the examples from http://sublime-text-unofficial-documentation.readthedocs.io/en/latest/extensibility/snippets.html ?

0 Likes