Vi & Vim

Vi is an editor which can be found on almost any Unix or Unix-like machine. So almost any computer running an operating system such as UNIX, FreeBSD, Linux or similar will have the vi editor installed. Unfortunately the most people experience vi as the most user unfriendly editor there is. They definately haven't seen Emacs or sed, but that's another issue. This page will provide some basic things you need to know about vi in order to learn to use it. For almost any Unix program the RTFM-rule (Read The F*cking Manual) applies, so vi also can be learned in that way. Tutorials are available and many systems will let you type in "man vi" to access the vi man pages. But it may be handy to know some basic things about vi before tarting around with it.

Since many persons didn't like the way vi had to be used an effort was made to make the editor more user friendly. The editor was rewritten and improved in many places. This new version of vi was more user friendly and therefor it was called vim which is an ancronym for Vi iMproved. Obviously many versions of this editor are around. Some really nice versions of vim also have syntax highlighting for many purposes like programming Java, C++ or JavaScript, or writing HTML, XHTML, XML, CSS, etc.

Basic Vi/Vim Commands

Note that all commands are case sensitive. So the i command is not the same as the I command!

Inserting and appending
i Insert before this character
I Insert before the first character on this line
a Append after current character
A Append after the last character on this line
 
Movement
O Go to the first character on this line
$ Go to the last character on this line
% Go to last line of this file
j Go down one line
k Go up one line
h Go one character left
l Go one character right
/ Find pattern that matches the text placed after the / or find the pattern previously searched for if nothing is placed after the /
 
Changing Text
s Replace the current character
ns Replace n characters
r Replace the current character with one different character
nr Overwrite n characters
R Overwrite the remainder of the current line
C Replace the remainder of the current line
S Replace the current line
 
Deleting
x Delete the current character
nx Delete n characters
D Delete the remainder of the current line
d Delete the current line
nd Delete n lines

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 

Links

Contact the author: Send an electronic mail to: pajtroon@dds.nl.
Peter's ICQ Number is: #3900785.

This page: Copyright © 2002 Peter A. J. Troon

Note: This page is part of the Peter Troon Site.