﻿---
title: Using tasks.json
date: 2024-01-26
excerpt: A brief note on using VS Code tasks.json to compile, run, package, and automate common project tasks.
tags:
  - VSCode
  - Productivity
updated: 2026-07-08 07:44:04
lang: en
i18n:
  cn: /tasks_json_usage
  translation: 2
---

## Intro

What does `tasks.json` do? The official explanation is:

> Lots of tools exist to automate tasks like **linting**, building, packaging, testing, or deploying software systems.
> Tasks in VS Code can be configured to run scripts and start processes so that many of these existing tools can be used from within VS Code without having to enter a command line or write new code.
> _**linting**: Linting is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs_ > [_Integrate with External Tools via Tasks_](https://code.visualstudio.com/Docs/editor/tasks)

In simple terms, `tasks.json` lets you write scripts inside VS Code to run tasks such as compiling code, running code, packaging code, and so on.

## properties

The task's properties have the following semantic:

- **label**: The task's label used in the user interface.
- **type**: The task's type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute.
- **command**: The actual command to execute.
- **windows**: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
- **group**: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
- **presentation**: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.
- **options**: Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.
- **runOptions**: Defines when and how a task is run.

## Examples

`tasks.json` is quite feature-rich. This post only lists a few commonly used features. For more, see the [official documentation](https://code.visualstudio.com/Docs/editor/tasks).

```json
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "compile c file(Windows)",
      "command": "gcc",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "group": "build"
    },
    {
      "type": "shell",
      "label": "compile c file(Linux)",
      "command": "gcc",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.out"
      ],
      "group": "build"
    },
    {
      "type": "shell",
      "label": "run exe file(Windows)",
      "command": "${fileDirname}\\${fileBasenameNoExtension}.exe"
    },
    {
      "type": "shell",
      "label": "compile and run c file(Windows)",
      "command": "gcc -g ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe && ${fileDirname}\\${fileBasenameNoExtension}.exe",
      "problemMatcher": ["$gcc"]
    }
  ]
}
```

- `compile c file(Windows)` and `compile c file(Linux)`: compile C files.
- `run exe file(Windows)`: run the compiled exe file directly when the code has not changed, avoiding recompilation.
- `compile and run c file(Windows)`: compile and run a C file.

_ps: Set a shortcut for `workbench.action.tasks.runTask` to show the task list._
