Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap Selection Snippet on Visual Studio Code (vscode)

Tags:

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?

like image 621
JohnnyAce Avatar asked Dec 20 '16 23:12

JohnnyAce


People also ask

How do you wrap long lines in VS Code?

You can toggle word wrap for the VS Code session with Alt + Z (macOS: Option (or Alt) ⌥ + Z ) or select View > Word Wrap from Menu.


1 Answers

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"   } } 
like image 160
Mark Avatar answered Oct 19 '22 17:10

Mark