By the way --
vim
is an improved version of vi -- the visual editor. In particular it
colors C++ programs in a helpful way.
Rule 0 -- READ ALL THE INSTRUCTIONS ON THIS PAGE before starting lab 3
Rule 1 -- terminate every UNIX command
Commands are not executed until you tap the ENTER key.
Set up
Login. Open this page in a browser. Make this window take up about 50%
of your screen.
Open up a terminal window ("f" -> System -> Terminal).
From now on.... type everything in the terminal window that pops up!
This window is working in your HOME directory, by the way. To find out where you are use
pwd(print working directory). Try it now. End each UNIX command by tapping the Enter key.
The following command will configure vi/vim into a more friendly mode than the default:
~dick/bin/Q noviceThis sets up vi/vim to show numbers and modes on the screen. Q is a program I wrote to help students and faculty do exercises, examples, experiments, and simple projects. This set up is permanent until such time as you edit a file called ".exrc" (don't ask). Meanwhile it makes it easy to compile and run simple programs with out leaving "vi".
Change directory to cs201 by typing a Change Directory command:
cd cs201This moves the window to work on your cs201 directory. Your command line prompt should change.
Make a new directory lab03 by using the Make Directory command:
mkdir lab03
Change directory to lab03:
cd lab03Perform all the exercises in this lab inside this directory.
Creating a file using the vi editor
We will now type in and save a small C++ program
in a file called "small.cpp".
Type in this command in the terminal window:
vim small.cppand it creates an empty file called small.cpp.
Rule 2 -- In vi each command is a character
To input text you must tap the "i" key.
Tap the "i" key for input(don't tap Enter!). Input the following code quickly and don't worry about typing errors!
main(){}
(This is the smallest C++ program that compiles and runs. It does nothing of course)
When you have typed in the whole program tap the "Enter" key a few times to give a blank line at the bottom.
Then tap the "Esc" key at the top left of the key board.
When you tap Esc vi/vim shifts back to Command mode so that you can save your work and correct any errors you've made.
Now save your work: Enter the following command into vim:
:wand tap the "Enter" Key.
Look at what you typed. Do you see any obvious errors? You can use the arrow keys to move to the error and change it with the following
| Key | Effect | Why |
|---|---|---|
| x | Delete character under cursor | Typists used to 'x' out errors. |
| X | Delete character before cursor | |
| r | Replace the character under the cursor | |
| i | Insert string of characters until you tap the "Esc" character. | |
| dd | Delete the current line | |
| dw | Delete the rest of the word. | |
| u | UNDO! |
If you now tap the letter "q" (lower case) my "Q" program will compile your program, report any errors, and then run it for you....
Are any compile errors output? If none go to the next experiment.
If you have errors fix it!
More on vi and vim
Look at this page
[ ../vi.txt ]
which gives a fast introduction to vi and
[ ../vi1.gif ]
and
[ ../vi2.gif ]
that show you what each key does. You might like to print these or
download them so you can read them later.
Another Quick Exercise
Use vim to create a file discount.cpp from page 42 in the text book.
Open the text at the right page and get started with
vim discount.cppan then tapping i.
Type the program in from the text and tap the Esc key when done.
Save the program (:w) and check for errors. Then fix them.
Save and quit out of vim (:wq).
This time we won't use Q. Use
g++ -o discount discount.cppto compile and
./discountto test it ... try both costs above and below the bound.
Yet More Vi and Vim
Have a look at
[ ../vi.notes.html ]
my notes on vi -- complete with alphabetical lists of commands.
The CSCI201 search engine on this page will find commands for you.
You would be wise to book mark (add to favorites) this page
so you can refer to it when you need to find a command to
delete a line or open a new line of text.
Copying and Editting a file
We will now change small.cpp into small2.cpp.
Start by using the UNIX cp copy command in your terminal window (if needed first quit vi).
cp small.cpp small2.cppThis creates a copy of small.cpp called small2.cpp.
You can now use
vim small2.cppto change it to output something when it runs -- anything you like....
Again my 'Q' is a quick way to save, compile, and run a C++ program. Inside vim, ESCape from INPUT mode if needed and tap the 'q' key.
(Hint. you may have to add a couple of extra lines before the main program starts....)
Earn Credit
To earn credit either show the lab instructor the three programs or print
them and hand the printouts in.
Notes
lpr program.cppbut has been known to print in a different laboratory and to remove the left hand side of your file!
You can produce some very nice looking print outs of code by first passing your file through the "ASCII to PostScript" program like this:
a2ps program.cpp | lpr
Other ways to compile a program outside vi
:wqand input the following command
c++ -o small small.cpp
Or you can try
Q small.cppand use my 'Q' program.
:!./small2(: means here is a one line command, the ! means that you want to run a program outside vi, and ./area3 selects the program to execute from this (.) directory).
vi +42 small.cpptakes you to line 42.
!c++Similarly you can repeat your last "vi" or vim UNIX command
!vi
You can use the arrow keys to repeat previous commands.
In 'vi' the dot key '.' repeats the previous command, and n repeats the previous search, so you may hear someone doing this:
n.n.n.n.n.n.n.to find and replace an identifier!
A Quick list of UNIX Commands
There are many commands in Linux/UNIX. Here is a list of some of them:
| Command | Purpose |
|---|---|
| cat >f | Upload or input a file called f. End with Enter and Control/D |
| cat f | View file f. |
| lynx u | View a web page with URL u. |
| g++ -o p p.cpp | Compile a program called p. |
| ./p | Execute/run a program called p in this directory(.) |
| make t | Follow a recipe in Makefile to make t |
| cd d | Change working directory(folder) to d |
| pwd | Print working directory |
| ls | List the the names of files and directories in this directory |
| mkdir d | Make a directory called d |
| more f | Display a file f one screen at a time, Tap space for a new screen |
| rm f | Remove a file (dangerous....) |
| mv f n | Change f's name to n, or move it to a directory |
| cp f n | Copy f to a file named n |
| vi f | Edit a file called f (powerful but not easy to use) |
| umenu | A Simple menu driven shell written by CSUSB students for CSUSB students to use. |
| pine | Handle your EMail. |
| pico f | Edit a file called f (easy to use but not powerful). |
See Also
Frequently asked questions about CSCI.CSUSB.EDU
[ ../../doc/CS_FAQ.html ]
. . . . . . . . . ( end of section Notes) <<Contents | End>>
. . . . . . . . . ( end of section CSCI201 Laboratory 4 -- More powerful tools) <<Contents | End>>
Abreviations