Mark
Mark

Reputation: 68

VSCode add snippet on next line below selection

Is it possible to move the cursor on to the next line? Basically I want to select a variable, type my shortcode and have it console log it on a new line. So

var foo = 'bar';

becomes

var foo = 'bar';
console.log("foo", foo); 

So far I've got

"Console log with variables": {
        "prefix": "cll",
        "body": ["$TM_SELECTED_TEXT",
            "console.log(\"$TM_SELECTED_TEXT\", $TM_SELECTED_TEXT);"],
        "description": "Console log from within js"
    },

but it overwrites the variable (obviously)

Upvotes: 2

Views: 821

Answers (1)

Mark
Mark

Reputation: 181339

If you wanted to do it in two steps - which I think you are trying to avoid - you could use this keybinding:

{
  "key": "alt+i",                   // whatever keybinding you want
  "command": "editor.action.insertSnippet",
  "args": {
    "snippet": "console.log (\"$CLIPBOARD\", $CLIPBOARD);"
  }
}

which requires you to copy the variable first and then move down a line, and then trigger the keybinding (or it could just be a snippet with no keybinding).


To do it in one step:

Here is a solution using an extension I wrote, Find and Transform,. Put this into your keybindings.json:

{
  "key": "alt+i",                   // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "preCommands": [
      "editor.action.clipboardCopyAction",     // copy it first
      "editor.action.insertLineAfter"          // insert a blank line below
    ],
    "replace": "console.log (\"${CLIPBOARD}\", ${CLIPBOARD});"
  }
}

Upvotes: 3

Related Questions