Home > Help Files > Unix > Vi Editor Commands

Vi


	SOME HINTS ON  USE OF THE VI EDITOR ON UNIX

	Prepared by C W Radcliffe for ME 128 students


The vi editor is a full screen editor and a very powerful
one.  The number of possibilities boggles the mind of the
simple souls who only want to create and edit some text.
The following is a list of some of the commands which
will enable you to get started.

1. CURSOR CONTROL

The cursor is moved using single key strokes on the keyboard
when you are NOT in an append or insert mode.  No CTRL or
ESC key is used here.  The direction moved is as given below.

h   moves the cursor  left
l                     right
j                     down
k                     up
w                     one word to the right
b                     back one word
              one line down
-                     one line up (same as using k)

2. SCREEN MOVEMENTS -- SCROLLING

CTRL - f              One screen forward
CTRL - b                         backward
CTRL - u              Scroll upward
CTRL - d                     downward

3. DELETIONS

Again, characters are used without CTRL or ESC.

x                     Delete character under the cursor
dw                           word containing the cursor
dd                           line containing the cursor
3dd                          3 lines

4. SEARCHING AND SUBSTITUTIONS (REPLACE)

/pattern              Search forward in the file for the pattern
?pattern                     backward
/ or ?                Repeat search for the pattern last specified
rx                    Replace a single character with x
R                     Replace multiple characters

5. INSERTING AND APPENDING TEXT TO THE FILE

a                     Append what follows AFTER the cursor
i                     Insert what follows BEforE the cursor
ESC                   End the append or insert mode
o  (use letter o)     Open a new line below the cursor
O  (use letter O)     Open a new line above the cursor

6. COMMAND MODE

Command mode can be called from vi and is better for some 
operations.  For example, to substitute one pattern for
another after you have found it in vi.

:                     Prefix to a command mode instruction
                      (commands are like ex instructions)
:                     The prompt while in command mode or ex
:1,5p                 Print lines 1 to 5, inclusive
:1,$p                 Print line 1 to end-of-file, inclusive
:nu                   Print current line number
:s/pat1/pat2/         Substitutes pat2 for pat1 in current line
                      Either / and ; work as delimiters.
                      pat2 could be a lot of new text.
:s/pat1/pat2/g        The 'g' means global.  Substitute pat2 for pat1
                      for each occurence in the line.
:g/pat1/s//pat2/      global search for each occurence of pat1 ... after it is
                      found then substitute pat2 (// uses previously defined
                      pat1) for pat1.  Global search and replace.
:w filename           Write current buffer to file, then return to vi
                      An easy way to rename the edited file.
:x                    Exit from command mode saving the changes
:q!                   Quit from command mode discarding changes
ZZ                    Exit from vi saving changes

7. THE EX/VI QUICK REFERENCE CARD HAS ALL THE COMMANDS

 


Comments to consult@newton.berkeley.edu
© 1998-2008 UC Regents