VS Code move to next line on run ctrl + enter

Viewed 11796

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?

4 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 <Crtl> + <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 <Crtl> + <Shift> + <P>
# Enter in search bar: JSON
# Select Open settings 
#
###############################

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

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"
        ]
    }

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" 
            ]
        }

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

Related