vi is a editor which comes when any one is asked to program in shell script. On the Linux screen, you need to press ESC to go to command mode which is followed by the keys

a Add test to right of cursor.

i Insert text to the right of the cursor.

To move around the files: use arrow keys or h, i, j, k for left, right, down and up respectively if you are too lazy to move your hands to the arrow keys.

w moves to beginning of new word.

b moves to beginning of previous word.

0 (ZERO) moves to beginning of current line.

$ moves to end of current line.

H moves to lower-left corner of screen ( first visible line ).

L moves to lower-left corner of screen ( last visible line ).

M moves to middle line of screen.

To delete some text:

x delete character under the cursor.

X delete character before the cursor.

dw delete till end of word.

d$ delete till end of line.

d0 delete till beginning of line.

For saving and quitting

ZZ save and quit.

:w save and continue editing.

:wq save and quit.

:q quits only if there are no changes to be saved.

:q! quits without saving changes.

Searching for a text

:g/printf search for printf in entire file.

:/hello search forward for hello

:?goodbye search backwards for goodbye.

:/The*foot search for The %$#_$#@ foot

:?[pP]rint search for print or Print

:s/sacnf/scanf rectify the spelling mistake in current line

:g/sacnf/s//scanf rectify the first occurrence of sacnf in every line

:g/sacnf/s//scanf/g rectify every occurrence of sacnf in entire file.

:g/sacnf/s//scanf/gp rectify every occurrence of sacnf in entire file and show every change

 

Few more commands

u undo

Ctrl + r redo

:! executes command from the shell

Ctrl + f one page forward

Ctrl + b one page backward

Ctrl + d half page forward

Ctrl + u half page backward

G end of file

lG beginning of file

 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments