A brief introduction to Vim (Part 5)
About
Here is the fourth (previous) part of this series: A brief introduction to Vim (Part 4)
Here is the sixth (next) part of this series: A brief introduction to Vim (Part 6)
Use numbers
One topic we haven’t talked about yet is numbers. Numbers can be used both in navigation and manipulations to perform a certain navigation or manipulation multiple times at once.
In terms used in my previous Post (A brief introduction to Vim (Part 4)) the number can be used as a modifier when it comes to manipulations.
- Delete 5
l
(d5l
): Deletes the 5 following characters including the character at the cursor. 3j
: Moves the cursor 3 lines down (Note: Using relative numbers as mentioned in A brief introduction to Vim (Part 3) comes in especially handy here because you can for e.g. instantly see how many lines you have to move downwards to reach a specific line)
Use the dot command
The dot command repeats the most recent change or manipulation performed in Vim.
Lets imagine we have a big text file and we want to replace some occurrences of the word terminal
with the word cli
.
- First we can search for the phrase
terminal
by entering/terminal
- Now we can change the word by pressing
cw
, which deletes the word and puts us into insert mode. Now we typecli
and pressESC
to get back into normal mode. - We press
n
to place the cursor on the next occurrence of the phraseterminal
and now we can use the dot command: We simply press.
and that will repeat the previous manipulation, we performed (change the word intocli
)
Search and replace in Vim
Searching and replacing a phrase in Vim is also done using a command. The most common use case is to replace a word or a phrase in the whole file, which I will show you now but if you for e.g. just want to replace something in the current line, read this.
Lets say we want to search for the phrase foo
and want to replace it with the phrase bar
in the whole file. The command for this would be: :%s/foo/bar/g
The visual mode
We already covered 3 essential modes in Vim: * normal and insert mode in A brief introduction to Vim (Part 1) * command-line mode in A brief introduction to Vim (Part 3)
Now it’s time to learn the visual mode which can be very helpful.
There are 3 sub-types of the visual mode which I will call visual mode, linewise visual mode and visual block mode.
Visual mode
Entering the visual mode is fairly easy, simply make sure you are in normal mode and then press v
. Nothing should happen but if you start navigating within the file, you will notice, that the text will be highlighted from where your cursor was when you pressed v
and where it is now.
To escape visual mode, press ESC
. Your Vim defaults might have a delay for exiting visual mode. To skip this delay, press ESC
again (twice).
Visual mode comes in handy if you first want to select something before performing an action on the selection. (Simply press the desired key of the verb after you have selected something and the action will be performed on the selection).
Linewise visual mode
Enter the linewise visual mode by pressing V
(uppercase). This will instantly highlight the line, the cursor currently sits on. If you start moving downwards (j
) the next line will be highlighted aswell.
Performing actions on the selection is the same as in the visual mode.
Linewise visual mode comes in handy for e.g. if you want to move one or more lines to a different position within the file.
Visual block mode
Enter the visual block mode by pressing CTRL+v
. You can now block-wise select text (test it out by pressing jl
and you will see, what I mean).
Performing actions on the selection is the same as in the visual mode.
Visual block mode comes in handy for e.g. if you want to want to change the same part on successive lines.
Let me give you an example:
const var1 = 1;
const var2 = 2;
const var3 = 3;
Lets say you want to change all three occurrences of const
to let
. You can do this easily by visual block selecting all three const
phrases, then pressing c
to change the selection, type in let
and pressing ESC
twice.
Visual mode and indenting
As mentioned in A brief introduction to Vim (Part 3) it is possible to perform (un)indenting in Vim. Now in combination with visual mode it is very easy to perform (un)indenting on multiple lines at once (by pressing <
or >
).
If you are in visual mode or linewise visual mode, the (un)indentation will be performed on all lines which are highlighed (even if not fully highlighted).
If you are in visual block mode, the (un)indentation will be performed on the beginning of the selection.
Ask me questions
I will be happy to answer your questions in the comments section below. Also let me know if you have any tips or ideas for me to improve my post!