vim in vs code

2022-04-01

 | 

~2 min read

 | 

388 words

One of the great things about massive changes (e.g., a move, a new job, etc.) is the natural tendency to reevaluate all of your habits and adopt new ones (there’s a reason why marketers pay big money to know when you move!). Starting a new role, I recently had an excuse to give Vim another go.

I’m not going fully native this time around, but am trying to adopt a basic workflow within VS Code using the Vim extension.

In trying this out, I had some nice advice for helpful keybindings that would make useful additions to the standard palatte. Since one of Vim’s value propositions is to avoid having to use the mouse, it stands to reason that whenever you do use the mouse, that’s a good candidate for a new keybinding - at least if it’s something you do regularly.

Two examples of this include the peak definition and references functionality.

settings.json
// ~/Library/Application\ Support/Code/User/settings.json
{
  "vim.normalModeKeyBindings": [
    {
      "before": ["g", "p"],
      "commands": ["editor.action.peekDefinition"]
    },
    {
      "before": ["g", "r"],
      "commands": ["editor.action.referenceSearch.trigger"]
    }
  ]
}

With these in place, I’m able to quickly view peak the definition or references. Even better VS Code places the focus in the new menu and you can navigate with your h, j,k, and l keys as well as the Return to drill down.

A few other interesting customizations:

// ~/Library/Application\ Support/Code/User/settings.json
{
  "vim.visualModeKeyBindingsNonRecursive": [
    {
      "before": [">"],
      "commands": ["editor.action.indentLines"]
    },
    {
      "before": ["<"],
      "commands": ["editor.action.outdentLines"]
    }
  ]
}

While a Tab key works to indent and Shift+Tab outdents, for that work you need to be in INSERT mode. These two short cuts allow you to indent/outdent while in NORMAL mode.

The next one is useful only in that it’s such a common action and we can eliminate one key. Bringing up the command palette is readily available by default with Cmd+Shift+P.

We can make it available with Shift+: with the following setting:

// ~/Library/Application\ Support/Code/User/settings.json
{
  "vim.normalModeKeyBindingsNonRecursive": [
    {
      "before": [":"],
      "commands": ["workbench.action.showCommands"]
    }
  ]
}

Lastly, based on some suggestions I remapped my <leader> key to the ,:

// ~/Library/Application\ Support/Code/User/settings.json
{
  "vim.leader": ","
}

Lots more to learn here, but it’s a fun start! Shoutout to some of the kind folks who wrote up their journey’s like Steve Losh and Michael Chen.


Related Posts
  • Vim Multicursor In VS Code


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!