16

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
  • 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 (: Commented 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? Commented 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. Commented Feb 24, 2018 at 0:58

6 Answers 6

20

Simplest solution would be, File -> Auto Save. This is available in v1.29.x

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This saves continuously, not just when I wish to run my code.
15

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 VS Code, Run Code

3 Comments

where would i type the above snippet?
This one worked for me. @Nick or someone who is wondering where to put it, Command key + , and then click the three dots just under your 'search settings' bar and choose 'open settings.json'
In windows, click F1 -> Enter "Open Settings(JSON)". This will open the settings.json file. Now you can add the setting: "code-runner.saveAllFilesBeforeRun": true
4

On VS-CODE Version 1.47 , go to :

File -> Preferences -> Settings -> (Search setting with "save"), go to bottom & follow like provided screenshots.

VS-CODE : save before run

Comments

2

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

search saveFileBeforeRun in settings and check it.
0

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 could work, but after learning that the shortcut is added by Code Runner extension, I found a configuration for saving the file before execution.
-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 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.