Dio*_*ogo 4 python spyder visual-studio-code vscode-settings
在使用 Python 的 VS Code 中,我们可以#%%在“Python 交互窗口”中运行一个“单元格”(以 开头的块)
我们可以在综合终端上做同样的事情吗?
我知道我们可以在Spyder 中做到这一点,其中终端通常总是一个 IPython 终端
Matlab 与它的终端以同样的方式工作。
我们可以在 VS Code 中做到这一点吗?
我在 GitHub 中打开了一个问题,@AdamAL 也打开了,但似乎他们无意这样做。这是其他用户的解决方法。
我之前回答过一个解决方法,它需要 2 个 VSCode 扩展而不使用智能命令jupyter.selectCellContents。
@AdamAL使用此命令共享了一个更好的解决方案,但需要注意的是光标会丢失位置并且仅在 Python 终端中聚焦。
@Maxime Beau指出将@AdamAL 解决方案扩展到活动终端(例如,可能是 IPython)
现在我正在接受所有答案并发布通用解决方案。当仅运行一个单元格时,此解决方案不会丢失光标位置。还有另一个命令可以运行单元格并前进到下一个单元格(如在 Jupyter 中),它对于活动终端(可能是 IPython)是通用的。
1.安装宏扩展:
这个扩展有一些多命令没有的额外命令(比如delay命令)
2.在settings.json中
"macros.list": {
    "runCellinTerminal": [
        "jupyter.selectCellContents",
        "workbench.action.terminal.runSelectedText",
        "cursorUndo",
        {"command": "$delay","args": {"delay": 100}},
        {"command": "workbench.action.terminal.sendSequence","args": { "text": "\n" }},
    ],
    "runCellinTerminaladvance": [
        "jupyter.selectCellContents",
        "workbench.action.terminal.runSelectedText",
        "cursorDown",
        {"command": "$delay","args": {"delay": 100}},
        {"command": "workbench.action.terminal.sendSequence","args": { "text": "\n" }},
    ],
}
OBS:cursorUndo将光标移回正确位置。cursorDown将光标移至下一个单元格。当终端是 IPython 终端时,命令delay和sendSequence \n很有用。
3. 在 keybinding.json 中:
{
    "key": "ctrl+alt+enter",
    "command": "macros.runCellinTerminal",
    "when": "editorTextFocus && jupyter.hascodecells"
},
{
    "key": "shift+alt+enter",
    "command": "macros.runCellinTerminaladvance",
    "when": "editorTextFocus && jupyter.hascodecells"
},
| 归档时间: | 
 | 
| 查看次数: | 733 次 | 
| 最近记录: |