I’m writing Python scripts in Visual Studio Code, and I execute them with Ctrl+Alt+N, a shortcut added by the extension Code Runner. Is there a way to force VS Code to save the .py
before running, thus eliminating the extra step to save the file?
3
Simplest solution would be, File -> Auto Save. This is available in v1.29.x
1
The shortcut is added by the Code Runner extension, which has an option to save the current file before execution.
{
"code-runner.saveFileBeforeRun": false
}
More options are available in the documentation
3
On VS-CODE Version 1.47 , go to :
File -> Preferences -> Settings -> (Search setting with “save”), go to bottom & follow like provided screenshots.
File > Preferences > Settings > Extensions > Run Code configuration > Save file before run.
(Visual Studio Code 1.32.3, extension Code Runner 0.9.8.)
1
I suggest using the extension macros as I suggested in my comment. It appears to be more powerful than the multi-command
extension. macros
will take command arguments and also work with your snippets for instance. It is not clear to me from the scant documentation that multi-command
will do these things.
In your case:
"macros": {
"chooseNameHere": [
"workbench.action.files.save",
"your run python script here command"
]
}
I don’t know what command your Ctrl–Alt–N, it isn’t one of the defaults so must be added by a python extension. You can find it by searching in your keybindings and use that command. And then any keybinding you like to this macro:
{
"key": "ctrl+shift+.",
"command": "macros.yourMacroNameHere"
},
1
This is currently not possible, but the corresponding feature request is tracked here.
Update
You can use multi-command. To do so install the extension and add the following to your settings.json
:
"multiCommand.commands": [
{
"command": "multiCommand.SaveAllAndDebug",
"sequence": [
"workbench.action.files.saveAll",
"workbench.action.debug.start"
]
}
]
And then add your custom command to the keybindings.json
:
{
"key": "Ctrl+Alt+N",
"command": "multiCommand.SaveAllAndDebug",
"when": "!inDebugMode"
}
1