My VsCode Vim Setup
VsCode + Vim is an essential part of my dev workflow, here are some settings I use
Why I use VsCode + Vim
- Vim requires too much of 'being on top of things'
- VsCode has some nice extensions and EverythingWorksTogetherNicely™ (mostly)
- I'm on Windows (because I like to play games too), and setting Vim or Neovim on Windows isn't straightforward
- Vim keybindings are the best part about Vim, so using that with VsCode is the most productive I can be without having a Vim setup
File Explorer
My leader key is space
, I've set the shortcut space+f
to toggle my file explorer. To do that, I need to change the shortcuts in two places:
// settings.json
{
"vim.normalModeKeyBindingsNonRecursive": [
{
"before" : ["<leader>","f"],
"after": [],
"commands" : [
"workbench.files.action.focusFilesExplorer",
],
"args": []
},
],
}
// keybindings.json
[
{
"key": "space f",
"command": "workbench.action.toggleSidebarVisibility",
"when": "filesExplorerFocus && !editorTextFocus"
}
]
This above setting, I got from: https://sendhil.blog/2020/10/02/vscode-bindings-to-toggle-file-explorer/
Move Lines Up and Down
When I first started using VsCode, I really liked the default shortcut to move lines up and down, just simply Alt+UpArrow
can move one line or multiple up and down. Here's how I do it in VsCodeVim:
// keybindings.json
{
"key": "alt+j",
"command": "editor.action.moveLinesUpAction",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+up",
"command": "-editor.action.moveLinesUpAction",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+k",
"command": "editor.action.moveLinesDownAction",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "alt+down",
"command": "-editor.action.moveLinesDownAction",
"when": "editorTextFocus && !editorReadonly"
}
Next/Previous Editor
One thing that I really liked about my Vim setups was the ability to move to next open buffer quickly. It makes quickly switching around in files very easy. In VsCodeVim I have this to make that happen:
// keybindings.json
{
"key": "ctrl+j",
"command": "workbench.action.previousEditor"
},
{
"key": "ctrl+pageup",
"command": "-workbench.action.previousEditor"
},
{
"key": "ctrl+k",
"command": "workbench.action.nextEditor"
},
{
"key": "ctrl+pagedown",
"command": "-workbench.action.nextEditor"
},
Indent/Outdent Lines
Indenting works in Vim with <
and >
. However it is not repeatable. True Vim Enthusiasts™ will say that the true vim philosophy is to use .
to repeat your last action. But I like having it repeatable.
// settings.json
"vim.visualModeKeyBindings": [
{
"before": [
">"
],
"commands": [
"editor.action.indentLines"
]
},
{
"before": [
"<"
],
"commands": [
"editor.action.outdentLines"
]
},
],
Paste without losing value from register
I really hate this default behavior of Vim. If I yank something and then paste it, I cannot paste it again, because it has been removed from that register. So here's how I have fixed that:
// settings.json
"vim.visualModeKeyBindingsNonRecursive": [
{
"before": [
"p",
],
"after": [
"p",
"g",
"v",
"y"
]
}
],
Undo/Redo mapping
I'm really used to using u
and Ctrl+r
to undo and redo. But that's not the behavior in VsCode, so here's how I've fixed that:
// settings.json
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["u"],
"after": [],
"commands": [
{
"command": "undo",
"args": []
}
]
},
{
"before": ["<C-r>"],
"after": [],
"commands": [
{
"command": "redo",
"args": []
}
]
},
}
Folding
I like folding/unfolding code with za
etc. But with folded code, the Vim line up, down doesn't work with j
, k
so to fix that I had to put this in my settings.json
// settings.json
"vim.foldfix": true,