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?
-
So basically you want to create file with random name in some default folrder (or /tmp)? It is looks like you need to make custom plugin for this kind of task and bind some hot-key/shortcut to make this quick save. I think that this feature is not worth it (:aiven– aiven2018-02-22 11:16:29 +00:00Commented Feb 22, 2018 at 11:16
-
It sounds like it would be easy to make a macro to do this. What command is your shortcut bound to?Mark– Mark2018-02-22 11:27:10 +00:00Commented Feb 22, 2018 at 11:27
-
@Mark I first thought this was a standard VS Code shortcut, but it's actually added by the Code Runner extension.H.Scheidl– H.Scheidl2018-02-24 00:58:13 +00:00Commented Feb 24, 2018 at 0:58
6 Answers
1 Comment
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 Comments
File > Preferences > Settings > Extensions > Run Code configuration > Save file before run.
(Visual Studio Code 1.32.3, extension Code Runner 0.9.8.)
1 Comment
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 Comment
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"
}

