55

I just started using Visual Studio Code and think it's really great. Also installed the vim extension, but I'm struggling with mapping esc to a another key.

Normally I have this:

:imap jj <Esc>

And I can see that VS Code has a keybindings.json file. I tried this:

[{
    "key": "jj",
    "command": "vim.Esc",
    "when": "editorTextFocus"
}]

Also there is a settings.json file, so I tried:

{
  "vim.keyboardLayout": "en-US (QWERTY)",
    "vim.insertModeKeyBindings": {
        "j": "vim.Esc"
    }
}

Also did not work. So does anyone know how to use the a vim extension with VS Code where I can map jj to Esc or something else to Esc perhaps?

Gama11
  • 31,714
  • 9
  • 78
  • 100
ganjan
  • 7,356
  • 24
  • 82
  • 133

3 Answers3

120

Add the following to settings.json (open the Command Pallete and search for "User Settings"):

"vim.insertModeKeyBindings": [
     {
         "before": ["j", "j"],
         "after": ["<esc>"]
     }
]

That should do it.

thedayturns
  • 9,723
  • 5
  • 33
  • 41
  • 1
    With current version of VSCodeVim, it works with `` instead of ``. – jarandaf Mar 07 '18 at 07:16
  • 1
    can you add another key after "" ? like this ```"after": ["", "l"]```? – sjt003 Apr 16 '18 at 19:51
  • 2
    This has worked for a long time for me, but I'm starting to see weird behavior ([see here](https://www.dropbox.com/s/a9mqqyfzykrh75w/Screen%20Recording%202020-05-02%20at%205.34.02%20PM.mov?dl=0)). The first j seems to stay in insert mode and then the second j moves back a character and sometimes deletes the line. Any idea what might cause this? – gwintrob May 03 '20 at 00:41
  • @gwintrob All of the sudden I get this mess too. Did the vim extension auto update or is this a vscode problem? I remapped to other double letters (ie. "x", "x") and still get the same behavior. – Isaac Pak May 03 '20 at 03:38
  • Nevermind. I got the fix from your github issue. [link](https://github.com/VSCodeVim/Vim/issues/4787) kudos! – Isaac Pak May 03 '20 at 03:51
  • @st003 Yes, I have mine set to run `"after": ["", ":", "w", ""]` – Shadoath Nov 29 '20 at 00:33
  • It says these in the README of the GitHub link (https://github.com/VSCodeVim/Vim). Other helpful tips can be found there, too! – Nathan majicvr.com Jun 27 '22 at 16:44
  • This not works for me: "vim.normalModeKeyBindingsNonRecursive": [ { "before": [ "n" ], "commands": [ "editor.action.nextMatchFindAction", ], "after": [ "" ], }, { "before": [ "N" ], "commands": [ "editor.action.previousMatchFindAction", ], "after": [ "" ], }, ], – luochen1990 Jul 25 '23 at 14:29
8

If you running on Linux and have used setxkbmap to remap Esc to Caps-Lock and have problems, I suggest the following workaround Fix for Esc remapping.

The solution is to add the following to your User Settings

"keyboard.dispatch": "keyCode"

You should save and restart after that enter image description here

fuadj
  • 434
  • 4
  • 10
4

From this issue, I learned that you can use something like

{ "key": "j j", "command": "extension.vim_esc", "when": "editorTextFocus" },

But it does come with a problem of not being able to use j for movement.

PS. I know this is not a complete answer but something to get going.

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
  • 2
    This almost works, but when I type j the application freeze waiting for second key.. Have to make sure it only happen when I'm in vim insert mode... – ganjan Jun 13 '16 at 08:55