9

VSCodeVim uses different undo stack and it annoys me when after undoing all the unsaved changes in vim undo stack, VSCode still shows that file is not saved. For that reason, I'd like to use VSCode's undo stack and map "u" to "Ctrl+z". My keybinding is following:

{
    "key": "u",
    "command": "undo",
    "when": "editorTextFocus && !editorReadonly && vim.active && vim.mode != 'Insert'" 
}

The problem is that even though I specified that it shouldn't work when vim mode is Insert it still undoes the last change and inserts 'u'. Can anyone suggest what is the correct way to rebind undo?

Gama11
  • 31,714
  • 9
  • 78
  • 100
Nikita Karpinsky
  • 503
  • 4
  • 10
  • 3
    I've never used the plugin, but the github page seems to be quite different to your try. So it would be good for us if you specify why you are not using these snippet from github: `"vim.otherModesKeyBindingsNonRecursive": [ { "before": ["u"], "after": [""] } ]` – Doktor OSwaldo Nov 28 '17 at 13:01
  • [Related issue](https://github.com/VSCodeVim/Vim/issues/2574) – ggorlen Apr 23 '23 at 01:32

2 Answers2

14

I tried the Doktor OSwaldo's proposal but for some reason it doesn't work. However I managed to find a solution:

"vim.otherModesKeyBindingsNonRecursive": [ 
     { 
         "before": ["u"], 
         "after": [],
         "commands": [
             {
                 "command": "undo", 
                 "args": []
             }
         ] 
     } 
 ]
Nikita Karpinsky
  • 503
  • 4
  • 10
  • Can you say why `[]` is also needed in the `"commands"` section? – Michael May 26 '19 at 10:47
  • 3
    Very nice! the `"args": []` doesn't seem to be necessary, and `"otherModesKeyBindingsNonRecursive"` doesn't exist as an option anymore. I bound mine to `normalModeKeyBindings`. Also bound `` to `redo` to use VSCode's redo stack as well – dtasev Jan 13 '20 at 16:52
14

To piggyback off of dtasev's comment

... the "args": [] doesn't seem to be necessary, and "otherModesKeyBindingsNonRecursive" doesn't exist as an option anymore. I bound mine to normalModeKeyBindings. Also bound <C-r> to redo to use VSCode's redo stack as well

on this answer (and to be explicit with the JSON), this is what I put in my settings.json using vim.normalModeKeyBindingsNonRecursive as opposed to vim.normalModeKeyBindings:

"vim.normalModeKeyBindingsNonRecursive": [
        { 
            "before": ["u"], 
            "after": [],
            "commands": [
                {
                    "command": "undo", 
                    "args": []
                }
            ] 
        }, 
        { 
            "before": ["<C-r>"], 
            "after": [],
            "commands": [
                {
                    "command": "redo", 
                    "args": []
                }
            ] 
        } 
    ]
cigien
  • 57,834
  • 11
  • 73
  • 112
chansonnier
  • 141
  • 1
  • 2