3 January 2023
_ __ _(_)_ __ ___ \ \ / / | '_ ` _ \ \ V /| | | | | | | \_/ |_|_| |_| |_|
Vim is the only editor you get faster with time.
I am writing an article on Vim to document the commands that I often forget or do not practice frequently.
Taking Backups I used to take backups of the file something like this.
cp file.yaml file.yaml.backup
The idea is to short this down and use
cp file.yaml file.yaml.b
Search & Replace
Before this I used to search using
/and then use
ito get into the insert mode and then edit the text. In my view
shift + cis a better alternative to this one as it directly replaces the texts. If you're looking for replacing the name of the name then use this.
Search and replace multiple instances of a word Use
:%s/old/new/gto replace all the instances of the word.
Appending text at the end I used to do this but now it has a formal name. If you are looking for appending some text at the end of the line then use
shift + A
Search and delete a word For searching use
/and then use
shift + dto delete the word.
Move complete Lines to left or right Use
>>to move the line to right and
<<to move the line to left.
Add lines above or below of a line Use
ofor inserting a line below, whereas use
Ofor inserting a line above.
Move multiple blocks of file for that use
shift + vand then select the chunks you want to move using arrow keys and then move.
Use multiple blocks for copy pasting Say you are given to create a multi container pod but you have only one pod manifest. You will copy the first container image chunk and then paste that for the second container and update the values.
Commenting something You can comment lines in vim using
Quit Instead of
:wqyou can use
:xas well to save and quit.
Selecting one line Use
shift + vto select single line and then use
kto select multiple lines.
One more thing from the perspective of CKA is that you can directly invoke vim after the output. Let me illustrate with an example.
kubectl run nginx-pod --image=nginx --dry-run=client -oyaml | vim -
and then you can save the file using
:wq pod.yaml. This will save some of your time during the exam and this is what I prefer.
For navigation more faster than using
ctrl+dcan be used to move downwards.
ctrl+ucan be used to move upwards.
}can be used to move to next empty line downwards and upwards respectively.
I know this but don't frequently use this one. Use
2wto move words in the right side direction and similarly for left.
Insert the same line below and above of the current line To add the same line to the line below use
yypTo add the content of the same line to the line above use
Most of the time, you'll use
Escto jump from insert mode to normal mode. I find it little hard. Thankfully, vim supports other keybindings to jump insert mode to normal mode. You can use the following.
ctrl + [-> There are other keybindings as well but I will stick to this one.
Say you're at the end of the line on your screen and now you wanted to move two lines down or up but the requirement is that your cursor cannot move. How can you do that? Well, in this case you can use
ctrl+emove your current cursor up and
ctrl+yto move the same down. This only works in normal mode. Here's video describing this and some more navigations https://youtu.be/Qem8cpbJeYc
Horizontal Navigation in Vim I use this for the purpose of recording macros.
0- beginning of the line
$- end of the line
^- first non-blank character of the line
g_- last non-blank character of the line // not many usecases for this one.
I find "gi" a particular powerful command: it starts insert mode in the last place where you have inserted text before, so you can write something, exit insert mode, navigate around the text, and then press "gi" and continue writing in the same place.