Search code examples
visual-studio-codecode-snippets

VS Code - Snippet Transform text with spaces for test methods


I've had this useful snippet in Sublime Text 3 for a while, and I am trying to replicate it in VS Code but with no success.

<snippet>
    <content><![CDATA[
/** @test */
public function ${1/\s/_/g}()
{
    ${0:// ${1:type name of method with spaces}}
}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>phpunit</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.php</scope> -->
</snippet>

I've basically created the same snippet in VS Code but it complains that \s is an invalid escape character.

Picture showing snippet error in VS Code

Where am I going wrong? Is it missing support for finding space characters?

Would like to get this snippet working again as it's a useful time saver.


Solution

  • Just double-escape it:

    public function ${1/\\s/_/g}()
    

    and it'll work fine. See transform examples and escaping:

    The examples are shown within double quotes, as they would appear inside a snippet body, to illustrate the need to double escape certain characters.