How to use Unix Editor Vi (Vim)

Today vi is considered the standard. It is the only editor that will be  installed by default on any UNIX system.

It is small, fast and efficient; useful to make some minor editing of system files, editing of huge data files or when a slow network link is involved.

It is important to learn at least the basics of vi.

Vi: Start-up, Modes, Save and Quit

To enter Vi type:

 vi FILENAME

Vi has two modes: Normal (command) mode and edit mode.

 Switch to normal mode:     <ESC>
 Switch to edit mode:       i or a

Getting out of Vi (change to normal mode <ESC>):

 Exit Vi:                       :q
 Exit Vi (ignore changes):      :q!
 Save:                          :w
 Save and Exit:                 :wq

Switch to (edit) an other file:

:edit FILENAME

Getting help:

:help topic

Vi: Move, Delete and Paste

Change to normal mode with <ESC>.

Move: Use either the arrow keys or the hjkl keys:

 h (left)    j (down)    k (up)    l (right)

 Getlocation and file status:   Ctrl-g
 Moves to end of the file:      Shift-G
 Moves line NUMBER:             NUMBER Shift-G

Delete:

 Delete a character:     x
 Delete a line:          dd

For multiple deletion precede command with a number.

 Delete 5 characters:     5 x
 Delete 7 lines:          7 dd

Paste: inserts all you deleted with the preceding delete.

 Paste:	                 p

Vi: Search and Replace

Change to normal mode with <ESC>.

Search (Wraped around at end of file):

  Search STRING forward :   / STRING.
  Search STRING backward:   ? STRING.

  Repeat search:   n
  Repeat search in opposite direction:  N  (SHIFT-n)

Replace: Same as with sed, Replace OLD with NEW:

 First occurrence on current line:      :s/OLD/NEW

 Globally (all) on current line:        :s/OLD/NEW/g

 Between two lines #,#:                 :#,#s/OLD/NEW/g

 Every occurrence in file:              :%s/OLD/NEW/g

 VIM QUICK REFERENCE CARD or VI Cheat Sheet

Leave a Comment

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top