VSCode + Vim extension: tweaks and QoL improvements to take your DX to the next level!
Tags: vscode, vim, productivity
Published: 2024-06-27 17:07:41




I've been using the [Vim extension for VSCode](https://github.com/VSCodeVim/Vim) for a couple years now and I've collected dozens of tips that I'd like to share here.

This will be a continuous post that I'll be updating overtime, feel free to comment and leave your own suggestions.

## Things to know

Vim extension has its own keyboard keybindings that are used by VSCode to execute all the [Vim motions](https://vim.rtorr.com) and commands.

Part of customizing your VSCode + Vim DX will be involved in changing the configurations of `settings.json` (User settings) and `keybindings.json` (Keyboard shortcuts) files side-by-side. I'll walk you through it.

To access your **User settings** press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> to access the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) and search for "Preferences: Open User Settings (JSON)".

To access your **Keyboard shortcuts** use the Command Palette and search for "Preferences: Open Keyboard Shortcuts (JSON)".

## Quality-of-life improvements

These are some of the change I've made to the behaviour of Vim that makes it more intuitive to use, by removing some of the overlapping conflicts with VSCode's own behaviours.

### Changing the Leader key

Vim's Leader key serves as the initial trigger for a multitude of shortcuts. I went with the community's most used <kbd>Space</kbd> for this one.

```json
// settings.json
"vim.leader": "<space>"
```

### Adjust the minimum visible lines surrounding the cursor

By default, your cursor will go all the way up to the top and bottom of the editor's margins, which difficults the vibility of where you can move. Adjust this one based on personal preference, mine is a minimum of 6 lines that are always visible as I go up and down the editor.

```json
// settings.json
"editor.cursorSurroundingLines": 6
```
![A GIF that shows surrounding lines working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1xl9pvd1nbexb8cmxkm.gif)

Notice how the number 6 (top of the editor) never changes when I go up, the same applies to the bottom.

### Prevent exiting Insert mode when closing the suggestions widget

Exiting Insert mode and closing the suggestions widget are both triggered by <kbd>Escape</kbd> and when Vim exits Insert mode on an empty line it puts the cursor at column 1, ignoring the current indentation. This shortcuts fix this by only closing the suggestions widget and not exiting Insert mode (preserving current indentation).

```json
// keybindings.json
{
  "key": "escape",
  "command": "extension.vim_escape",
  "when": "editorTextFocus && vim.active && !inDebugRepl && !suggestWidgetVisible"
},
{
  "key": "escape",
  "command": "-extension.vim_escape",
  "when": "editorTextFocus && vim.active && !inDebugRepl"
}
```

![A GIF that shows the suggestion widget closing and Insert mode staying active](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ornyhzkeidnl4fabhh7.gif)

### Disabling Vim keybindings that conflict with native VSCode shortcuts

You can disable Vim's <kbd>Ctrl</kbd> shortcuts by settings which combinations you'd like with the `vim.handleKeys` key, instead of deleting it via the "Preferences: Open Keyboard Shortcuts" (<kbd>Ctrl</kbd>+<kbd>K</kbd> <kbd>Ctrl</kbd>+<kbd>S</kbd>).

```json
// settings.json
"vim.handleKeys": {
  // Open Sidebar
  "<C-b>": false,
  // Find
  "<C-f>": false,
  // Replace
  "<C-h>": false,
  // View: Toggle Panel Visibility (the terminal panel)
  "<C-j>": false,
  // Ctrl+K is used by dozens of VSCode shortcuts
  "<C-k>": false,
  // Go to file...
  "<C-p>": false,
  // Trigger Suggest
  "<C-space>": false,
  // View: Close Editor (Vim's window manager works in VSCode, so if you'd like it don't use this one)
  "<C-w>": false
}
```

### Conclusion

Well, I hope you find these tips useful for moving and editing your code faster, godspeed.