I don’t use Vim for everyday coding, I believe that modern IDEs are far more superior. However, Vim is a perfect tool for small edits on remote environments. For me, it’s a must when you work with Kubernetes YAMLs.
There are a lot of great guides and resources on Vim itself, so here I’ll focus on those of the functionalities, that are especially useful when editing Kubernetes YAMLs.
Proper configuration
It’s not obvious to rookies, that Vim is a lot more pleasant to work after a few tweaks in default configuration.
This can be easily done from the .vimrc
file in your home directory (it may not exist, so you’d have to create it).
The bare minimum that is usually recommended is:
expandtab ts=2 sw=2
set number
syntax on
Let’s break it down and explain it line by line:
expandtab ts=2 sw=2
This is a combo of three settings:
expandtab
will make the “tab” key input spaces instead of a tab character.ts=2
(short fortabstop
) defines how much spaces a single tab character is.sw=2
(short forshiftwidth
) specifies indentation width.
Why is this important? According to the specification, YAML files have to be indented with spaces. If we mix tab characters and spaces in YAML files, Kubernetes will return an error.
Such mistakes are really hard to spot, so if we forgot about the expandtab
setting and want to find a problem,
we can use the set line
setting, which will show all special characters. You can also use :retab
command to replace
all tab characters with spaces.
set number
Line numbers are very useful. Both to quickly find a mistake from a Kubernetes error message, as well as to quickly
navigate. If we want to quickly jump to line 42, we just need to use the command :42
.
The caveat is, that if you would want to copy text with your mouse, the line numbers would be copied as well.
Fortunately, we can just temporarily turn the line numbers off with set nonumber
syntax on
This setting makes sure, that the syntax highlighting is enabled. This obviously isn’t mandatory, but I find it really helpful.
Searching
To search a specific expression, use /
. For example: /image
will search for the “image” string. You can use regex as well.
Indenting
To indent the line we use >>
.
We can select a block of code with visual select with v
(or Shift-V
for selecting whole lines).
You can repeat indentation, by doing 3>>
(which will indent selected lines 3 times).
Unfortunately, after doing the indent, the selection will disappear, so we can’t keep the lines selected and indent them until we see that we’re finished.
What we can do is to repeat the indent command by pressing the comma key .
.
Remap caps-lock to Esc
It may seem inconvenient to constantly reach out to Esc
button to exit the insert mode.
However, remapping the caps lock button to act as a Escape has literally changed my life for better.
Not only I don’t accidentally turn in on, but it’s way easier to press Escape button now.
On macOS, this can be set from the system preferences, so there is no need to use external tools.