Troubleshooters.Com and T.C Linux Library Present

VI Quick Start

Copyright (C) 1999 by Steve Litt
[ Linux Library | Troubleshooters.Com | Email Steve Litt | Copyright Notice ]

Getting Help

Inside vi, :help (press the colon key, then type the word help, then press the enter key).
 

Starting VI

From the command prompt, type vi and press the enter key. Or, to edit a specific file, type vi, then a space, then the filename, then the enter key. Use the -R argument to edit a file in read-only mode.

Examples:
 
vi
Run the editor with no file
vi /etc/hosts
Edit /etc/hosts
vi -R /etc/hosts
Edit /etc/hosts in read-only mode -- view it

Quitting VI

From inside vi, press the colon key to get into command mode, then press the proper quit command:
 
:q
Quit, no save, from an unmodified file. Gives an error message if the file has been modified since its last save.
:q!
Quit, no save, no confirmation, whether the file has been modified or not.
:wq
Write the file and quit.

Search and Replace

 
To substitute new for the first old on a line type
s/old/new
To substitute new for all 'old's on a line type 
s/old/new/g
To substitute phrases between two line #'s type
#,#s/old/new/g
To substitute all occurrences in the file type
%s/old/new/g
To ask for confirmation each time add 'c'
%s/old/new/gc

 

VI Tips:

If you want to eliminate every line EXCEPT those matching a search criteria:

:g!/searchstring/d

Then, to sort the remainder:

:%!sort
 

Finally, to delete duplicates from your sorted file:

:%!uniq
 
 


See also: [ Code Corner | Troubleshooters.Com | Email Steve Litt | Copyright Notice ]

Copyright (C)1997 by Steve Litt. -- Legal