Search code examples
regexvisual-studio-codecode-snippetsvscode-snippets

Is it possible to add a a variable within a placeholder transform in Visual Studio Code?


I am looking to write an visual studio code user snippet like this:

 "Surround With Div": {
    "prefix": "sdiv",
    "body": ["${1/(.*)/<div class=\"${1}\">${TM_SELECTED_TEXT}<\\/div>/}"],
    "description": "Surround With Div"
  }

But it does not seem to be working. Is there any way I can make something like this work? Using the variable inside the result of the regex transform?

To clarify for people:

What I was trying to achieve is this:

Select a portion of html document

Type sdiv over it to get the snippet

write something like -> header for the class name...

then when I would hit TAB I would get a div with the class header and the content I first selected inside it

But I realized you can't do something like that...could have done it with $CLIPBOARD but had to copy it first

So I decided to do something a bit different using a keybinding instead.


Solution

  • It is actually more straightforward than it seems. You do not need a transform at all - you can't put a variable like $TM_SELECTED_TEXT or $CLIPBOARD inside a transform anyway.

    "Surround With Div": {
      "prefix": "sdiv",
      "body": [
        "<div class=\"${1}\">${TM_SELECTED_TEXT}</div>"],
      "description": "Surround With Div"
     }
    

    selected text snippet

    selected text and emmet wrap demo