【问题标题】:VS Code move to next line on run ctrl + enterVS Code 在运行时移动到下一行 ctrl + enter
【发布时间】:2020-02-12 16:45:55
【问题描述】:

我是 VS Code 的新手,我使用 Ctrl + enter 将代码运行到 python 交互式窗口中。我希望光标自动移动到下一行,以便我可以逐行浏览代码。

这个可以吗?

【问题讨论】:

  • 我也想知道这个。您是否设法启用它?
  • 不,我还没有找到解决办法。

标签: python vscode-settings


【解决方案1】:

正如this blog post 中所述,您可以使用 Ctrl + enter 让 VS 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.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" 
        ]
    }

【讨论】:

  • 在删除已经使用 ctrl+enter 的预设快捷方式(“editor.action.insertLineAfter”)后,这对我有用。它没有被这个 keybindings.json 或 settings.json 覆盖。
【解决方案2】:

P.Marres 建议的框架在“run and down”部分适用于我,但它会将命令发送到终端。下面的设置帮助我在交互式窗口中运行.py 文件“line and down”。

先决条件:

  • 发布者的宏扩展:“geddski”
  • Jupyter 扩展(默认与 Python 扩展一起安装,替换旧的 python.datascience

在 keybindings.json 中,包括:

[
    {
        "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'"
    }
]

在 settings.json 中包括:

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

【讨论】:

    【解决方案3】:

    上面 P.Marres 的答案显示 this blog 帖子的代码很棒!我需要这个用于 linux 中的 windows 子系统。

    以下是在 Visual Studio Code 中为 Linux 的 Windows 子系统执行此操作的方法:

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

    【讨论】:

      猜你喜欢
      • 2022-07-15
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 2017-01-10
      相关资源
      最近更新 更多