Geting To Grips With vi
vi is the text editor that comes supplied with the NetBSD
distribution. You will almost certainly have to use vi to make changes to
many of NetBSD's configuration files. Unlike Amiga text editors vi has no menus, instead it is completely controlled by the keyboard. vi has two modes of
operation, command mode and insert mode. In command mode each key performs a
different function, for example typing d5d
will delete five
lines of text! vi automatically starts in command mode.
In insert mode vi acts as a normal text editor and anything typed at the
keyboard will be inserted into the file at the current cursor position.
Back Up File Before Editing
Before you edit a file you should always make a backup copy of it. Apart
from the fact the it is very easy to completely trash the contents of a file
by pressing the wrong keys when using vi, it also offers some protection
should NetBSD crash whilst you are editing.
cp filename filename.old
Starting and Quiting vi
Now you can safely edit the file using the command
vi filename
If you make a mistake sometimes the easiest option is to exit vi and start
again. To exit without saving any changes use the key
combination.
<esc> q ! <enter>
To exit and save the file use the key combination
<esc> w <enter>
If you are presented with a "permission denied" message you can override
the access permissions by using the key combination
<esc> w ! <enter>
Note:you must be the owner of the file or root for this
operation to succeed.
Navigating in vi
If you have your terminal emulation set correctly you can use the arrow
keys to move the cursor around the screen. If the cursor keys do not work
you can use the following keys to move the cursor.
up =k
down =j
left =h
right =l
G = Go to end of file
Searching
You can search for occurrences of a particular word of a string by
using
/string_to_search_for
typing / on it's own will cause vi to repeat the last search
Editing
The following keys can be used to edit the file
x = delete character
dd = delete line
r = replace single character
cw = replace word
i = insert text at cursor
A = insert text at end of current line
Note: The i and A commands put vi into "edit mode".
To return to "control mode" press the <esc> key.