How to use vim (key-bindings) with Visual Studio Code vim extension
Asked Answered
P

3

58

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?

Parhe answered 12/6, 2016 at 17:59 Comment(2)
Are you using github.com/VSCodeVim/Vim ?Jag
yes, I'm using github.com/VSCodeVim/Vim @sudobangbangParhe
B
124

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.

Blizzard answered 27/7, 2016 at 5:1 Comment(9)
With current version of VSCodeVim, it works with <Esc> instead of <esc>.Gymnast
can you add another key after "<Esc>" ? like this "after": ["<Esc>", "l"]?Glossary
This has worked for a long time for me, but I'm starting to see weird behavior (see here). 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?Legionnaire
@Legionnaire 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.Nikola
Nevermind. I got the fix from your github issue. link kudos!Nikola
@st003 Yes, I have mine set to run "after": ["<Esc>", ":", "w", "<Enter>"]Protect
It says these in the README of the GitHub link (github.com/VSCodeVim/Vim). Other helpful tips can be found there, too!Waggle
This not works for me: "vim.normalModeKeyBindingsNonRecursive": [ { "before": [ "n" ], "commands": [ "editor.action.nextMatchFindAction", ], "after": [ "<Esc>" ], }, { "before": [ "N" ], "commands": [ "editor.action.previousMatchFindAction", ], "after": [ "<Esc>" ], }, ],Stalinsk
this is working as of Jan 2024, but make sure this is inside curly braces { }Freshen
A
10

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

Autolysin answered 6/4, 2018 at 13:12 Comment(0)
J
3

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.

Jag answered 13/6, 2016 at 3:23 Comment(1)
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...Parhe

© 2022 - 2024 — McMap. All rights reserved.