VI Editor Part One


The VI editor (Visual Editor) is screen editor which is almost similar in all Unix based OS (Operating System).  Once we are used with them then you will feel vi editor are easy and very powerful during editing in all Unix based OS.

Let see how to start an vi editor in Unix system.



To start using vi, at Unix prompt type is vi followed by a file name, if we want to edit any existing file type or creating any new file then user can type give a name with any extension.

%vi filename

Let see different Modes and Moods of vi editor.

vi editor has two modes they are.

1. Command Mode
2. Insert Mode

So it is essential for user to know which mode they are at any given point of time.  When we are in command mode, letters of keyboard will be interpreted as commands.  When we are in insert mode the same letters of keyboard will type or edit text value.  vi always starts out in command mode.  When we try to move between these two modes then press or type i and enter the insert mode if we want to leave insert mode and return to command mode hit ESC key.

Note: If we are not aware in which mode we are try hit ESC couple of times that will move us back to command mode.

General Command Information about vi editor
  • they are case sensitive, lowercase and uppercase command letters do different things
  • they are not displayed on the screen when we type them
  • generally do not require a Return after we type the command
  • We will see some commands which start with a colon (:) These commands are ex commands which are used by the ex editor.  ex is the editor which lies underneath vi.
Entering Text

To start entering text in an empty file we must change to insert mode from command mode.  To move from command mode to insert mode press letter i.  When we start typing anything we can see that letters entered into the file. Once we type few lines and hit Return at the end of each of line.  It will break a line at edge of the screen, if we make a mistake then we can use Backspace key to remove the error text.  If Backspace key doesn't work then try using Ctrl+h key combination.

Note: Unlike other word processor vi editor do not use word wrap option.

Cursor Movement

To move cursor position in file we must change the status from insert mode to command mode and use following command to locate cursor position.

Moving One Character at a Time : These command are only useful when up/down/right/left keys do not work. We will see them in simple way like h,j,k and l keys.

h left one space
j down one space
k up one space
l right one space

As far as we move cursor using following command we fill fine beep symbol on screen.

Moving among Words and Lines : These command will more helpful to move from line or words in file.

w moves the cursor forward one word
b moves the cursor backward one word 

Note:If beep is in middle of word then this command will move cursor to beginning of current word

e moves the cursor to the end of the word

Now we will see some command for moving cursor to a line.

H moves the cursor to the top line of the screen.
M moves the cursor to the middle line of the screen
L moves the cursor to the last line of the screen

To scroll through the file and see other screen use following commands.

Ctrl+f scrolls down one screen
Ctrl+b scrolls up one screen
Ctrl+u scrolls up a half a screen
Ctrl+d scrolls down a half a screen


Note: To make it more better using these commands with a number for greater movement.
eg:  
10w will move forward 10 words 
8b would move us backward eight words 
7j would move down seven characters
Two special shortcuts can used for quickly on a line they are $ and 0 [Zero] key. $ key will move us to end of a line and 0 [Zero] will move us quickly to the beginning of a line.

Similarly other two commands are used for moving quickly from one end of the file to other.  G to move to end of the file and 1G to move to beginning of the file.  If we precede G with a number then we can move to specific line in the document like 18G will move to 18 lines.

Moving by Searching : Try to use '/' option followed by text with in command mode press Return key or Enter key to get cursor to move to the first incidence of that string of text.  If we want to repeat the search then type n or search in backwards direction use N.









No comments:

Post a Comment