3

I wonder, how can I save current file automatically in VSCode after leaving insert mode in Vim by pressing Esc key?

james dilaw
  • 39
  • 1
  • 8
Shersh
  • 9,019
  • 3
  • 33
  • 61
  • You could just turn on auto save in VSCode. `File > Toggle Auto Save` – tim-phillips Mar 14 '20 at 20:18
  • @Rohmer thanks for your comment! I tried this option a while ago, but it doesn't work as smooth as I wanted it to be. I described my workflow in detail under the corresponding GitHub issue: https://github.com/VSCodeVim/Vim/issues/3717#issuecomment-496089728 – Shersh Mar 15 '20 at 12:25
  • no problem! The reason I found this question was because VSCode / VSCodeVim was over saving, during insert mode, every time I entered characters. Maybe some things have changed since your post. – tim-phillips Mar 15 '20 at 16:35

2 Answers2

6

Replacing <Esc> with <Esc>:w<Enter> works, but this is probably a slightly more elegant solution (if you have other save commands you'd like to run for example).

    "vim.insertModeKeyBindingsNonRecursive": [
    {
        "before": [
            "<Esc>"
        ],
        "after": [
            "<Esc>"
        ],
        "commands": [
            "workbench.action.files.save"
        ]
    }
],
horace he
  • 417
  • 5
  • 10
2

You can add an insertModeKeyBinding to the Esc key in your settings.json like this:

"vim.insertModeKeyBindingsNonRecursive": [
    {
        "before": ["<Esc>"],
        "after": ["<Esc>", ":", "w", "<Enter>"]
    }
]

Note that this will ONLY save if you go from insert mode to normal mode with the Esc key.

edit: After a bit of testing, I found out you need to stay in insert mode for around 2 seconds after your last change for it to work, otherwise it won't see the <Esc> keystroke as a single event.

As a workaround, you could map to <leader><Esc>, if you need it instantly.

"vim.insertModeKeyBindingsNonRecursive": [
    {
        "before": ["<leader>", "<Esc>"],
        "after": ["<Esc>", ":", "w", "<Enter>"]
    }
]
Decay42
  • 802
  • 1
  • 9
  • 20
  • Thanks for your answer! However, this doesn't seem to work. First, I had to use `insertModeKeyBindingsNonRecursive`, otherwise `Esc` stops working at all. Second, even with `insertModeKeyBindingsNonRecursive` it doesn't save the file on `Esc`. – Shersh Apr 03 '19 at 07:38
  • I tested it on my end and it works as I would expect it (being in insert mode, hitting Esc: leaves insert mode to normal mode and saves the file). Can you elaborate on how it doesn't work for you exactly? Though you are right about the nonRecursive - I actually used nonRecursive myself, just didn't paste the right one. – Decay42 Apr 03 '19 at 07:43
  • You need to wait for ~2 seconds after your last keystroke in insert mode before pressing `Esc`. It just seems to be that way for binding `Esc` in insert mode. – Decay42 Apr 03 '19 at 08:00
  • Thanks! This seem to work now. Waiting for ~2 is a little bit unconvenient, but I guess it deserves separate issue in the Vim plugin. As a side question, how can I set `` is `\` and it just inserts the `\` character in the insert mode. – Shersh Apr 03 '19 at 08:49
  • `"vim.leader": " "` will set the leader to `Space`, so `Space+Esc` would be "leave insert mode and save". I don't think you can set the leader to the `Meta`-key. – Decay42 Apr 03 '19 at 08:56
  • The delay before this keybinding will be triggered can be controlled by `vim.timeout`. Set it lower to lower the delay needed. For example, `vim.timeout=100` will require only a 100ms gap. I think there's still a bug somewhere, but `vim.timeout` should resolve your issues. – horace he Jul 30 '19 at 06:20