Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple commands/tasks with Visual Studio Code

I have a local folder that I use as a scratch pad for multiple little sample and toy pieces of code. I store a host of python, C++, shell scripts etc. in this directory.

I'm using Visual Studio Code (on OS X) and am looking into its tasks to run/compile the code snippets without having to switch to a terminal.

For example, I found this following task will run python on the currently open file.

// A task runner that runs a python program {     "version": "0.1.0",     "command": "/usr/bin/python",     "args": ["${file}"] } 

This task will use python as the task runner irrespective of the type of file I'm currently editing.

How do I implement a task to run a command based on the file type (or select between multiple commands)? I.e. if I'm editing a C++ file, it will run clang++.

  • If I can't do it based on file type; are there any alternatives to this?
  • An alternative would be; are multiple commands supported?
like image 599
Niall Avatar asked Jul 29 '15 13:07

Niall


People also ask

How do I run multiple commands in VS Code?

You can always mark your 2 most common commands with isBuildCommand and isTestCommand to run them via cmd + shift + b or cmd + shift + t respectively. This answer has some helpful information that might be useful to you as well.

How do I make multiple cursors in VS Code?

VS Code supports multiple cursors for fast simultaneous edits. You can add secondary cursors (rendered thinner) with Alt+Click. Each cursor operates independently based on the context it sits in. A common way to add more cursors is with Shift+Alt+Down or Shift+Alt+Up that insert cursors below or above.

How do I run a build task in VS Code?

Tip: You can run your task through Quick Open (Ctrl+P) by typing 'task', Space and the command name.

How do I run a grunt task in Visual Studio Code?

The most recen update to VSC has auto-detects grunt (and gulp tasks) so you can now just use cmd+p then type task (notice the space at the end) and VSC will show you the available tasks to run. Show activity on this post. You can also modify the existing tasks option to add specific tasks to run in your build.


2 Answers

You can always use bash as your task runner and then assign arbitrary terminal commands as your tasks.

{     "version": "0.1.0",     "command": "bash",     "isShellCommand": true,     "showOutput": "always",     "args": [         "-c"     ],     "tasks": [         {             "taskName": "My First Command",             "suppressTaskName": true,             "isBuildCommand": true,             "args": ["echo cmd1"]         },         {             "taskName": "My Command Requiring .bash_profile",             "suppressTaskName": true,             "args": ["source ~/.bash_profile && echo cmd2"]         },         {             "taskName": "My Python task",             "suppressTaskName": true,             "args": ["/usr/bin/python ${file}"]         }     ] } 

A few notes on what is happening here:

  • Using bash -c for all tasks by putting it in args list of the command so that we can run arbitrary commands. The echo statements are just examples but could be anything executable from your bash terminal.
  • The args array will contain a single string to be passed to bash -c (separate items would be treated as multiple arguments to the bash command and not the command associated with the -c arg).
  • suppressTaskName is being used to keep the taskName out of the mix
  • The second command shows how you can load your ~/.bash_profile if you need anything that it provides such as aliases, env variables, whatever
  • Third command shows how you could use your Python command you mentioned

This will not give you any sort of file extension detection, but you can at least use cmd+p then type "task " to get a list of your tasks. You can always mark your 2 most common commands with isBuildCommand and isTestCommand to run them via cmd+shift+b or cmd+shift+t respectively.

This answer has some helpful information that might be useful to you as well.

like image 128
bingles Avatar answered Oct 02 '22 10:10

bingles


The simplest way would be to add them separated by ; (or &&) in a shell:

tasks.json:

{     "version": "2.0.0",     "tasks": [         {             "label": "test",             "type": "shell",             "command": "cd ~/dev/xxxx;source ~/dev/yyyy;ls",         }     ] } 
like image 33
Adriel Jr Avatar answered Oct 02 '22 12:10

Adriel Jr