Visual Studio Code - Save file before build

11,467

Solution 1

This feature seems to have been implemented now:
Goto: File > Preferences > Settings
In "Search Settings" box at top, enter "Save".
Multiple matches will be listed.
Scroll down to: Task: Save Before Run and select: Always save all editors before running
Before leaving, take a moment to review some of the other language-specific options to see if any of them is of interest.

Solution 2

If you don't need to build but rather run the file, then you can adjust the Code Runner extension settings by adjusting your user settings. Settings can be accessed in file > preferences > settings (or with Ctrl+comma.)

To save the current file before running add the line below to your User Settings:

"code-runner.saveFileBeforeRun": true,

or to save all files before running, add this line:

"code-runner.saveAllFilesBeforeRun": true,

I also added this shortcut key(keybinding) to file > preferences > keyboard shortcuts > keybindings.json

{
    "key": "ctrl+enter",
    "command": "code-runner.run"
},

Now I can just hit Ctrl+Enter and all the magic happens.

Solution 3

Turns out there is an open feature request for this - https://github.com/Microsoft/vscode/issues/21342

The reverse thing (build on save) can be done, for example, with extension "Trigger Task on Save" (https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.triggertaskonsave)

Solution 4

Setting files.autoSave to afterDelay is probably your best alternative. You can combine it with decreasing autoSaveDelay so that files are autosaved quickly without you having to do anything.

"files.autoSave": "afterDelay",
"files.autoSaveDelay": 100,
Share:
11,467

Related videos on Youtube

Amomum
Author by

Amomum

Updated on July 02, 2022

Comments