VS Code Python move to next line on run ctrl + enter

Question:

I’m new to VS Code and running code into the python interactive window using Ctrl + enter. I would like the cursor to move to the next line automatically so I can go through the code line-by-line.

Can this be done?

Asked By: kav

||

Answers:

As explained in this blog post, here is how you can make VS Code run code selections using Ctrl + enter and move to next line:

###############################
# 1. Install extension "macros" in Visual Code
#
# Hit View on top menu
# Search for extension named "macros" (by geddski)
# Install "macros" extension
#
###############################


###############################
# 2. Add code below to keybindings.json
#
# Hit <Ctrl> + <Shift> + <P>
# Enter in search bar: JSON
# Select Open keyboard shortcuts
#
###############################

{
        "key": "ctrl+enter",
        "command": "macros.pythonExecSelectionAndCursorDown",
        "when": "editorTextFocus && editorLangId == 'python'"
    }


###############################
# 3. Add code below to settings.json
#
# Hit <Ctrl> + <Shift> + <P>
# Enter in search bar: JSON
# Select Open settings 
#
###############################

"macros": {  // Note: this requires macros extension by publisher:"geddski" 
        "pythonExecSelectionAndCursorDown": [
            "python.execSelectionInTerminal", 
            "cursorDown" 
        ]
    }
Answered By: P.Marres

The answer above by P.Marres showing this blog post’s code was great! I needed this for the windows subsystem in linux.

Here is how you would do it for the Windows Subsystem for Linux in Visual Studio Code:

###############################
# 1. Install extension "macros" in Visual Code
#
# Hit View on top menu
# Search for extension named "macros" (by geddski)
# Install "macros" extension
#
###############################


###############################
# 2. Add code below to keybindings.json
#
# Hit <Crtl> + <Shift> + <P>
# Enter in search bar: JSON
# Select Open keyboard shortcuts
#
###############################

{
        "key": "ctrl+enter",
        "command": "macros.ExecSelectionAndCursorDown",
    }


###############################
# 3. Add code below to settings.json
#
# Hit <Crtl> + <Shift> + <P>
# Enter in search bar: JSON
# Select Open settings 
#
###############################

"macros": {  // Note: this requires macros extension by publisher:"geddski" 
            "ExecSelectionAndCursorDown": [
                "workbench.action.terminal.runSelectedText", 
                "cursorDown" 
            ]
        }
Answered By: Stephan

The framework suggested by P.Marres works for me on the "run and down" part, but it sends commands to the terminal. The below setting helped me to run a .py file "line and down" in the interactive window.

prerequisite:

  • The macros extension by publisher:"geddski"
  • The Jupyter extension (installed by default with the Python extension, replacing the older python.datascience)

In keybindings.json, include:

[
    {
        "key": "ctrl+enter",
        "command": "macros.pythonExecSelectionAndCursorDown",
        "when": "editorTextFocus && editorLangId == 'python'"
    },
    {
        "key": "ctrl+enter",
        "command": "macros.jupyterExeSelThenCursorDown",
        "when": "editorTextFocus && isWorkspaceTrusted && jupyter.ownsSelection && !findInputFocussed && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'"
    },
    {
        "key": "ctrl+enter",
        "command": "macros.pythonExecSelectionAndCursorDown",
        "when": "editorTextFocus && !findInputFocussed && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'"
    },
    {
        "key": "ctrl+enter",
        "command": "macros.jupyterRunCellThenCursorDown",
        "when": "editorTextFocus && isWorkspaceTrusted && jupyter.hascodecells && !editorHasSelection && !notebookEditorFocused"
    },
    {
        "key": "ctrl+enter",
        "command": "macros.interactiveExe",
        "when": "resourceScheme == 'vscode-interactive'"
    }
]

In settings.json include:

"macros": {  
        "pythonExecSelectionAndCursorDown": [
            "python.execSelectionInTerminal",
            "cursorDown"
        ],
        "jupyterExeSelThenCursorDown":[
            "jupyter.execSelectionInteractive",
            "cursorDown"
        ],
        "jupyterRunCellThenCursorDown":[
            "jupyter.runcurrentcelladvance",
            "cursorDown"
        ],
        "interactiveExe":[
            "interactive.execute",
            "cursorDown"
        ]
    }
Answered By: Yuanhong Song

To prevent ctrl + enter to launch a python terminal when you are using a jupyter notebook add resourceExtname parameter to the when clause:

{
    "key": "ctrl+enter",
    "command": "macros.pythonExecSelectionAndCursorDown",
    "when": "editorTextFocus && editorLangId == 'python' && resourceExtname == '.py'"
}

This will prevent the overwriting of the default jupyter notebook behaviour, which caused me some headache

Answered By: Kaan Yolsever

I made an extension for this at https://github.com/insilica/vscode-pycmd. Install it from the vscode extensions view by searching for pycmd.

After installing, create a Ctrl+Enter hotkey for executing Python code in the active terminal. These are some reasonable settings for keybindings.json:

{
    "key": "ctrl+enter",
    "command": "extension.pycmd",
    "when": "editorTextFocus && editorLangId == 'python'"
}

pycmd uses the python abstract syntax tree to find the next complete expression following your cursor.

Answered By: Thomas Luechtefeld

This is actually part of the new default behaviour for versions of the Python extension equal to or greater than 2023.20.0. See Prevent VSCode from skipping to next non empty line when executing current line with Shift+Enter in Python and related release notes for more info. If you want to disable it, use the python.REPL.enableREPLSmartSend setting.

Prior to version 2023.20.0 of the Python extension, for running code in the terminal when there are no code cells (#%% cell magic), and running in an interactive windows when there are, as of VS Code 1.74, you can do this as follows using runCommands (put this in your keybindings.json file, which you can open with Preferences: Open Keyboard Shortcuts (JSON) in the command palette):

{
    "key": "", // TODO
    "command": "runCommands",
    "args": {
        "commands": ["python.execSelectionInTerminal", "cursorDown"],
    },
    "when": "editorTextFocus && !findInputFocussed && !jupyter.hascodecells && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'",
},
{
    "key": "", // TODO
    "command": "runCommands",
    "args": {
        "commands": ["jupyter.execSelectionInteractive", "cursorDown"],
    },
    "when": "editorTextFocus && isWorkspaceTrusted && jupyter.hascodecells && !editorHasSelection && !notebookEditorFocused"
}

Though note that in Notebook editors, notebook.cell.executeAndSelectBelow is bound by default to shift+enter, and similarly, to jupyter.runcurrentcelladvance in a Python file with code cells.


Note that this was raised as a feature-request before, but it was declined with the reply:

Thanks for the suggestion! We talked about it with the team and we have unfortunately decided we will not be moving forward with this idea. We think there isn’t an enough widespread need for this to warrant the maintenance cost for the feature.

There’s a subsequent re-raising of the request which is still open at the time of this writing: Go to next line #14695. I suggest that you give that issue ticket a thumbs up to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

Answered By: starball
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.