#A Vim user’s guide to VS Code, part 1
Add editors to the list of things you don’t discuss in polite company, right after politics, religion, and baseball.
The editor wars have a long and bloody history.
I’m a Vim user. My everyday editor is Neovim, loaded with plugins.
These are my notes on getting Vim-style editing into VS Code.
Since the volume is going to be huge, I think I'll probably break it into several articles.
##In this article
- Why I started configuring VS Code
- The essentials for Vim editing in VS Code
##Why I started configuring VS Code
Calling myself a Vim user probably makes me sound more experienced than I am. I’ve only been using it seriously for a little over a year.
However, I have already become a fan of Vim, and I have started to feel pain in typing text in an environment where Vim cannot be used.
(In work, we sometimes have to create sentences with Word, but that's the biggest hardship...)
Also, I am both a Vimmer and an Ubuntu user, and my home PC also runs Ubuntu.
However, I still have to use Windows for work quite often, which is stressful...
Regarding Ubuntu, I manage all settings in dotfiles and make sure everything, including the editor environment, can be replicated immediately.
But, the behavior is so different on Windows that I can't do it this way...
(I wonder if using WSL2 or something like that can make it look nice? I'd like to try that next time.)
And recently, there is a popular editor called VSCode.
It is very multifunctional and, since it can also operate as a terminal, it is gradually taking on an IDE-like position that can integrate various languages.
And above all, it can run in various environments such as Windows, MacOS, and Linux.
I had been using VSCode for a while before, but I couldn't really beat the feel of NeoVim, which I had tuned myself, and I didn't have many opportunities to use it that much.
However, there are plugins that also allow you to use Vim's keyboard bindings in VSCode, and I learned that there is a way to synchronize settings across multiple devices. If we properly tune it and make it easy to use, I thought it could become an editor that can be used across the Linux and Windows barriers.
By the way, there are two ways to synchronize settings on multiple devices, and I decided to use the latter method.
Therefore, we aim to build a user-friendly environment for Vimmer in VSCode.
(Note: This article is being written while organizing and porting the environment I built and tested in VSCode to VSCode Insiders.)
I will explain each of the settings items one by one, so I would like people who have used Vim before but didn't get it to work out this time to try it out.
##The essentials for Vim editing in VS Code
Let's get started then.
This article aims to set up the minimum necessary configuration for now.
We define the minimum necessary level as an environment setup that does not cause irritation while typing for now.
Therefore, I will want to implement settings that allow file operations and other tasks to be performed only using the keyboard in future versions.
###Installing extensions
First, install extensions into VSCode.
We will install only two for now.
-
https://marketplace.visualstudio.com/items?itemName=vscodevim.vim
-
https://marketplace.visualstudio.com/items?itemName=cocopon.iceberg-theme
You, who commented that it's not a one-sided theme, are right!
I use this theme in NeoVim too, but just adding it makes the editor instantly look cool.
It's important to start with the form.
By the way, I also set my terminal to this Iceberg theme.
Details are here.
###Setting up keybindings
As soon as you insert the Vim plugin just now, the Vim keybinding will be activated, but unfortunately, this alone is not useful.
Three things prompted the switch.
- Some keys are overlapping with VSCode's default key bindings and don't work properly.
- Some Vim commands are unstable and behave in unexpected ways.
- (It's a problem with Vim itself, but) there are some things that are difficult to use with the default Vim commands.
You need to edit two files to adjust these.
- settings.json contains the VS Code settings.
- keybindings.json: Setting up keybindings
The relationship between these two files is very difficult to understand, but the settings of VSCode itself and plugins are done in settings.json, and the keybinding settings on VSCode are done in keybindings.json.
Therefore, the key bindings for the Vim plugin are set in settings.json.
In that case, if a similar keybinding is set in keybindings.json, it will be prioritized, and the expected behavior will not be achieved.
Note that these files can be opened by pressing Shift+Ctrl+p to open the command palette,
- settings.json : Preferences: Open Settings (JSON)
- keybindings.json : Preferences: Open Keyboard Shortcuts (JSON)
By selecting each one, you can open them individually.
###Configuring the editor and extension
First, set up the configurations in settings.json.
Since we will upload the entire file later, we will explain each setting one by one while checking what it is doing.
Editor settings
- "editor.minimap.enabled": false
The screen showing the full-size text being zoomed in on the top right of the VSCode editor area.
I don't feel the necessity for it, so I'll turn it off.
- "editor.renderWhitespace": "all",
Setting to visualize half-width spaces.
This makes it easier to check that there are no tabs or full-width spaces included.
- "files.trimTrailingWhitespace": true,
A setting to automatically remove unnecessary spaces.
Leave it on for convenience.
Vim settings
- "vim.leader": "<space>",
Setting up Vim's Leader key.
Leader allows you to combine with other keys to execute various commands.
There are various schools of thought, but I belong to the school that assigns keys to spaces.
- "vim.useSystemClipboard": true,
When you turn this on, the text you type in Vim like a yank (something like copying) will be saved to the clipboard.
That lets me paste the selection into other applications, including my browser.
The same applies in reverse.
I have it set to ON because it's convenient.
- "vim.useCtrlKeys": true,
To configure Vim key bindings to use Ctrl.
Leave it on.
- "vim.easymotion": true,
An extension to jump to a cursor position using labels.
In Normal mode, press Leader twice to activate it.
After that, you can choose various motion, but the one you use the most is the one with a one-letter search motion using the "s" key.
Since the label appears with Leader+Leader+s+(the text you want to search for), you can jump by pressing the key for the location you want to jump to.
- "vim.visualstar": true,
Allows you to search for the words you are selecting with the * key and # key in Visual mode.
It's convenient, so I'll turn it on.
- "vim.hlsearch": true,
Enable highlight search to highlight search results.
Turn on so that search results are easier to understand.
Normal mode keybindings
{
"before": [
"u"
],
"commands": [
{
"command": "undo"
}
]
}
{
"before": [
"<C-r>"
],
"commands": [
{
"command": "redo"
}
]
}
The behavior of undo and redo using the u key and Ctrl+r is somehow strange, so I switched it to the undo and redo functions provided by VSCode.
(In the original settings, the undo and redo range is wide, and everything is skipped from the current position to the previous or next position all at once)
{
"before": [
"x"
],
"after": [
"\"",
"_",
"x"
]
}
{
"before": [
"s"
],
"after": [
"\"",
"_",
"s"
]
}
A setting that prevents a character from being written to the clipboard when deleting or correcting a character using the x and s keys.
{
"before": [
"j"
],
"after": [
"g",
"j"
]
}
{
"before": [
"k"
],
"after": [
"g",
"k"
]
}
This setting ensures that the cursor moves as shown when the movement of the j and k cursors is reversed.
{
"before": [
"n"
],
"after": [
"n",
"z",
"z"
]
}
{
"before": [
"N"
],
"after": [
"N",
"z",
"z"
]
}
{
"before": [
"*"
],
"after": [
"*",
"z",
"z"
]
}
{
"before": [
"#"
],
"after": [
"#",
"z",
"z"
]
}
A setting that brings the words selected when navigating by search to the center of the screen.
{
"before": ["<Leader>", "c"],
"commands": [
{
"command": "editor.action.commentLine"
}
]
}
Leader+c to comment out that line setting.
{
"before": ["<Leader>", "w"],
"after": [],
"commands": [
{
"command": "workbench.action.files.save",
"args": []
}
]
}
Setting to save files with Leader+w.
{
"before": ["<Leader>", "q"],
"after": [],
"commands": [
{
"command": "workbench.action.closeActiveEditor",
"args": []
}
]
}
Close file with Leader+q setting.
{
"before": ["<Leader>", "x"],
"after": [],
"commands": [
{
"command": "editor.action.formatDocument",
"args": []
}
]
}
Settings for formatting (indenting) files using Leader+x.
{
"before": [
"<Leader>",
"l"
],
"after": [
"$"
]
}
{
"before": [
"<Leader>",
"h"
],
"after": [
"^"
]
}
Settings to move the cursor to the end with Leader+l and to move the cursor to the beginning with Leader+h.
The problem with Vim is that the default keys are located in a hard-to-press position, so they become very comfortable when you change them.
{
"before": ["<Leader>", "m"],
"after": ["%"]
}
Setting to move to the corresponding parenthesis with Leader+m.
This also has the default keys that are hard to press.
{
"before": ["<Leader>", "z"],
"commands": [":noh"]
}
To remove highlighting from search results in Leader+z.
It's handy to have, even if it's understated.
Insert mode keybindings
{
"before": ["j", "j"],
"after": ["<Esc>"]
}
Setting to exit Insert mode with j+j.
Required settings.
The Escape key is too cumbersome to press for frequent use.
{
"before": [";", ";"],
"commands": ["editor.action.triggerSuggest"]
}
;+; setting to display the completion view.
I found it useful after seeing what others had set up, so I borrowed it.
Visual mode settings
{
"before": [
"<Leader>",
"l"
],
"after": [
"$"
]
}
{
"before": [
"<Leader>",
"h"
],
"after": [
"^"
]
}
{
"before": [
"<Leader>",
"m"
],
"after": [
"%"
]
}
Settings to enable fast cursor movement just like in Normal mode.
{
"before": [
">"
],
"commands": [
"editor.action.indentLines"
]
}
{
"before": [
"<"
],
"commands": [
"editor.action.outdentLines"
]
}
A setting to allow consecutive changes to indentation.
I also added this after finding it online.
On the one hand, it seems convenient, but on the other hand, it seems inconvenient to go back to Visual mode manually...
Since Vim can repeat processing with the key, you might not need it.
If you feel it's a bit off when you try it, stop using it.
Choosing a theme
- "workbench.colorTheme": "Iceberg",
Iceberg should install automatically.
Cool.
There are still some items that need to be set up to use them conveniently, but we will stop here for this time.
It ended up quite long for sure...
Here are the settings I ended up with:
- settings.json
{
"editor.minimap.enabled": false,
"editor.renderWhitespace": "all",
"files.trimTrailingWhitespace": true,
"vim.leader": "<space>",
"vim.useSystemClipboard": true,
"vim.useCtrlKeys": true,
"vim.easymotion": true,
"vim.visualstar": true,
"vim.hlsearch": true,
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["u"],
"commands": [
{
"command": "undo"
}
]
},
{
"before": ["<C-r>"],
"commands": [
{
"command": "redo"
}
]
},
{
"before": ["x"],
"after": ["\"", "_", "x"]
},
{
"before": ["s"],
"after": ["\"", "_", "s"]
},
{
"before": ["j"],
"after": ["g", "j"]
},
{
"before": ["k"],
"after": ["g", "k"]
},
{
"before": ["n"],
"after": ["n", "z", "z"]
},
{
"before": ["N"],
"after": ["N", "z", "z"]
},
{
"before": ["*"],
"after": ["*", "z", "z"]
},
{
"before": ["#"],
"after": ["#", "z", "z"]
},
{
"before": ["<Enter>"],
"commands": ["editor.action.insertLineAfter"]
},
{
"before": ["<Leader>", "c"],
"commands": [
{
"command": "editor.action.commentLine"
}
]
},
{
"before": ["<Leader>", "w"],
"after": [],
"commands": [
{
"command": "workbench.action.files.save",
"args": []
}
]
},
{
"before": ["<Leader>", "q"],
"after": [],
"commands": [
{
"command": "workbench.action.closeActiveEditor",
"args": []
}
]
},
{
"before": ["<Leader>", "x"],
"after": [],
"commands": [
{
"command": "editor.action.formatDocument",
"args": []
}
]
},
{
"before": ["<Leader>", "l"],
"after": ["$"]
},
{
"before": ["<Leader>", "h"],
"after": ["^"]
},
{
"before": ["<Leader>", "m"],
"after": ["%"]
},
{
"before": ["<Leader>", "z"],
"commands": [":noh"]
}
],
"vim.insertModeKeyBindingsNonRecursive": [
{
"before": ["j", "j"],
"after": ["<Esc>"]
},
{
"before": [";", ";"],
"commands": ["editor.action.triggerSuggest"]
}
],
"vim.visualModeKeyBindingsNonRecursive": [
{
"before": ["<Leader>", "l"],
"after": ["$"]
},
{
"before": ["<Leader>", "h"],
"after": ["^"]
},
{
"before": ["<Leader>", "m"],
"after": ["%"]
},
{
"before": [">"],
"commands": ["editor.action.indentLines"]
},
{
"before": ["<"],
"commands": ["editor.action.outdentLines"]
}
],
"workbench.colorTheme": "Iceberg"
}
##Wrapping up
These changes get VS Code to the point where I can use it for day-to-day editing.
Next I’ll work on keybindings.json and the rest of the interface.