In VSCode, Exit Vim Insert Mode on Save
Asked Answered
V

3

14

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?

Vishnu answered 1/3, 2018 at 12:49 Comment(0)
H
17

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"
    }
Helpmate answered 2/4, 2018 at 14:12 Comment(3)
small typo in the name "saveand..." vs "saveAnd". After fixing this, it works greatPreindicate
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.Putrefy
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.Cheekbone
L
8

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.

Lamson answered 1/3, 2018 at 22:56 Comment(3)
Odd. I tested it using ctrl+n because I have something else bound to ctrl+s and that seemed to work for me.Lamson
I created an issue on the extension's github: github.com/VSCodeVim/Vim/issues/2411Lamson
Thanks, I've been binding it to [":", "w"] for the time being but takes an additional keystroke.Vishnu
R
-2

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.

Revealment answered 29/4, 2020 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.