About Vi Editor

VI editor is the default file editor in most of the Linux machines. It has great capabilities to edit a file within few key strokes.

A. Modes of VI :
Vi has two mode of operation.
1. Command mode
2. Insert mode

Command mode :
Vi editor begins in command mode, where cursor movement and editing occur.
To enter in to the command mode from Insert mode press Esc button.

Insert mode :
Used for entering text, this is similar to notepad in Windows. To enter in to insert mode you can use any of the following.

i or I => present line
o => one line down the present line
O => one line above

Note : All comments will work only in command mode.

B. Navigational commands :
1. Character navigation k, h, l and j

h => To move one character left.
j => To move one line down.
k => To move one line up.
l => To move one character right.

Above commands can be use as follow:

6j => to move 6 lines down from the present courser.
7k => to move 7 lines above from the present courser.

2. Word Navigation

w => word forward.
e =>word forward, but end of the word.
b => one word backward.

Examples :
54w => To move 54 words forward
8b => To move 8 words back.

3. Setting (nu) numbering to lines
:set nu

Removing of (nonu) numbering to lines
:set nonu

4. Moving paragraphs

move one paragraph up => {{
move one paragraph down => }}

5. Moving page up/down

For up => ctrl+b
For down => ctrl+f

6. Moving start/end of the file

Starting of the file(first line => [[
End of the file(last line) => ]]

7. To go to any line :
:lineno

Example :
If we want to go to 77 line then type
:77

C. Editing commands

8. Replace one letter

Replace one letter => r
Delete one letter => x

9. Edit one word

Edit one word => cw
Delete one word => dw

10. Edit one line
Edit a line from courser to the end of that line => d$

11. Cut/Delete one line => dd

Examples :
4dd(delete/cut four lines)

12. Paste

Paste a line below the courser => p
Paste a line above the courser => P

13. Copy

Copy one line => yy
Copy n lines => nyy

14. Special commands

join lines => J
undo things => u
To repeat previous command => .

D. Search and replace

15. Search for a term /fcoos

Example : If you want to search for fcoos then press /fcoos , then hit enter

Move to next occurrence, press “n” without quotes.
Move to previous occurrence, press “N” without quotes.

16. Search and replace a term (here separator is / )

:%s/searchterm/replaceterm/g

change default separator
:%s_/home/shrinu/grade_/home/testing/dest_g

To search and replace particular term from given line to another given line.
:%s294,304/shrinu/xyz/g

e)Save and Quit a file

:w => save the file
:q => quit the file
:wq => save and quit
:w! => force save the file
:q! => force quit without save
:wq => save and quit forcefully

Thank you.

Leave a Reply