12

I use VSCode with the VSCodeVim extension. When in Insert Mode, I can press "CMD-S" to save. When I do, I would also like to exit Insert Mode automatically instead of pressing "ESC" as well.

Is this possible?

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
earllee
  • 285
  • 3
  • 11

3 Answers3

15

I was looking for a solution similar to this issue as well.

It appears that multiple commands cannot be mapped to key bindings in vscode; however, I've found an extension, macros, that let's you do this.

If you use the below steps with the macros extension, I believe that you'll have the solution you're looking for, until vscode implements something to address this feature request.

Step one, edit user settings with the below:

"macros": {
    "saveAndExitVimInsertMode": [
        "workbench.action.files.save",
        "extension.vim_escape"
    ]
}

Step two, edit keyboard bindings:

    {
    "key": "cmd+s",
    "command": "macros.saveAndExitVimInsertMode"
    }
Gabe Kopley
  • 16,281
  • 5
  • 47
  • 60
Kim
  • 856
  • 1
  • 11
  • 21
  • 1
    small typo in the name "saveand..." vs "saveAnd". After fixing this, it works great – iacobSon Apr 15 '19 at 19:08
  • Note that you may need to save the settings files via some means other than cmd+s because cmd-s will fail if you enable to keyboard binding before successfuly enabling the macro. – Gabe Kopley Nov 05 '19 at 18:02
  • 1
    This worked great...thanks a bunch! For future readers, to open your prerefernces and keyboard bindings: "...edit the underlying settings.json file by using the Open Settings (JSON) command" and the "Open Keyboard Shortcuts (JSON)" command as well. – brianz Jun 04 '21 at 20:25
7

VSCodeVim extension supports insert mode key bindings. Here is a link to the extension readme that describes this feature.

Here is a snippet you could put in your settings.json.

"vim.insertModeKeyBindingsNonRecursive": [
    {
        "before": [ "<C-s>" ],
        "commands": [
            { "command": "workbench.action.files.save" },
            { "command": "extension.vim_escape" }
        ]
    }
],

After reloading your vscode window you can pressing ctrl+s to exit insert mode and save the file.

alexriedl
  • 5,916
  • 3
  • 21
  • 17
0

open Show Command Palette (Ctrl+Shift+P). enter 'open keyboard shortcut' , search 'extension.vim_escape' and edit it to be 2 times caps lock key.

RobC
  • 22,977
  • 20
  • 73
  • 80