Danish Adeel
Danish Adeel

Reputation: 730

Using $ in vscode snippets

I am trying to create a snippet which includes a $ sign, but in output vscode is skipping the $ sign and making the text selected.

"Color 1": {
        "prefix": "c1",
        "body": [
            "color: $c1;"
        ]
    }

Output

color: c1;

I have also tried using \ and ''. Can anyone please guide.

Upvotes: 0

Views: 122

Answers (2)

rioV8
rioV8

Reputation: 28663

From the Snippet Doc page


How do I have a snippet place a variable in the pasted script?

To have a variable in the pasted script, you need to escape the '$' of the $variable name so that it isn't parsed by the snippet expansion phase.

"VariableSnippet":{
    "prefix": "_Var",
    "body": "\\$MyVar = 2",
    "description": "A basic snippet that places a variable into script with the $ prefix"
  }

This results in the pasted snippet as:

$MyVar = 2

Upvotes: 1

TalVik99
TalVik99

Reputation: 149

You can escape $ sign in snippets with double backslash \\ like this:

"Color 1": {
    "prefix": "c1",
    "body": [
            "color: \\$c1;"
    ]
}

Upvotes: 2

Related Questions