How to use Vi editor?

Starting vi
    You may use vi to open an already existing file by typing
     vi filename

vi Modes
     vi has two modes:
    * command mode
    * insert 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.
     In insert mode, the letters you type form words and sentences. Unlike many
word processors, vi starts up in command mode.

 Entering Text
     In order to begin entering text in this empty file, you must change from
command mode to insert mode. To do this, type
      I

Deleting Words
     To delete a word, move the cursor to the first letter of the word, and type
      dw
      This command deletes the word and the space following it.
      To delete three words type
       3dw

Deleting Lines
    To delete a whole line, type
     dd
    The cursor does not have to be at the beginning of the line. Typing dd deletes
the entire line containing the cursor and places the cursor at the start of the
next line. To delete two lines, type
       2dd
     To delete from the cursor position to the end of the line, type
       D (uppercase)

Replacing Characters
    To replace one character with another:
   1. Move the cursor to the character to be replaced.
   2. Type r
   3. Type the replacement character.
   The new character will appear, and you will still be in command mode.

Replacing Words
    To replace one word with another, move to the start of the incorrect word and
type
     cw

You are now in insert mode and may type the replacement. The new text does
not need to be the same length as the original. Press <Esc> to get back to
command mode. To replace three words, type
     3cw

Replacing Lines
    To change text from the cursor position to the end of the line:
   1. Type C (uppercase).
   2. Type the replacement text.
   3. Press <Esc>.

Moving around in a file
     H            to top line of screen
     M            to middle line of screen
     L            to last line of screen
     G            to last line of file
     1G           to first line of file

Moving by Searching
    To move quickly by searching for text, while in command mode:
   1. Type / (slash).
   2. Enter the text to search for.
   3. Press <Return>.
   The cursor moves to the first occurrence of that text.
   To repeat the search in a forward direction, type
     n
    To repeat the search in a backward direction, type
     N

To save the edits you have made, but leave vi running and your file open:
   1. Press <Esc>.
   2. Type :w
   3. Press <Return>.
To quit vi, and discard any changes your have made since last saving:
   1. Press <Esc>.
   2. Type :q!
   3. Press <Return>.

Comments

Popular Posts