Vim
3 January, 2023 - Tags: git
Vim
_
__ _(_)_ __ ___
\ \ / / | '_ ` _ \
\ 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 usei
to get into the insert mode and then edit the text. In my viewshift + c
is 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/g
to 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 useshift + d
to 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
o
for inserting a line below, whereas useO
for inserting a line above. -
Move multiple blocks of file for that use
shift + v
and 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
0i#
-
Quit Instead of
:wq
you can use:x
as well to save and quit. -
Selecting one line Use
shift + v
to select single line and then usej
ork
to 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
hjkl
keys,ctrl+d
can be used to move downwards.ctrl+u
can be used to move upwards. -
{
and}
can be used to move to next empty line downwards and upwards respectively. -
I know this but don't frequently use this one. Use
2w
to 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
yyp
To add the content of the same line to the line above useyyP
-
Most of the time, you'll use
Esc
to 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+e
move your current cursor up andctrl+y
to 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 lineg_
- 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.