Search code examples
visual-studio-codevscode-snippets

Can a VSCode snippet make three substitutions?


I have written a VSCode snippet that makes two substitutions to the clipboard content. For instance, this snippet changes "a" characters into "x" and "e" characters into "y"

    "t2": {
        "prefix": "t2",
        "body": [
            "${CLIPBOARD/(a)|(e)/${1:?x:y}/g}"
        ],
        "description": "Makes two substitutions to the clipboard"
    },

Using this snippet, if I cut the text "This snippet changes the clipboard" and I execute the snippet, the pasted text is "This snippyt chxngys thy clipboxrd". My question is: Is it possible to create a snippet that applies three substitutions? For instance, "a" into "x", "e" into "y" and "i" into "z", obtaining "Thzs snzppyt chxngys thy clzpboxrd" in the example above.


Solution

  • Yes, you can change as many as you want. I made this snippet for another answer:

    "color conditional": {
        "prefix": "_hex",
        "body": [
    
          "let color = '${1};",      
          "let hex = '${1/(white)|(black)|(red)/${1:+#fff}${2:+#000}${3:+#f00}/}';" //works      
        ],
        "description": "conditional color"
      },
    

    color conditional snippet demo

    See vscode if/else conditions in user defined snippet.

    for one example but in your case try:

    "${CLIPBOARD/(a)|(e)|(i)/${1:+x}${2:+y}${3:+z}/g}"

    instead of using the if/else conditional, you can use any number of if's.

    snippet multiple replacement