Wednesday, June 22, 2011

VI EDITOR

vi is a full screen text editor available with all the Unix systems. Linux generally uses vim (vi improved).

Three modes in vi:
 A vi session begin by invoking the command vi with or without a filename

$ vi myfile

~
~
~
~
~
~

You are presented with a full empty screen, each line beginning with a ~(tilde). This indicates that they are non-existent lines. For text editing, vi uses 24 of the 25 lines that are normally available in the terminal. The last line is reserved for some commands. This line is also used by system to display messages. The file name appears in this line with the message  “myfile”[New file]
When you open a file with vi, the cursor is positioned at the top left-hand corner of the screen. You are said to be in command mode from where you can pass commands to act on the text.
There are three modes in Unix :
  • Command mode-where keys are used as command to act upon text.
  • Input mode-where any key depressed is entered as text.
  • ex mode – where ex mode commands can be entered in the last line of the screen to act on the text.

Input mode

By default you are command mode. First enter the following command:

:set showmode <enter>

This command displays the mode at the bottom line of the screen.

** In Linux showmode setting is not necessary .vim sets it to this by default.

Before you attempt to enter text into the file , you need to change the default command mode to an input mode. There are several  methods of entering into this mode, depending upon the type of input you wish to key in, but in every case the mode is terminated by pressing <esc> key.

Insertion of text

 The simplest type of input is the insertion of text. whether the file contains some text or not ,when vi is invoked, the cursor is always positioned at the first character of the first line. To inset the text at this position press I,

 The character doesn’t show up on the screen, but pressing this key changes the mode from command mode to input mode. Further key  depression will result in text being entered and displayed on the screen.
The input mode is terminated by pressing the <Esc> key which takes you back into the command mode.

You started the insertion with i, which put text at the left of the cursor position. If the  i is invoked with the cursor positioned on existing text, text on it right will be shifted further without being overwritten.
    
      The vi editor
    
                           i full-screen<Esc>        

              The vi full-screen editor                                   

Append

To append text to the right of the cursor position,

a    #existing text will also be shifted right

After you have finished editing press <Esc>

I inserts text at the beginning of a line, while A appends text at the end of the line



 
  The vi full screen editor
 
                                           
              a, a link of ex<Esc>

  The vi full-screen editor, a link of ex

Opening a new line with o

You can also open a new line by positioning the cursor at any point in a line and pressing o

This inserts an empty line below the current line.



 
vi and ex are one and the same editor

             
                oIt is due to William joy<Esc>

vi and ex are one and the same editor
It is due to William joy                                    



 
O opens a line above the current line.

Replacing text

Text is replaced with r, R, s, and S.
To replace one single character by another, use r followed by the character that replaces the one under the cursor. you can replace a single character only in this way.



 

      this is vi full-screen editor



 
   rT

      This is vi full-screen editor 



To replace more than one character, use R followed by the text.
This replacement is restricted to the current line only.
The s replaces a single character with text irrespective of its length. s replaces the entire irrespective of the cursor    position.
S replaces the entire line.

Save and quit

Save and exit can be done using ex mode.
To enter any command in this mode enter a  :  ,which appear at the lat line of the screen.
To save a file and remain in the editing mode use w (write):

:w <Enter>

“myfile”, 8lines, 231 characters

The message shows the file name along with the number of lines and characters saved.
With w you can optionally specify a file name as well, then the contents separately written to another file.
:w virani <Enter>

To save and quit the editor use x ;

:x <Enter>

“myfile”, 8lines ,   231 characters

$_

It is also possible to abort the editing process and quit the editing mode without saving the buffer:

:q <Enter>
$_

Save and exit commands:



 

Command                         Action
:w                               Saves file and remains in editing mode     
:x                                Saves file and quits editing mode
:wq                            Saves file and quits editing mode
:w emp.txt                   It’s like save as option of windows
:q                                Quits editing mode when no changes are  made
:q!                              Quits editing mode after abandoning changes
:n1,n2w new.txt writes line n1 to n2 to file new.txt
:.w new.txt                  writes current line to file new.txt
:$w new.txt                 writes last line to new.txt
:sh                              Escape to Unix shell

Repeating Text

Suppose we want to insert a series of 10 vsc in one line, so instead of using I and the entering 10 vsc, we can write,

10ivsc <Esc>

Command mode

Deletion with x

x command deletes the character under the cursor. Move the cursor  to the character that need to be deleted and then press x

The character under the cursor gets deleted and the text on the right shifts left to fill the space.
4x  deletes the current character as well as three characters from the right.
Deletion with dd:

Entire lines can be removed by command dd,

Cursor movement

h(or backspace)
Moves the cursor left
l (or spacebar)
Moves the cursor right
K
Moves the cursor up
J
Moves the cursor down


 The word oriented navigation:
B
Moves back to beginning of word
E
Moves forward to end of word
W
Moves forward to beginning of word

A repeat factor can be used.4b takes the cursor four words back.
The beginning or end of line:
To move the first character of a line
0 or |                           #30| moves cursor to column 30

to move to the end of the line use $

paging and scrolling

<ctrl-f>             scrolls full page forward

<ctrl-b>             scrolls full page backward

<ctrl-d>             scrolls half page forward

<ctrl-u>             scrolls half page backward

<ctrl-l>              redraws the screen

You must be in command mode to use this control keys.

Moving between lines

If you want to the line number of your current cursor position ,press <ctrl-g>.

“script.sh” [modified] line 403 of 541 –74%--

In vim ,in Linux the current line number and column number appears automatically.
If you know the line number you can use G to move cursor to a specific line number.
30G                  #goes to line number 30

End of file is reached by simply using G

Pattern Search

Lines containing a string can also be located by prefixing the string with / (front slash).To locate the first occurrence of  the string “virani” use,

/virani<Enter>

The cursor will be positioned at the first character of the first occurrence of this pattern.
? searches in the reverse direction.

Repeating the last pattern search:
For repeating search in the direction of the previous search made with / or ? use n and for repeating the search in the reverse direction, use N

Joining lines:

Lines can be joined with J

4j    #joins following three lines with the current line

Ø  The  last command can be repeated using . (dot)
Ø  To undo last instruction use u
Ø  In vim (Linux) multiple undo and redo is possible with repeat factor
C<ctrl-r> means three redo and 3u means to undo last three actions.

0 comments:

Post a Comment