Search code examples
visual-studio-codecode-snippets

Escape $ in snippet


I'm trying to escape the $ character in my snippets but I cannot seem to get it correct.

If I use \$ as it looks like I should be from the documentation, I get the error:

file: path/to/snippets/php.json'
severity: 'Error'
message: 'Invalid escape character in string'

And no $ appear in my snippet.

If I use $$ found from this answer, I get a $ to show up but it thinks that the text immediately following is a tabstop.

If I use $\ it works but I have to have a character that is part of an escape sequence immediately following. So if I wanted $factory, I would need to do $\ffactory. \f seems to be the best as it does not effect the layout of my snippet.

I'm pretty sure that I am missing what needs to be done here.

My snippet for reference:

"factory" :{
    "prefix": "factory",
    "body": [
        "\$factory->define($1, function (Faker\\Generator \$faker){",
        "\treturn [",
        "\t\t$2,",
        "\t];", 
        "}"
    ],
    "description": "Creates Model factory"
},

Solution

  • After much trial and error, I found that using \\$ will give me the desired results. So if I wanted $test in my snippet output, with test not being a tabstop, I would need \\$test snippet definition body:

    "factory" :{
        "prefix": "factory",
        "body": [
            "\\$factory->define($1, function (Faker\\Generator \\$faker){",
            "\treturn [",
            "\t\t$2,",
            "\t];", 
            "});"
        ],
        "description": "Creates Model factory"
    },