[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci201] >> [Lab01] >> index
[Index] [Schedule] [Syllabi] [Glossary] [Labs] [Projects] [Resources] [Grading] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Wed Jan 16 13:57:10 PST 2008

Contents


    CSCI201 Laboratory 3 -- More powerful tools

      Goals

      To practice using UNIX commands rather than the mouse.

      Why


      1. You need to work at home on our computers using SSH.
      2. You can type faster than you can mouse.
      3. Computer scientists master the low level control of machines.
      4. Some of the power commands impress your friends, colleagues and bosses.

      Process

      We abandon using the KDE Graphic User Interface and use only a browser and a terminal window. Inside the terminal window you will practice commands like cd (change directory), mkdir(make directory), and the editor vi/vim.

      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 novice
      This 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 cs201
      This 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 lab03
      Perform 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.cpp
      and 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:

       		:w
      and 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
      KeyEffectWhy
      xDelete character under cursorTypists used to 'x' out errors.
      XDelete character before cursor
      rReplace the character under the cursor
      iInsert string of characters until you tap the "Esc" character.
      ddDelete the current line
      dwDelete the rest of the word.
      uUNDO!

      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.cpp
      an 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.cpp
      to compile and
       		./discount
      to 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.cpp
      This creates a copy of small.cpp called small2.cpp.

      You can now use

       		vim small2.cpp
      to 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

        Printing from the terminal window

        This command seems to work
         		lpr program.cpp
        but 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

          You can quit "vi"/vim
           		:wq
          and input the following command
                          c++ -o small small.cpp

          Or you can try

           		Q small.cpp
          and use my 'Q' program.

        Rerunning a program inside vi

        You can then do another test without recompiling by typing this command into 'vi'
         		:!./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).

        Going to a line number in a file inside vi

        You can move rapidly to line 42 for example by tapping these keys as a vi command: 42G. The "G" is for "Goto". You can also make many arrow movements by typing the number before the arrow key.

        Go directly to line 42...

        Suppose there was an error was on line 42 then the UNIX command:
                        vi +42 small.cpp
        takes you to line 42.

        Repeating previous commands

        In UNIX you can repeat the c++ command by inputting this command:
         			!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:
        CommandPurpose
        cat >fUpload or input a file called f. End with Enter and Control/D
        cat fView file f.
        lynx uView a web page with URL u.
        g++ -o p p.cppCompile a program called p.
        ./pExecute/run a program called p in this directory(.)
        make tFollow a recipe in Makefile to make t
        cd dChange working directory(folder) to d
        pwdPrint working directory
        ls List the the names of files and directories in this directory
        mkdir dMake a directory called d
        more fDisplay a file f one screen at a time, Tap space for a new screen
        rm fRemove a file (dangerous....)
        mv f nChange f's name to n, or move it to a directory
        cp f nCopy f to a file named n
        vi fEdit a file called f (powerful but not easy to use)

        UNIX for Beginners

        umenuA Simple menu driven shell written by CSUSB students for CSUSB students to use.
        pineHandle your EMail.
        pico fEdit 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

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. TBA::="To Be Announced", something I have to do.
  3. TBD::="To Be Done", something you have to do.

End