Sublime Forum

Escaping dollar sign in snippets

#1

Hey there!
I’m creating my own snippet library, and can’t solve this:
Adjusting a CSS property based on received props in styled-components looks like this:

background: ${props => props.primary ? 'palevioletred' : 'white'};

Since I type that interpolation a lot, I wanted to make a snippet out of it, however, my snippet code’s not working.

<snippet>
	<content><![CDATA[
${(props) => props.${1}};
]]></content>
	<tabTrigger>sci</tabTrigger>
	<scope>source.js</scope>
	<description>styled-components interpolation</description>
</snippet>

I have tried to escape the dollar sign (the first character on the line) in a few ways, but no success.

Is it even possible? How?

0 Likes

#2

Solved it. One backslash - \ - did the trick. Don’t understand how I missed that one…

\${(props) => props.${1}};

2 Likes

#3

TL;DR: this is mentioned in the unofficial docs at http://docs.sublimetext.info/en/latest/extensibility/snippets.html :slight_smile:

0 Likes

How can I use this Code as Snippet for JS?