2

I wrote my first snippet which would wrap the selected text in if() { selected text } block.

"if block - snippet": {
        "prefix": "if block - snippet",
        "body": [
            "if( $1 ) {",
            "$TM_SELECTED_TEXT",
            "}",
            "$0"
        ],
        "description": "if block - snippet"
    }

When I select the text and hit CTRL+SPACE it shows the intellisense but, when I start searching for my snippet "if block - snippet" instead of searching it clears-out the selected text and start writing the "if blo...." :P


One workaround is to have a dedicated keybinding to trigger snippets but I want it more implicit like in the intellisense suggestions itself. is it possible?

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225

3 Answers3

1

Update: vscode now "remembers the $TM_SELECTED_TEXT even though it appears to disappear as you write your snippet prefix. So your original snippet works as you expect. No need to use $CLIPBOARD.


Previous answer:

It is if you copy the selection to the clipboard first so you can use:

    "if block - snippet": {
        "prefix": "if block - snippet",
        "body": [
                "if( $1 ) {",
                "$CLIPBOARD",
                "}",
                "$0"
        ],
        "description": "if block - snippet"
}
Mark
  • 143,421
  • 24
  • 428
  • 436
  • @quetzalcoatl is right. I'm not looking for clipboard based pasting. – GorvGoyl Aug 06 '18 at 06:46
  • and everyone's right, I totally mistook VS <> VCode, sorry about that!! – quetzalcoatl Aug 06 '18 at 06:47
  • It is one extra step to copy your text after you selected it, then the snippet works exactly as you want. Let's see if there is another non-keybinding way to do it. – Mark Aug 06 '18 at 06:49
0

Once hitting CTRL+SPACE and intellisense shows, instead of searching the snippet by typing its name, use the UP/DOWN buttons to search it.

benshabatnoam
  • 7,161
  • 1
  • 31
  • 52
0

On the editor.action.showSnippets key binding, you can also do a when clause of editorHasSelection, and then you can use Ctrl+Space for both functionalities. It's not the nice inline snippet Intellisense, but it's closer to what we want.

Andrew
  • 5,839
  • 1
  • 51
  • 72