28

I want to create an snippet when triggered it will surround the given text. Currently my snippet is:

{ 
  "Function Creator Helper": {
    "prefix": "_w",
    "body": [
      "public function $TM_SELECTED_TEXT () {",
      "  $1",
      "}",
    ],
    "description": "Creates a function given the text selection"
  }
}

This results on:

Wrapping snippet

What I do is:

  1. Select the text.
  2. Write the prefix (_w)
  3. Press Tab

This results on:

public function  () {

}

But I was expecting

public function person () {

}

Any ideas on how can I make this snippet or how can I triggered it correctly?

JohnnyAce
  • 3,569
  • 9
  • 37
  • 59
  • If I follow yous instructions and the correct answer, and select (highlight) the text to wrap and then type my prefix (rf) it will replace the text, not wrap it. – Eduard Oct 08 '18 at 12:02

7 Answers7

24

See https://stackoverflow.com/a/48676522/836330 Your example will work, as of vscode v1.49, as you have it. Vscode snippets have been updated to "remember" your selected text even though you seemingly overwrite it with your snippet prefix.

selected text wrap


Older answer:

You can use $TM_SELECTED_TEXT if you trigger it with a hotkey:

{
  "key": "cmd+k 1",
  "command": "editor.action.insertSnippet",
  "when": "editorTextFocus",
  "args": {
    //  "langId": "csharp",
    "name": "Function Creator Helper"
  }
}
Mark
  • 143,421
  • 24
  • 428
  • 436
14

The currently selected text is exposed as ${TM_SELECTED_TEXT}, not $TM_SELECTED_TEXT.

edit: As commented below, this is not the case for this particular use-case

MatthewWilkes
  • 1,048
  • 8
  • 15
  • 2
    This is not true, both versions work, see https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables. If you are doing transforms on those variables then you need to use the ${} form obviously but otherwise either is fine. – Mark Jan 22 '19 at 09:49
  • `${TM_SELECTED_TEXT}` worked for me in version 1.32.3, but `${selectedText}` didn't – Carl Walsh May 20 '19 at 19:23
4

I was just struggling with this myself. In order to get this to work, the only thing you have to do is press F1, run the Insert Snippet command and then select your snippet from the list.

Jan De Dobbeleer
  • 566
  • 4
  • 12
2

${TM_SELECTED_TEXT} does not work for me either.

${selectedText} has been added as a Snippet Editor Variable: https://github.com/Microsoft/vscode/pull/39483#issuecomment-383552677

Example:

"JS Block Quote": {
    "prefix": "c2",
    "body": [
        "/* ${selectedText} */",
    ],
    "description": "JS Block Quote"
}

At this time it is not correctly documented: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables

NOTE: In a multi-line selection, ${selectedText} is truncated to the first line. An alternative is to use the the clipboard and the ${CLIPBOARD} variable. An extra step :(

Mitch
  • 641
  • 1
  • 10
  • 13
  • https://code.visualstudio.com/docs/editor/variables-reference : ${selectedText} is a task or launch variable only. It does not work in snippets which have their own variables. It wouldn't make sense to have ${selectedText} and ${TM_SELECTED_TEXT} as both variables for snippets. – Mark Jul 23 '19 at 22:06
2

from Mitches example:

"JS Block Quote": {
    "prefix": "c2",
    "body": [
        "/* $TM_SELECTED_TEXT */",
    ],
    "description": "JS Block Quote" }

from the article: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables the docs must have been ahead of the release.

This works fine in vscode v1.30.2

brian burnett
  • 83
  • 1
  • 9
1

If somebody wants to know, it works like that for me :

I created two same snippet which only matches when I'm in html or php file (just create two snippets files in your snippets folder "php.json" and "html.json" it works for any languages) and added this code inside :

   "unicommentary": {
        "prefix": "unicommentary",
        "body": "<?php /* ${TM_SELECTED_TEXT} */ ?> ${0}",
        "description": "Creates a universal comment to disable both html and php."
    }

The ${TM_SELECTED_TEXT} tag works when you select some text and trigger your snippet by the Insert Snippet command, you can't just write on selected text.

When you want to use this, select the text you want in your snippet, press Ctrl + Shift + P and select Insert snippet then, type the name of your snippet, press enter and there you go !

0

Press F1. choose the emmet option. And then use the emmet short cut. For example, to insert your selected text into a span with class decimal, you type span.decimal. That'll do the trick

  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34329237) – Narendranath Reddy May 09 '23 at 08:35