27

VSCode on Github has an issue titled Running commands as tasks #11396. It involves running a VSCode command as an internal task in VSCode.

alexr00 commented on Dec 20, 2018 that:

You can now have commands in tasks.json and points to the following docs:

https://code.visualstudio.com/docs/editor/variables-reference#_settings-command-variables-and-input-variables

I've read through these docs and I am still unable to figure out how to do what I want. For starters, I'd like to create a simple task that runs the liveserver extention start code: extention.liveServer.goOnline

Anyone have any thoughts on what to try or where to look?

MilanRegmi
  • 499
  • 3
  • 12
Scott VanKirk
  • 445
  • 2
  • 5
  • 8

1 Answers1

29

So for instance you could do this:

    {
      "label": "run copyLinesDown command",
      //  "type": "shell",
      
      "command": "${command:editor.action.copyLinesDownAction}",

      // "command": "${command:extension.gist.open}"  // etc

      // "runOptions": {
      //   "runOn": "folderOpen"
      // }
    },

That is a task in tasks.json. When you run that task, the current line in the active editor will be copied down.

So I presume if you used

    "command": "${command:extension.liveServer.goOnline}",

in a task like the above that extension command should be run. (Check the spelling, is it extention or extension?)

See specifically command variables.

And then you can assign a keybinding to that task with (in keybindings.json):

    {
        "key": "ctrl+h",            // binding of your choice
        "command": "workbench.action.tasks.runTask",
        "args": "run copyLinesDown command"
    }
almaceleste
  • 397
  • 4
  • 11
Mark
  • 143,421
  • 24
  • 428
  • 436
  • 2
    Thanks for that. I kept trying to find a "type" for the command. It never occurred to me to just leave that out. Your example worked perfectly. Unfortunately, it doesn't seem to recognize commands from add-ons such as: command:extention.liveServer.goOnline – Scott VanKirk Aug 13 '19 at 20:41
  • 2
    Hmm, that is why I tried it with `extension.gist.open` a command from an installed extension and it worked just fine. It seems to work with/without the `shell`/`process` type for me. Did you try both versions with your command? It might need `process` for example. – Mark Aug 13 '19 at 21:23
  • Okay, I'm an idiot. I misspelled "extension", thank you Mark :) – Scott VanKirk Aug 15 '19 at 14:09