Introduction
Modes
Movements
Insert
Find
Go to
Search
Remove and Delete
Replace and Repetition
Visual Mode
Practice
Vim
is one of the most powerful text editors I have ever used in CLI(Command Line Interface). There are so many files which need to be opened or edited. Vim
can be the best choice because Vim
is more than an editor.
Installation commands on Ubuntu
:
$ sudo apt update
$ sudo apt install vim
There are two basic modes in Vim
.
insert
mode (In this mode you can edit texts in your own way
).normal
mode (In this mode you can manipulate and play with texts according to some instructions
).To switch between these modes
you need to use Esc
for normal
mode and i
for insert
mode.
There is another mode in vim called visual mode
. It is described here.
You can move the cursor using following keys instead of uding arrow keys:
h
for moving towards left
.l
for moving towards right
.k
for moving towards up
.j
for moving towards down
.Movement between words is possible via these keys:
w
or W
for moving to the beginning of the next word from current cursor and will continue.b
or B
for moving to the beginning of the previous word from current cursor and will continue.e
or E
for moving to the end of the next word from current cursor and will continue.Use number
before these keys for moving fast e.g. 3w
or 3W
means cursor will move to the beginning of the 3rd next word
.
Suppose you want to insert yes
30 times consecutively. Steps will be as follows:
i
, vim
will be switched from normal mode
to insert mode
.yes
then press Esc
.You will see that yes
has been written 30 times consecutively.
By pressing o
or O
, a new line will be inserted and vim
will be switched to insert
mode as well.
fq
to find next q
.Fq
to find previous q
.3fq
to find 3rd next
occurrence of q
.3Fq
to find 3rd previous
occurrence of q
.*
to find the next occurrence of the word under current cursor.#
to find the previous occurrence of the word under current cursor.(), {}, []
) by simply pressing %
.0
to go to the start of the line.$
to go to the end of the line.gg
to go to the beginning of the file.G
to go to the end of the file.G
with number before
to go to the specific line e.g.
press 3G
to go to the 3rd line
.You can search a specific text in a file i.e. for searching text
in your file, steps will be as follows:
/text
and then hit Enter
button.n
, it will find the next text
from the file and so on.N
, it will find the previous text
from the file and so on.x
, the character under the cursor will be removed.X
, the character to the left of the cursor will be removed.The delete key is d
. You can use it with movements' keys
.
E.g.
dw
will delete the first word on the right side of the cursor. It also copies the deleted content so that you can paste it to another location by pressing p
.
For replacing a letter under cursor:
r
and then write replaceable letter e.g. a
to replace cursor's letter by a
.Repeat the previous command by pressing simply .
E.g.
d2w
will delete next two words of current cursor..
, the previous command i.e. next two words will also be deleted and so on.To switch to visual mode
, just press v
. In this mode you can select a text via movement keys.
E.g.
v
and then e
to select a word.d
to delete that word.When you change the file in vim you have to know these commands as well:
:w
(save):wq
(save and quit):q
(quit):q!
(quit without saving)u
(For undo
)ctrl+R
(For Redo
):help
(For Help
)This article is written according to this website. You can practice above commands
in realtime.
Comments