1
Sur vival
In the Beginning
This is a synopsis of the information on vi contained in chapter 2 of the book,
with all of the extraneous information shorn away.
Vi appears to work fine on our systems with whatever the default terminal type is.
There should be no need to change this.
Vi has 2 modes, command and input mode:
Command mode: When in this mode, whatever you type will not appear on the
screen. Instead it will be interpreted as instructions to vi on what it is you want to do.
What you type in command mode has to be recognizable as a command sequence.
Command mode is case sensitive. If you are in the wrong case and you type in a
command it may not be recognized or it may be a command that does something else.
By default when you start vi, you enter command mode. To get into or return to
command mode at any time while vi is running, hit the ESC key, which is in the
upper left hand corner of the keyboard.
Input mode: When in this mode, you are entering stuff that will appear on the screen
and can be saved in a file. In input mode what you type will appear on the screen. If
you are in command mode, the way to enter input mode is by hitting the “i” key.
(This is mnemonic. “i” is for input.) Notice that if you are already in input mode,
hitting the “i” key will result in an “i” being placed on the screen.
Here is the most basic sequence of steps for creating your first file in Unix using
vi. Try it and then read on.
From the shell prompt:
vi filename The file doesn’t have to exist in advance. This will create a new one.
i Press the i key. It will not appear. This starts the editor’s input mode.
Type some stuff
ESC Press the escape key. This takes you from insert to command mode.
:wq Type this sequence. It stands for “write, quit”.
ENTER Other editor commands take effect immediately, but writing and quitting
is concluded by pressing the enter key.
Once a file has already been created using vi, it is possible to edit the file again.
You start the new editing session just like you started the old one, with the exception that
a file of this name already exists:
vi filename
2
More about vi
Vi was designed so that you could accomplish just about everything without
moving your fingers from the home keys on the keyboard. No moving your hand to the
mouse, no function keys, no control key sequences.
Moving around in a file: The standard arrow keys will allow you to move
around in the file. However, when you’re in command mode, the right hand keys also
serve the same function:
h = left arrow
j = down arrow
k = up arrow
l = right arrow
Putting text into a file: When you’re in command mode, these single letter
commands start input mode, allowing you to enter text into the file:
i = insert (enter text at the current cursor position if not the end of a line)
a = append (append text at the end of a line)
o = open (open a new line for input at any point in the file)
Deleting text from a file: Vi does this in its own way, consistent with its other
commands. What the right hand inputteth, the left hand deleteth away. When you’re in
command mode, these commands allow you to do deletions:
x = delete (delete the current character. Note that this does not backspace. It works like
the delete key, eating up characters from the right.)
dw = delete word (if the cursor is anywhere on a word, this will delete that word)
dd = delete line (if the cursor is anywhere on a line, this will delete that line)
Undoing deletions: The command to undo the most recent deletion is:
u = undelete
How not to delete: In order to erase or correct mistakes, on our system, do not
use the CTRL combinations mentioned by the author. Also note that the backspace key
does not work. You are limited to doing deletions either with the delete key or the x key
in command mode.
Quitting an editing session: To end an editing session, the commands are shown
below. Ignore the book’s comment about a ZZ command.
:q = quit (simply quit. This works if there were no changes.)
:q! = quit and do not save any changes made since the file was opened
:wq = write quit (save the changes from the current editing session)