visual-studio-codevscode-snippets

VS Code end snippet by copying text or selecting text


I would like to modify my VS Code snippet so that after it completes, I have either copied a piece of the text output, or have that text selected.

I have the following snippet currently:

    "new section heading with link": {
        "prefix": "iheading",
        "body": [
            "$1## ${CLIPBOARD}",
            "",
            "This section: [${2:${CLIPBOARD}}](${TM_FILENAME}#${CLIPBOARD/(([\\w-]*)([^\\w\\s]*)*(\\s)*)/${2:/downcase}${4:+-}/g}) | [Back to top](#top)"
        ],
        "description": "Insert heading and link to section whose heading text is in the clipboard"
    }

I use this in markdown files. Let's say I have copied the text "Howdy" and am editing a document called "notes.md". After running the snippet, the output will be:

## Howdy

This section: [Howdy](notes.md#howdy) | [Back to top](#top)

What I would like is to end that snippet with the text [Howdy](notes.md#howdy) either copied to the clipboard, or selected - so that my next action can be to copy it.


Update Monday 13 January 2025, 09:12:07 am - mistakenly posted an old version of the macro. Corrected with the actual version.

Update Tuesday 14 January 2025, 09:59:36 am

Final snippet after advice from @Mark

"new section heading with link": {
    "prefix": "iheading",
    "body": [
        "## ${CLIPBOARD}",
        "",
        "This section: ${1:[${CLIPBOARD}](${TM_FILENAME}#${CLIPBOARD/(([\\w-]*)([^\\w\\s]*)*(\\s)*)/${2:/downcase}${4:+-}/g})} | [Back to top](#top)"
    ],
    "description": "Insert heading and link to section whose heading text is in the clipboard"
}

Solution

  • You could wrap that output you want to be ultimately selected with ${3:...} like so:

    "This section: ${3:[${2:${TM_SELECTED_TEXT}}](${TM_FILENAME}#${TM_SELECTED_TEXT/(([\\w-]*)([^\\w\\s]*)*(\\s)*)/${2:/downcase}${4:+-}/g})}"
    

    After triggering the snippet you will have to tab twice to get to that third tabstop - your text will now be selected - and Ctrl+C to copy it.

    Wrapping some output in a tabstop like ${3:...} will select it.


    If you didn't need those tabstops 1 and 2, you could automate the copying by creating a "macro-like" keybinding. First, modify your snippet to:

    "new section heading with link - from selected text": {
      "prefix": "iheading from selected text",
      "body": [
        "## ${TM_SELECTED_TEXT}",
        "",
        "This section: ${1:[${TM_SELECTED_TEXT}](${TM_FILENAME}#${TM_SELECTED_TEXT/(([\\w-]*)([^\\w\\s]*)*(\\s)*)/${2:/downcase}${4:+-}/g})}"
      ],
      "description": "Insert heading and link to section whose heading text is in the clipboard. Linked to keyboard shortcut"
    }
    

    where I removed your tabstops 1 and 2. Then create a keybinding that will insert the snippet and then copy the selected new tabstop 1 text:

    // in your keybindings.json
    
    {
      "key": "ctrl+shift+/",        // whatever keybinding you want
      "command": "runCommands",
      "args": {
        "commands": [
          {
            "command": "editor.action.insertSnippet",
            "args": {
              "name": "new section heading with link - from selected text"
            }
          },
          "editor.action.clipboardCopyAction"
        ]
      },
      "when": "editorHasSelection && editorTextFocus && !editorReadonly && editorLangId == markdown"
    }
    

    Run that keybinding on your selected text and voila your desired text has been copied to the clipboard.