CuteMachine

Key Mappings With Vim For VSCode

Today I learned to map a Visual Studio Code keyboard shortcut to a vim key binding.

I'm a Vim user; I switched to VSCode only because the Vim for Visual Studio Code plugin is excellent. Several years back I tried to switch to WebStorm, but the Vim integration was so bad that I switched back to Vim for my preferred IDE only two weeks later.

Surprisingly I'm delighted with VSCode and the Vim support it has through the plugin. Still, you will only get a fraction of Vim's real power, but it is good enough for me.

There is a shortcut in VSCode with which you can close all editors but the current one.

As a Vim user, I wanted to add a key binding for this action in my vim settings. In Vim I wanted to just press <leader>-o to close all editor sessions but the active one.

The keyboard shortcut to trigger this action in VSCode is option-command-t.

To find the name for the triggered action you can open the Default Keybindings file. You can open this file in VSCode with the Show All Commands command shift-command-p and typing default keybindings.

In this file, we can search for "alt+cmd+t" to find the associated action name.

{ "key": "alt+cmd+t",             "command": "workbench.action.closeOtherEditors" },

Now that we know the action name, workbench.action.closeOtherEditors, we can map this in settings.json, where all our vim key bindings are stored.

Open settings.jsonwith shift-command-p and then search for settings.json.

In the setting vim.normalModeKeyBindingsNonRecursive add the following mapping:

    "vim.normalModeKeyBindingsNonRecursive": [

        {
            "before": ["<leader>", "o"],
            "after": [],
            "commands": [
               "workbench.action.closeOtherEditors",
            ]
        }

]

Now you should be able to trigger the action to close all other editors with the vim mapping <leader>-o. My leader is mapped to the key ,.


"vim.leader": ",",

So pressing , and shortly thereafter the key o will do the trick.

Posted on CuteMachine.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer