Thursday 29 September 2011

Mastering vi Editor




1. Start-up
vi filename

2. vi Modes
vi has two modes

  •     Command mode: In command mode, the letters of the keyboard perform editing functions (like moving the cursor, deleting text, etc.). To enter command mode, press the escape <Esc> key. By default vi starts in command mode. 
  •     Insert mode: In insert mode, the letters you type form words and sentences. Unlike many word processors, vi starts up in command mode.

3. Entering Text
Press i
If you make a mistake, pressing <Backspace> or <Delete> may remove the error, depending on your terminal type.

4. Moving the Cursor
To move the cursor to another position, you must be in command mode. If you have just finished typing text, you are still in insert mode. Go back to command mode by pressing <Esc>. If you are not sure which mode you are in, press <Esc> once or twice until you hear a beep. When you hear the beep, you are in command mode.
The cursor is controlled with four keys: h, j, k, l.

     Key        Cursor Movement
     ---        ---------------

     h        left one space
     j        down one line
     k        up one line
     l        right one space
   
5. Undoing
To undo your most recent edit, type
     u
To undo all the edits on a single line, type
     U (uppercase)
Undoing all edits on a single line only works as long as the cursor stays on that line. Once you move the cursor off a line, you cannot use U to restore the line.

6. Closing and Saving a File
With vi, you edit a copy of the file, rather than the original file. Changes are made to the original only when you save your edits.

To save the file and quit vi, type
     ZZ
The vi editor editor is built on an earler Unix text editor called ex. ex commands can be used within vi. ex commands begin with a : (colon) and end with a <Return>. The command is displayed on the status line as you type. Some ex commands are useful when saving and closing files.

To save the edits you have made, but leave vi running and your file open:

    Press <Esc>.
    Type :w
    Press <Return>.

To quit vi, and discard any changes your have made since last saving:

    Press <Esc>.
    Type :q!
    Press <Return>.

No comments:

Post a Comment