Linux provides a lot of useful tools from the command line. Vi or Vim is one of the very popular text editor used in Linux, Unix, and BSD operating systems. In this tutorial, we will look at different usage examples of the Vim. From up to now we will call Vim which is an improved version of Vi.
Open with File Name
We can open a file by giving its name to the Vim. In this example, we want to open a file named test.txt
. If this does not exist it will be created after saving operation.
$ vim test.txt
Command Mode
Vim and vi mainly work with commands. Vim has command mode in which these commands can be issued. We can use Escape
and :
to enter command mode. In command mode, we will use the commands described below.
Exit From Vim
For beginners exiting from vim is very hard job. Vim have commands to manage Vim behaviour. We can exit from Vim with the following commands.
Exit
We can easily exit with the following command.
:q
Exit Without Saving
If we have made some changes and try to exit vim or vi will prevent it unless we save it or exit without saving.
:q!
Move Cursor In The Vim Editor
Cursor can be moved with given direction keys. But Vim provides more shortcuts for this.
Down Single Line
j
Up Single Line
k
Left Single Character
h
Right Single Character
l
Start Of The Current Line
0
End Of The Current Line
$
First Line Of File
:0
Last Line Of File
:$
Move Cursor To The Given Line Number
One of the most popular command for Vim is moving cursor to the given line. We will use :n
for this but we will also provide line number. In this example we want to move cursor to the line number 500.
:n500
Insert/Edit Mode
Insert/Edit mode is the situation where all given keystrokes are interpreted as text input. We will look how to enter insert/edit mode below.
Add Text
We generally need to add some text to the files. There are different ways to use insert mode. Here some of them.
Insert
We can write the text and it will be inserted
i
Append
a
Copy Text
Copy operating will operate at the given lines. Copy operation will copy lines to the buffer.
Copy Current Line
We will copy the current line where the cursor resides.
yy
Copy Given Range
We can copy given range which is 4 lines in this example.
y4y
Paste Text
Copied text will be pasted from buffer.
p
Undo Last Change
We can revert changes easily with the following command
u
Delete Text
We can delete lines with the following command. Deleted lines also copied to the buffer where we can use this buffer to paste. This is same as moving.
Current Line
We will delete the current line where the cursor resides
dd
Given Line Range
We will provide the line count we want to delete which is 5 in this case.
d5d
Search Text
We can search given string, text in the current file in different ways like forward search, backward search, next occurrence, previous occurrence, etc.
Forward Search
We will search the text STRING
to the forward which means top to down.
/STRING
Backward Search
We will search STRING
to the backward which means down to top.
?STRING
Next Occurrence
Jump to the next occurrence of the search to the top to down.
n
Previous Occurrence
Jump to the previous occurrence of the search which means down to top.
N
Show Current Line Number
We can show current line number in the bottom with the following command.
:.=
Show Total Lines Number
If we need the total lines count we can use following
:=
Save File
Current file can be saved with the following command.This will save to the same file.
:w
Save File with Different Name
We have also option to save into different file name. We will provide the file name to the :w
command like below.
:w test2.txt
Save and Close
We can save and close current file easily with the following command.
:wq
2 thoughts on “Linux Vi and Vim Command-Line Text Editor Tutorial”