🐥

VSCode Vim拡張 Jupyter拡張 でノーマルモードに移行して実行する keybindings.json 設定

に公開

問題

VSCode の Vim 拡張を使っていると、 .ipynb ファイルでセルを実行する際に、挿入モードのまま実行してしまい、実行後のセルでモードが混在してしまいややこしくなる.
ここで考えているのは Ctrl+Enter, Alt+Enter, Shift+Enter

解決方法

この問題を回避するために、実行前に自動的にノーマルモードへ戻すキーバインドを設定する.
keybindings.jsonに以下を追加する

    {
        "key": "ctrl+enter",
        "command": "runCommands",
        "args": {
            "commands": ["extension.vim_escape", "notebook.cell.executeAndFocusContainer"]
        }, 
        "when": "notebookCellListFocused && editorTextFocus && vim.mode == 'Insert'"
    },
    {
        "key": "alt+enter",
        "command": "runCommands",
        "args": {
            "commands": ["extension.vim_escape", "notebook.cell.executeAndInsertBelow"]
        }, 
        "when": "notebookCellListFocused && editorTextFocus && vim.mode == 'Insert'"
    },
    {
        "key": "shift+enter",
        "command": "runCommands",
        "args": {
            "commands": ["extension.vim_escape", "notebook.cell.executeAndSelectBelow"]
        }, 
        "when": "notebookCellListFocused && editorTextFocus && vim.mode == 'Insert'"
    }

Discussion