Using TM_SELECTED_TEXT in my custom snippets

后端 未结 4 1230
醉话见心
醉话见心 2021-01-04 19:06

With the November 2016 (version 1.8) release of VSCode Snippet Variables are now supported, specifically TM_SELECTED_TEXT.

This makes me happy as I have used these h

4条回答
  •  佛祖请我去吃肉
    2021-01-04 19:34

    https://github.com/Microsoft/vscode/issues/17780

    as per this thread you can assign exact snippet to keybinding by providing args. keybinding example for bootstrap media queries

    {
        "key": "ctrl+alt+b",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "name": "bsup"
        }
    },
    {
        "key": "ctrl+alt+shift+b",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "name": "bsup_copy"
        }
    },
    

    snippet example

    "bsup": {
        "prefix": "bsup",
        "body": [
            "@include media-breakpoint-up(md){",
            "\t${TM_SELECTED_TEXT}",
            "}"
        ],
        "description": "Bootstrap media up"
    },
    "bsup_copy": {
        "prefix": "bsup_copy",
        "body": [
            "${1:${TM_SELECTED_TEXT}}",
            "@include media-breakpoint-up(md){",
            "\t${2:${TM_SELECTED_TEXT}}",
            "}"
        ],
        "description": "Bootstrap media up + copy selected text"
    },
    

    UPD: moreover - you can define snippet directly in keybindings.json which seems to be even more convenient for me in some cases

    {
      "key": "cmd+shift+c",
      "command": "editor.action.insertSnippet",
      "when": "editorTextFocus",
      "args": {
         "snippet": "console.log('${TM_SELECTED_TEXT}', $TM_SELECTED_TEXT$1);"
      }
    }
    

提交回复
热议问题