7.5 Editing Program Source Code
All of the features discussed so far in this book are of interest
whether you are editing English text or program source code. However,
there are a number of additional features that are of interest chiefly
to programmers. These include indentation control, searching for the
beginning and end of procedures, and using The following discussion is adapted from documentation provided by Mortice Kern Systems with their excellent implementation of vi for DOS-based systems, available as a part of the MKS Toolkit or separately as MKS Vi. It is reprinted by permission of Mortice Kern Systems. 7.5.1 Indentation ControlThe source code for a program differs from ordinary text in a number of ways. One of the most important of these is the way in which source code uses indentation. Indentation shows the logical structure of the program: the way in which statements are grouped into blocks. vi provides automatic indentation control. To use it, issue the command:
:set autoindent Now, when you indent a line with spaces or tabs, the following lines will automatically be indented by the same amount. When you press [RETURN] after typing the first indented line, the cursor goes to the next line and automatically indents the same distance as the previous line. As a programmer, you will find this saves you quite a bit of work getting the indentation right, especially when you have several levels of indentation. When you are entering code with autoindent enabled, typing [CTRL-T] at the start of a line gives you another level of indentation and typing [CTRL-D] takes one away. We should point out that [CTRL-T] and [CTRL-D] are typed while you are in insert mode, unlike most other commands, which are typed in command mode.
The amount of indentation provided by
[CTRL-T]
or
:set tabstop=4 will change the tab settings for a file. Try using the autoindent option when you are entering source code. It simplifies the job of getting indentation correct. It can even sometimes help you avoid bugs (e.g., in C source code, where you usually need one closing curly brace (}) for every level of indentation you go backwards).
The
You can shift a number of lines by typing the number followed
by The default shift is eight spaces (right or left). This default can be changed with a command like:
You will find it convenient to have a shiftwidth that is the same size as the width between tab stops. Sometimes indentation won't work the way you expect, because what you believe to be a tab character is actually one or more spaces. Normally, your screen displays both a tab and a space as white space, making the two indistinguishable. You can, however, issue the command:
:set list
This alters your display so that a tab appears as the control
character
:5,20 l displays lines 5 through 20, showing tab characters and end-of-line characters. 7.5.2 A Special Search Command
The characters (, [, {, and < can all be called
opening brackets. When the cursor is resting on one of these
characters, pressing the
if ( cos(a[i]) > sin(b[i]+c[i]) ) { printf("cos and sin equal!"); }
and press
Similarly if the cursor is on one of the closing bracket
characters, pressing
Not only does this search character help you move forward and
backward through a program in long jumps, it lets you check the
nesting of brackets and parentheses in source code. For example, if you put
the cursor on the first { at the beginning of a C function,
pressing Another technique for searching matching brackets is to turn on the following option:
:set showmatch
Unlike 7.5.3 Using Tags
The source code for a large C program will usually be spread
over several files. Sometimes, it is difficult to keep track of
which file contains which function definitions. To simplify
matters, a UNIX command called
The
will create a file named tags under your current directory that contains information on the functions defined in file.c . A command like:
will create a tags file describing all the C source files under the directory. Now suppose your tags file contains information on all the source files that make up a C program. Also suppose that you want to look at or edit a function in the program but do not know where the function is. From within vi , the command:
:tag name will look at the tags file to find out which file contains the definition of the function name . It will then read in the file and position the cursor on the line where the name is defined. In this way, you don't have to know which file you have to edit; you only have to decide which function you want to edit.
|
|