William
William

Reputation: 4578

How to use dollar signs/JQuery with Sublime Text 2 Snippets?

I am trying to create some code "Snippets" with Sublime Text2 and everything works fine except when I try and add jQuery code. Apparently any "Snippets" code that includes lines that begin with a $ will simply not work. I couldn't find any answers online regarding this.

Upvotes: 32

Views: 10943

Answers (2)

Ricardo Zea
Ricardo Zea

Reputation: 10283

Although not an 'answer' per se, my comment is more an example.

I had the same issue but for creating jQuery's shorthand document.ready function snippet in Sublime Text 2.

With your answer, I was able to make it work:

<snippet>
  <content><![CDATA[
    \$(function(){
      \$("$1").$2("$3");
     });
  ]]></content>
     <tabTrigger>jq</tabTrigger>
</snippet>

Type jq, press TAB and you get this:

$(function(){
    $("|").("");
});

Notice the cursor is inside the first set of parenthesis, this is caused by the $1 in the snippet. Once you've finished typing the value, press TAB again and the cursor will move to where the method goes, which is where the $2 is in the snippet. Press TAB again and cursor moves to the second set of parenthesis where $3 is.

Hope this helps others.

Upvotes: 6

Zendmailer
Zendmailer

Reputation: 880

Did you try escaping the $ with a \?

For instance in PHP, the $GLOBALS snippet is:

<snippet>
    <content><![CDATA[\$GLOBALS['${1:variable}']${2: = }${3:something}${4:;}$0]]></content>
    <tabTrigger>globals</tabTrigger>
    <scope>source.php</scope>
    <description>$GLOBALS['…']</description>
</snippet>

As you can see in <content>, $GLOBALS is expressed as \$GLOBALS. This is because $ is a symbol used for fields like ${1:variable}.

Upvotes: 71

Related Questions