Thursday, October 20, 2011

Vi Editor

When we need to add some more lines to existing file we can use "cat" commnad, if we need to replace some string we have "sed"

But when we need to create a new file or edit already existing files we have to go for screen oriented editors.
There are different types of editors vi, ed, ex and emacs of which vi is the most choosen one. vi and emacs use the entire screen to edit.

Different modes of vi:
vi editor has two different modes, hence it is also called "modaleditor". The two different modes are "insert" and "command" mode. The way UNIX interprets your input depends on which mode we are. For example when we are in commnad mode and enter "dd", it will delete one single line, whereas if we are in edit mode entering "dd" will add "dd" to the file. So whenever you are in doubt in which mode you are, just keep on pressin escape button which will take you to command mode.


Diffrence beween vi and emacs:
emacs is a modeless editor. ie whenver you enter any key it adds to the file. Command in emac is entered by holding the control key while entering the command. For example ctrl+c will delete a character.

Cursor Moverments:

In keyboard the letters H,J,K,L comes in a series.
To move the curson left in a file -H
Right - L
Down - J
Up -K

You can move to the begining of next line by pressing the return key.
Move the cursor to the begining of line - ^,0 (zero)
Move the cursor to the end of line - $
Move forward word by word - w
Move backward word by word- b
Move forward to mid of the text in screen - ctrl+d
Move backward mid of the text - ctrl+u
Page down - ctrl+f
Page up -ctrl+b

vi insert modes:
There are basically four different ways you can get into vi insert mode.
i - insert text into file
a - append text into file
o - Open up a line below current line
O - Open up a line above current line (capital o)

Difference btw i and a for edit mode:
at present the cursor is on a, and now I am going to enter edit mode by entering "i"
On entring "i" the curson doesnt move and it will be there in the same location as above and now I am entering "cursor not moved" and you can see that the text entered to the left of "a" with cursor is not moved at all from "a" and then  press escape, you will find cursor move one space back.

On entering "a" to get into the edit mode, vi behaves differently.
Assume the cursor position is on "a" same as above and press "a" to move to edit mode, you can see that the cursor moves one place forward to "l" and whatever we enter is appended after the letter "l" and press escape, you see cursor moves one space back.

No comments:

Post a Comment