Easy Guide to use vi text editor in Linux terminal – Top 20 commands

vi is visual terminal based text editor for linux. vi can open, create and  modify. To use vi text editor in Linux you have to enter vi in status line below text of file. To exit from command mode and enter in text input mode press I. Pressing Escape will make you exit from input mode. Here we have top 20 commands used frequently in vi text editor.

Top 20 command of vi text editor in Linux

Open File in vi text editor

To open a file in vi text editor in current folder type vi filename
In case file exists in different location type path to file followed by file name.

Syntax

vi /path to file/filename

Modify or Edit files in vi text editor

To enter in Edit mode press i in command mode vi editor. On pressing i you will enter editing mode.

Exit editing mode of vi text editor

Once you are through with editing of File, press Escape key. On pressing Escape key you will enter command mode of vi text editor.

Create new lines in vi text editor

While editing a file using vi text editor in Linux you may need to create a new line, so in order to create new line below cursor enter o in command mode. If you want to create line above cursor press shift+o in command mode.

Syntax

Create line below cursor

o

Create line above cursor

Shift + o

Copy Line (Single / Multiple) or Word on vi text editor

vi text editor provides option for coping line from command mode. First take your cursor to Line or word you want to copy.

Syntax

Copy Line

yy 

Copy word

yw

Copy multiple lines

Number of lines yy

Example

To copy 5 line use below command

5 yy

Paste in vi text editor

To Paste content copied earlier, first take your cursor to point where you want to paste text.

Syntax

To paste copied text in next line press

p

To paste copied text in line above cursor

Shift + p

Delete line or word in vi text editor

You can delete a line or word from command mode of vi text editor, like before bring your cursor to point you want to delete any content.

Syntax

To delete a line below cursor

dd

To delete a word press

dw

Undo or redo last change in vi text text editor

Like any other editing application vi text editor also has undo and redo option just enter in command mode vi text editor.

Syntax

Undo last change

u

redo last change

Control + r

Navigate in a document in vi text editor

To navigate across file use below commands

Go to end of file press

Shift + g

To go to Start of file press

gg

To go to beginning of a line

o (Zero)

To go to end of line

$

Show or Hide line number in vi text editor

While scripting its useful have line numbers displayed while you are working on your file.

Syntax

Show line number

se nu

Hide line numbers

se nonu

Save and Exit from vi text editor

Once you are through with editing of your document in vi text editor, you will need to save before exiting vi text editor. Enter in command mode and use below commands at prompt.

Syntax

Save file

w

Save file and quit

wq

quit without saving

q

Force save and quit

wq!

These are few of frequently used commands.

Follow below link for detailed guide on use vi text editor

Detailed Guide to use of vi text editor in Linux