0% found this document useful (0 votes)
60 views

Introduction To Vi: Dr. Mike Murphy

This document provides an introduction to using the vi text editor to write and edit C programs. It demonstrates a basic workflow of using vi to edit a "Hello, World" C program in one terminal window while compiling the code in a second terminal window. The key steps are: 1) Creating and opening a hello.c file in vi 2) Typing C code into the file in insert mode 3) Saving and closing the file using vi commands 4) Compiling the code in another terminal window and fixing any errors by editing in vi

Uploaded by

my_scribd_docs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Introduction To Vi: Dr. Mike Murphy

This document provides an introduction to using the vi text editor to write and edit C programs. It demonstrates a basic workflow of using vi to edit a "Hello, World" C program in one terminal window while compiling the code in a second terminal window. The key steps are: 1) Creating and opening a hello.c file in vi 2) Typing C code into the file in insert mode 3) Saving and closing the file using vi commands 4) Compiling the code in another terminal window and fixing any errors by editing in vi

Uploaded by

my_scribd_docs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to vi

Dr. Mike Murphy


Revised 24 February 2011

Overview
The purpose of this tutorial is to:
Introduce the vi editor
Demonstrate a workflow for programming tasks using vi

This tutorial builds upon the following tutorials:


Preparing a Project Directory
Compiling C Programs on Ubuntu

Background
The vi editor, which stands for visual (by 1976 standards), is a command-line application for
editing text files. This editor predates such features as graphical interfaces, F-keys at the top of
the keyboard, and even arrow keys. It is useful to know how to use vi because it is the one
editor that is almost always installed on Unix systems, even when those systems lack a lot of
basic programs and a graphical interface. Technically speaking, Ubuntu actually supplies an
editor called vim, for vi improved, but the difference doesnt matter too much for our purposes.

This tutorial introduces the editor by demonstrating how to write a Hello, World application in
C, explaining both the basic use of the editor and a simple workflow for programming using the
editor in one Terminal window while compiling in a second Terminal window.

Procedure
1. Prepare a directory to hold a C programming project, and open a second Terminal
window. Arrange the two windows so that you can switch back and forth between them
easily.

2. Were going to create a file named hello.c to hold our basic Hello, World program. An
easy way to create a new, empty file is to use the touch command in one of the
Terminal windows. Run:

touch hello.c
3. Vi is started from the command line, taking the file to edit as an argument. To open vi on
our hello.c file, type the following:

vi h

Now press the Tab key on the keyboard. The CLI will automatically complete the
command for us, saving us some time. You should now have:

vi hello.c
4. Press Enter to start the editor.

5. The key to understanding vi is to realize that its designers had to do something clever to
support keyboards that only had a minimum set of keys. To enable files to be edited,
while at the same time providing a way to search, save, close the editor, etc., vi uses two
different modes: command mode and i nsert mode. The editor starts in command mode,
which upon first opening a file shows the file name and total number of lines and
characters in the lower-left portion of the screen (see above). In this case, our file is
empty, so the cursor appears at the top of the file, and lines below the cursor have the ~
character, which simply helps to show where the file ends.

6. To add some program code to our file, we need to change from command mode to insert
mode. To do this, press the i key on the keyboard (i for insert). You will now see the
word INSERT in the lower-left corner (be warned now that not every version of vi has
this... sometimes you just have to know what mode youre in).

7. Now we can type in some code. Carefully type the following, noting that in general, you
can only use backspace on the current line youre editing (this version of vim is actually a
bit more forgiving and will let you backspace between lines). This code example contains
an intentional syntax error.

#include <stdio.h>

int main(int argc, char * argv[]) {


printf(Hello, World\n)
return 0;
}

You should wind up with something that looks like the following:

8. Now we need to save the file. First, switch back to command mode by hitting the Esc
(Escape) key in the upper left corner of the keyboard. Then, type the following special
command and hit the Enter key to save the file:

:w

That is a colon (full colon, not a semicolon - press shift along with the semicolon key)
followed by the letter w. The w here stands for write -- namely, to write the file to disk.
This version of vi will confirm the file has been written with a message in the lower left
corner of the window.
9. To compile the code, switch to the other Terminal window and run:

gcc -Wall hello.c


You should get the following:

10. The compiler produces an error message:

hello.c:5: error: expected ; before return

This means that the error is probably somewhere around line 5 of the file hello.c. I say
probably because there are some errors where the compiler will give an incorrect line
number.

To go to line 5 of the file, switch back to the Terminal window where vi is running. You
should still be in command mode. Type the following, and press the Enter key, to move
the cursor to line 5:

:5
11. In this case, the syntax error is actually a missing semicolon at the end of line 4. You can
move to line 4 either with the arrow keys or with the command : 4

sign (shift+4). Now type the letter a


To move the cursor to the end of the line, type the $
to go into append mode (really the same thing as insert mode), and type in the
semicolon.
12. It is vitally important to remember to SAVE THE FILE b efore trying to recompile! The
compiler reads hello.c from disk, not from the editor.

13. Once you are finished editing the source file, exit with the command :q

You might also like