Visual Studio Code - Save file before build
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,
Related videos on Youtube
Amomum
Updated on July 02, 2022Comments
-
Amomum over 1 year
Is it possible to automatically save all changed files before build? It's a bit tedious to press Ctrl+s and then Ctrl+Shift+b all the time.
-
Amomum over 6 yearsSince I'm using an extension to reformat my code after save, using autosave could be a pain because code will literally slip away from my cursor.
-
Cameron Tacklind about 5 yearsAs of sometime in 2017, running tasks does automatically save all: github.com/Microsoft/vscode/issues/21342#issuecomment-343141199 The issue will remain open until the behavior can be controlled: github.com/Microsoft/vscode/issues/21342#issuecomment-432593879