Trigger Task on Save
Normally you would use a watch task to make something happen when a file changes, but sometimes you just need a task to run once on save. This extension lets you do that.
When the task is running a status bar item is shown with the task name. Clicking this will stop the task.
Note: This extension is for triggering vscode tasks, as defined in your
You can install the latest version of the extension via the Visual Studio Marketplace here.
The source code is available on GitHub here.
Task names are associated with filename globs, e.g.
In the example above, the task build will be triggered when any .h or .cpp files, except for ModuleVersion.h, are saved in the src subfolder. The task test will be triggered when any .h or .cpp files are saved in any subfolders ending with -ut.
Note: The array of globs for each task is processed in order and stops as soon as there is a match. Exclude globs (starting with
Environment variables and the vscode variable
The target task can also be overridden. In this case, any of the globs that match will trigger the selected task instead. This allows a build task to be run when changing a file in a dependent folder, for example. The selected task can be set using the Trigger Task On Save: Select Task command or by setting:
When a task has been selected, it is indicated on the status bar with a star. The selected task can be cleared by clicking the status bar item or using the command Trigger Task On Save: Clear Selected Task.
The extension can be temporarily enabled/disabled with:
A delay can be specified (in milliseconds) before triggering the task if you are experiencing problems with formatter timeouts, for example.
There is also an option restart the task if it is currently executing:
Notifications of tasks starting and ending can be enabled with:
If required, a status bar item allowing the extension to be enabled/disabled can be shown by setting this to true:
The following commands are provided, which can be accessed from the command palette (F1), or bound to keys:
When restarting tasks, there may be a delay before the running task finishes and the task restarts. This is necessary to avoid an error that gets generated (see issue 53331). Hopefully when this issue is fixed, the delay will no longer be apparent.
This is my first extension and is loosely based on Format On Save.