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

55,282

Solution 1

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.

Solution 2

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

Solution 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.

Share:
55,282

Related videos on Youtube

ganjan
Author by

ganjan

Updated on February 20, 2020

Comments

  • ganjan
    ganjan about 4 years

    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?

    • sudo bangbang
      sudo bangbang almost 8 years
      Are you using github.com/VSCodeVim/Vim ?
    • ganjan
      ganjan almost 8 years
      yes, I'm using github.com/VSCodeVim/Vim @sudobangbang
    • Gerry
      Gerry over 2 years
      yikes why would you ever want to do that...
    • Jarmos
      Jarmos over 2 years
      @Gerry why not? I think it comes down to personal preferences, isn't it? I use Neovim like 7 out of 10 times a day. But there are a few moments when I would like use VSCode instead. And during those few moments, I would like to stay within my favoured Neovim-like navigation environment. I don't see why it could be an issue.
  • ganjan
    ganjan almost 8 years
    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...
  • jarandaf
    jarandaf about 6 years
    With current version of VSCodeVim, it works with <Esc> instead of <esc>.
  • sjt003
    sjt003 about 6 years
    can you add another key after "<Esc>" ? like this "after": ["<Esc>", "l"]?
  • gwintrob
    gwintrob almost 4 years
    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?
  • Isaac Pak
    Isaac Pak almost 4 years
    @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
    Isaac Pak almost 4 years
    Nevermind. I got the fix from your github issue. link kudos!
  • Shadoath
    Shadoath over 3 years
    @st003 Yes, I have mine set to run "after": ["<Esc>", ":", "w", "<Enter>"]