How to use the vi text editor in linux

This post will explain the basic of using vi text editor to edit in linux and example. 

Open/Create file with vi editor

You may use vi to open an already existing file by typing

vi file_name

where 'file_name' is the name of the existing file. If the file is not in your current directory, you must use the full pathname.

Or you may create a new file by typing

vi new_file_name

where 'new_file_name' is the name you wish to give the new file.

Closing and Saving a file

To save the edits you have made:

Press <Esc>

Type :wq

Press <Return>

To quit vi, and discard any changes your have made:

Press <Esc>

Type :q!

Press <Return>

Editing text in vi editor

To insert/edit text in a line:

Position the cursor where the new text should go.

Type i

Enter the new text.

Copy and Paste content in vi editor

Press to get back to command mode.

Type yy to copy a line

Type p to paste line

Searching in vi editor

To move quickly by searching for text, while in command mode:

Type / (slash).

Enter the text to search for.

Press .

The cursor moves to the first occurrence of that text.

To repeat the search in a forward direction, type


To repeat the search in a backward direction, type


Show line number in vi editor

Press to get back command mod

Type :set nu to show line number

Type :set nu! to hide line number

Tags: vi editor