【发布时间】:2020-02-12 16:45:55
【问题描述】:
我是 VS Code 的新手,我使用 Ctrl + enter 将代码运行到 python 交互式窗口中。我希望光标自动移动到下一行,以便我可以逐行浏览代码。
这个可以吗?
【问题讨论】:
-
我也想知道这个。您是否设法启用它?
-
不,我还没有找到解决办法。
我是 VS Code 的新手,我使用 Ctrl + enter 将代码运行到 python 交互式窗口中。我希望光标自动移动到下一行,以便我可以逐行浏览代码。
这个可以吗?
【问题讨论】:
正如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"
]
}
【讨论】:
P.Marres 建议的框架在“run and down”部分适用于我,但它会将命令发送到终端。下面的设置帮助我在交互式窗口中运行.py 文件“line and down”。
先决条件:
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"
]
}
【讨论】:
上面 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"
]
}
【讨论】: