Although there are many editors and word processors available for Linux and UNIX, the vi visual editor (always pronounced "vee-eye", never "vie" or "six") is the standard editor that is available on every system. You will come to appreciate its many powerful features and its universal availability. Another widely used editor is emacs which is more powerful and also much more difficult to learn. The version of vi most often found on Linux systems is actually vim with many extensions beyond the simple and basic vi.
When using vi remember that it was originally developed before the arrow keys, Page Up/Down keys, and other editing keys were commonly available on keyboards. To work around this, all forms of vi define three different operating modes in addition to any extensions that may have been added:
command mode: cursor movement and simple editing (ESC to return from text mode);
text mode: text insertion (following Ins, i, o, etc.);
edit mode: (or ed mode) more sophisticated editing (":"from command mode to enter).
Therefore each key on the keyboard can have three entirely different meanings, and some have more with various shift combinations. Try both p and P for pasting, for example, in command mode.
When you first start vi, you are in command mode. Press i to enter text mode and type your material. Return to command mode by using the ESC key to leave text mode. Make changes or corrections from text mode as needed. When you are finished, enter edit mode with : to save your file and exit. Use :w to write your file to disk, or :w <filename> if it needs a new name, and :q to quit or exit. You may combine :w and :q as :wq.
Even though there is an undo facility, you should develop the habit of saving files often, and making backups of your files from time to time in case of keyboard operator error.
The diagram below illustrates methods of moving between each mode, and some of the keys available in each mode.
|
|
Some basic commands for vi
These basic commands are enough to get you started and to complete most editing tasks. They will also help you with mid-term and final exam questions about vi.
|
i insert new text in front of cursor |
o open a new line after (below) cursor |
|
a insert new text after cursor |
O open a new line before (above) cursor |
|
A move to end-of-line and insert new text |
|
In vim, you can also use the arrow, Home/End, and PgUp/PgDn keys in both command and text mode.
|
k move backwards (up) one line |
|
|
- move backwards (up) one line |
|
|
h move backwards one character |
l (lower case L) move right one character |
|
b move backwards one word |
sp (space) move forward one character |
|
j move down (forward) one line |
|
|
↵ (ENTER) move down one line |
|
Each can be preceded by the number of items to use: 3dd will cut/delete 3 lines.
|
x delete one character (under cursor) |
dd cut (delete) current line |
|
r replace character (under cursor) |
yy copy (yank) current line |
|
dw delete one word (containing cursor) |
p paste most recently cut/copied text |
|
cw change word (from cursor) |
u undo last editing command |
|
/text search for next occurrence of text |
?text search backwards for a preceding text |
|
:s/old/new/ replace old with new once |
|
|
:s/old/new/g replace all occurrences of old with new on the current line only |
:1,$s/old/new/g replace all occurrences of old with new on all lines from 1 to $ (last line); may also use % to represent 1,$ (all lines) |
|
:w filename write as filename |
:q quit, exit, leave vi |
|
:w write using existing filename |
:q! quit at once, discarding all changes |
You will often find vi commands in other places as well, either derived from vi itself or often from a common ancestor program (like ed). For example, the / and ? search commands work in more, less, and man, and a close relative of :s works in sed. An excellent reference book is "Learning the vi Editor" by Linda Lamb and Arnold Robbins, O'Reilly Associates (ORA) 1998, 6th edition, ISBN 1-56592-426-6.
Compiling simple C language programs is a one-step procedure, since the compiler also performs a linking or binding phase as part of its default actions. In this course we will use the GNU C compiler gcc (GNU Compiler Collection) and its associated tools. On many systems, cc will invoke the default C compiler. By the way, GNU stands for GNU's Not Unix, a self-referencing acronym or initialism, and an infinite loop if you think about it.
Note that the C compiler uses filename extensions to determine the type of a file (e.g. C source files are always .c , object files use .o, include files are .h, and so on).
The most basic usage of the command is
gcc demo.c
which produces an executable program called a.out, the usual default executable name.
The -o option allows you to specify the name of the executable, as in:
gcc -o demo1 demo.c
which produces an executable program named demo1.
However, we will always use the following options on any form of gcc that we use: -ansi -pedantic -Wall -Wextra -O2, where
-ansi specifies the standard C89/C90 form;
-pedantic requests strict adherence to that standard;
-Wall asks for all warning and error messages (but see the next item);
-Wextra has some more error messages;
-O2 (capital OH, not zero) is an optimization level that can sometimes find more errors.
The option -l (lower case L) specifies the name of a link library, such as m for specialized mathematical functions like sqrt(), sin(), and so on:
gcc -ansi -pedantic -Wall -Wextra -O2 -lm -o demo2 math-test.c
Note that the name of a library and even the need for some of them will vary from system to system.
In each of the three gcc examples above, the compiled program is executed simply by typing its name (often with a path like ./) on the command line: ./a.out, ./demo1, or ./demo2. Note that none of them uses a .exe extension like MS Windows. Although you could do that if you chose to, there is no need to do so (and people will laugh! and probably point at you!):
gcc -ansi -pedantic -Wall -Wextra -O2 -o demo3.exe trial2.c
The -c option forces compile-only, by disabling the linking step as in:
gcc -ansi -pedantic -Wall -Wextra -O2 -c demo4.c
which produces only the object file named demo4.o from the filename of the source. There is no point in using -o or -l options if -c has been specified since they are only used in linking.
Since the compiler relies on file name extensions, it correctly handles application programs split across multiple source files:
gcc -ansi -pedantic -Wall -Wextra -O2 -c part1.c part2.c part3.c
gcc -ansi -pedantic -Wall -Wextra -O2 -lm -o all part1.o part2.o part3.o
which performs only the three separate compiles in the first command, and only the single link phase in the second command. Note that .c files may be intermixed with .o files in a single compile command:
gcc -ansi -pedantic -Wall -Wextra -O2 -c part1.c part2.c
gcc -ansi -pedantic -Wall -Wextra -O2 -lm -o all part1.o part2.o part3.c
Linux and UNIX have a standard utility called make that reads a file of commands (somewhat like the last example) to determine file dependencies and do re-compiles and re-links as necessary to create an up to date version of a program you have just modified. You will learn some simple uses of make early in this course.
Your programs will not work perfectly the first time (a great surprise to you, I am sure). Many people use print statements of the appropriate flavour to debug their programs, a practice best suited for the most trivial of programs but sometimes very useful.
Real programmers use professional debugging tools whenever possible.
Most compilers produce two kinds of messages to the programmer: errors and warnings. You must fix each error or the executable file will not be produced, but some people are inclined to ignore mere warnings — at their peril! Make sure you understand why each warning message has been issued. If it's truly trivial you can leave it alone for a short time, but you must never submit an assignment or in-lab exercise that still has warning messages in it.
Many warnings do in fact indicate fatal run-time errors. They are warnings only because the compiler can still make some kind of sense from the source code, not necessarily matching what you meant. At the very least, they are ambiguous and need to be clarified.
Pointers. Pointer errors, sometimes as array references, sometimes as function arguments. You are trying to address something at location 0x00000003 or at NULL, or you forgot a dereference level, or added one, or you need an &, or you have one but shouldn't have. Or it's an un-initialized pointer, or a clobbered one. Find where it is, using gdb,and fix it. It's often located by looking at compiler warning message.
If you include the -g option in the gcc command (both for compile and link), the C compiler will keep additional information that can be used by symbolic debuggers. Symbolic or source-level debugging allow you to set break points in the code to halt execution and jump over loops, allow you to inspect and set the value of variables, and more, while working from an on-screen listing of the source code.
This is the text-only command line version of the interactive source level debug tool for Linux using gcc. Note that you can use this debug tool in the lab or remotely via telnet or ssh.
Use the man command to learn about it, and once you have it up and running try its own internal help system. We will also have a lab exercise on it.
Since Linux often uses a graphical desktop interface (I prefer KDE but many like Gnome), there is also the ddd debug tool for use mainly n the lab. It is simply a graphical wrapper around gdb and therefore offers few extensions beyond point-and-click.
You may find it easiest to learn it by testing it with a small and fairly simple test program with known bugs once you have a basic understanding of gdb.