Python: Enable Ctrl+Enter in VS Code

    less than 1 minute read    

Enable Ctrl+Enter in VS Code

The method is offered in this discussion thread.

Currently it is manually set up by changing Macros of VS Code following the steps below:

  1. Install the macros extension macros geddski in VS Code.

  2. Access settings.json is by visiting “code preferences settings [ or ctrl+, ]” and then clicking the “{ }” icon in top right corner.
  3. Add the following code to settings.json.
"macros": {  // update: requires macros extension by publisher:"geddski" 
        "pythonExecSelectionAndCursorDown": [
            "python.execSelectionInTerminal", 
            //"vscode.window.activeTextEditor.show()",
            "cursorDown" 
        ]
    },
  1. Access keyboardsettings.json by visiting “code preferences keyboard shortcuts [ or ctrl+k ctrl+s ]” and then clicking the edit keybindings.json link.
  2. Add the following code to keybindings.json.
{
        "key": "ctrl+enter",
        //"command": [ "python.execSelectionInTerminal", "cursorDown" ],
        "command": "macros.pythonExecSelectionAndCursorDown",
        "when": "editorTextFocus && editorLangId == 'python'"
    }
  1. Once the macros extension and associated settings.json and keybindings.json content is in place the ctrl+enter when working with a .py file operates just like R Studio, VsCode R language support and Visual Studio R language support does where ctrl+enter executes whatever you have highlighted and then advances ide cursor to next line in source file.

Updated: