vi
is a very powerful text editor where most of the Linux distributions provide. vim is an improved version of the vi editor with more features. Most of the intersecting features comply with vim. In this tutorial, we will learn how to use vi
from start.
Open File
We will start by opening a text file by providing the name of the file. In this example, we will open the file named poem.txt
. Alternatively, we can provide a complete path like /home/ismail/poem.txt
.
$ vi poem.txt
$ vi /home/ismail/poem.txt

Insert Mode
After opening the vi
editor we will be in the command mode. Command mode will enable vi
shortcuts like Copy
, Paste
, Delete Line
etc. If we want to change text we need to enter Insert Mode
. By entering i
we will enter the Insert Mode.
i

Command Mode
We can enter the command mode again with the Esc
or Escape. This will return from the Insert Mode to the Command mode.
Esc
Copy
There are different commands in the Command Mode. We can copy the given line with the yy
command below. This will copy the current line.
yy
OR we can specify the number of lines starting from current line which is 5 in this case.
y5y
Paste
With yy
command given line will be copied to the buffer. We can paste the latest buffer content to the cursor position with the p
command like below.
p

Delete Line
We can delete the current line with the dd
command. As stated this will only delete the current single line. If we want to delete from the current line to the down we can provide the line numbers between d’s. In this example, we will delete 5 lines from the current line.
d5d

Save Text Content
Over time we will make changes in the current file. These changes should be saved after some time in order to prevent loss. We can save the current file with the :w
which means save.
:w

Save and Quit
We can also make some work quicker. We want to save file and then quit. This can be down with a single command :wq
:wq
Quit without Saving
If we made some changes but do not want to save them and preserve the original file we can use :q!
:q!