2

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.

Talmacel Marian Silviu
  • 1,596
  • 2
  • 6
  • 14
  • this is a visual studio user snippet I created, but one part is not working. I know there are some quite knowdlegeable people here about visual studio code user snippet thats why I asked. – Talmacel Marian Silviu Sep 26 '20 at 15:27
  • use `Emmet: Wrap with abbreviation` there is a single and a multicursor variant (I have no clue why) and fill in the next box `.thedivclass` – rioV8 Sep 26 '20 at 16:09
  • You should show the output you want. It doesn't look like there is any need for the variable within the transform. – Mark Sep 26 '20 at 18:17
  • @Mark 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 – Talmacel Marian Silviu Sep 26 '20 at 18:24

1 Answers1

1

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

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/222148/discussion-on-answer-by-mark-is-it-possible-to-add-a-a-variable-within-a-placeho). – Samuel Liew Sep 27 '20 at 13:27