visual-studio-codeneovimvscodevim

How to remap keys in Vim VS Code?


I'm remapping keys in settings.json (VS Code) like that:

  "vim.normalModeKeyBindingsNonRecursive": [
    {
      "before":["<S-j>"],
      "commands": [
        ":m-2",
      ]
    },
    {
      "before":["<S-k>"],
      "commands": [
        ":m+",
      ]
    } 
  ]  

So when I try to type Shift-k, I see :m+ in Vim command line but it's not performed. I think I need to use <CR> somehow, but I don't know how.


Solution

  • Currently, most of the Ex commands of Vim is not implemented in VSCode, you need to map the keys to the internal implementation and commands of VSCode. For example, if you want to move line up/down, you can add mappings for Normal mode and Visual mode like this:

    {
        "vim.visualModeKeyBindings": [
            {
                "before": [ "K" ],
                "commands": [ "editor.action.moveLinesUpAction" ]
            },
            {
                "before": [ "J" ],
                "commands": [ "editor.action.moveLinesDownAction" ]
            }
        ],
        "vim.normalModeKeyBindings": [
            {
                "before": [ "K" ],
                "commands": [ "editor.action.moveLinesUpAction" ]
            },
            {
                "before": [ "J" ],
                "commands": [ "editor.action.moveLinesDownAction" ]
            }
        ]
    }
    

    IMHO, J itself is a useful command, so I mapped these two commands to _ and -.

    Similarly, if you want to copy line up/down, you can map the editor.action.copyLinesUpAction / editor.action.copyLinesDownAction command. To get other commands, you can open the Keyboard Shortcuts list, then right click and copy command ID. For more to see VSCode Key Bindings and Built-in Commands.