The program vi is a visual text editor for Unix. It is the de facto
standard visual editor for Unix systems. A visual editor presents
a full screen representation of the contents of the file in
a terminal window called the screen. On the screen there is always a cursor
which indicates the current insertion point.
The vi editor is
designed so you only need the normal typewriter keys (plus the ESC
key). If you are a touch-typist you will find that, with some practice, you
can do all the tasks needed to edit a file without moving your hands far
from the touch-typing home keys. The arrow keys, although not needed, are
frequently easier to use to move the insertion point around the file.
Since vi only uses the standard typewriter keys, it uses editor modes to know which action is assigned to each key. The two primary modes are command mode and insert mode.
You must always remember which mode you are in; thus vi takes some practice to become proficient.It is important to remember that vi only uses the keyboard keys, and everything is done in reference to the cursor. If your terminal window is on a system having a a mouse, then the mouse has no direct relationship to the vi operation. (You can use the mouse to select text and paste it from the clipboard onto your vi screen as long as you are currently in insert mode.)
The vi program is available on Strauss and Mahler and most other Unix systems. To edit a file, type the Unix command:
vi fileName
If fileName
does not exist, vi will create a new empty file
having that name.
We recommend installing an extended version of vi called vim, which gives you all the features of the Unix keyboard as well as a screen-oriented interface. You can get vim from the vim online web site. You will also get enhanced features allowing your to use a mouse in an MS Windows environment.
Use the vi Unix command. To edit a file named "program.c", for example, type the command
vi program.c
If program.c does not exist, vi will create the file.
You will get a screen that displays the contents of your file.
#include "theta.h" int main() { int i; printf(" i theta0 theta1 inv(theta0,theta1)\n"); for (i=1; i<=32; i++) { printf("%3d %6d %6d ",i,theta0(i),theta1(i)); printf("%13d\n",invTheta(theta0(i),theta1(i))); } } ~ ~ ~ ~ ~ ~ ~ ~ ~ "program.c" 10 lines, 236 characters
The lines with start with ~
are placeholders and indicate that the line is empty.
When you first start vi, you begin in command mode. The command mode keystrokes let you move around the screen. These one-letter commands are also used in many other Unix commands, so it is worth learning them. The commands are case-sensitive: an upper-case X has a different action than a lower-case x.
All of these motion commands will scroll the window, if needed, so the cursor remains visible on the screen. Thus you can move to any point in the file with these commands.
When you start vi with the vi
command, you begin in
command mode. All the keys you type will be interpreted as
commands, and they will not appear in the document. To insert text
you must change to "insert mode" by typing i (or a).
The ESC
key returns you to command mode.
There are many text-modification commands to delete, replace, modify, and insert text in the document. These commands are described in the tutorials listed in the More Help section below.
To exit vi and save the file, type
the sequence :wq
or ZZ
.
(If the characters :wq
or ZZ
appear in the text, it is because you are in "insert mode", not
"command mode." You must type the ESC
key to
exit insert mode and re-enter command mode. Then delete the unwanted
characters and retype
the sequence :wq
or ZZ
.
To exit without saving the file, just type
the sequence :q!
.
No. The vi program needs very little to run; it does not even need the arrow keys to work properly. However, there is a setup file and some things you may want to do to make what you learn using vi more broadly useful.
.exrc
file:
set ts=4Advanced users can set function keys in the
.exrc
file to have macros
available while using
vi. .cshrc
file.
setenv VISUAL vi
tcsh
has a built-in editor you can
use to edit the Unix command you are currently typing
or previously typed commands before you
submit the command with a RETURN
. You can tell tcsh
to use the vi key bindings you have learned in using vi with
the shell command bindkey -v
. If you want this behavior
for all shells put this line in your .cshrc
file:
if ?$tcsh bindkey -v
Note:
We recommend putting
all your shell run-control commands in .cshrc
, the csh
run-control file. For commands that work only in the tcsh
, prepend if ?$tsch
in front of those commands, as illustrated above.
There are many thousands of web pages describing the use of vi. Here are a small sample:
If you need help quickly, you may call the IT Help Center at 831-6000 between 8 a.m. and 5 p.m., Monday through Friday. Or, you can submit a question through e-mail.