38

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?

1 Answer 1

47

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" } 
Sign up to request clarification or add additional context in comments.

6 Comments

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
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.
Okay, I'm an idiot. I misspelled "extension", thank you Mark :)
If the command has some output, and you're using that task as a build task, VSCode might try to execute the output. For example, ${command:tinymist.exportCurrentPdf} will be replaced with the exported PDF's path. In this case, you can use "type": "shell" together with "args" to prevent this behavior: "label": "Tinymist: Export current PDF", "type": "shell", "command": "echo", "args": ["${command:tinymist.exportCurrentPdf}"]
I'm wondering if it's also possible to pass arguments to the command.
No, I think these are the only arguements that the runTask will take: task and type. See github.com/microsoft/vscode/issues/155350. It just matches to an existing task where the arguments live.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.