7.5 Editing Program Source Code
All of the features discussed so far 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 and Windows-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. There are two additional variants of the [CTRL-D] command.[8 ]
Try using the 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. vi attempts to be smart when doing indenting. Usually, when you see text indented by eight spaces at a time, vi will actually insert tab characters into the file, since tabs usually expand to eight spaces. This is the UNIX default; it is most noticable when you type a tab during normal input, and when files are sent to a printer -- UNIX expands them with a tab stop of eight spaces. If you wish, you can change how vi
represents
tabs on your screen, by changing the :set tabstop=4
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 whitespace, 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
if ( cos(a[i]) > sin(b[i]+c[i]) ) { printf("cos and sin equal!\n"); } and press Similarly if the cursor is on one of the closing bracket
characters, pressing vi
is even smart enough to find a bracket character
for you. If the cursor is not on a bracket character, when you press
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 Another technique for finding matching brackets is to turn on the following option: :set showmatch Unlike
7.5.3 Using Tags
The source code for a large C or 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 in 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 in 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 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. You can use the tag facility from vi
's command
mode as well. Place the cursor on the identifier you wish to look
up, and then type
The Solaris 2.6 version of vi actually supports tag stacks . It appears, however, to be completely undocumented in the Solaris man pages. Because many, if not most, versions of UNIX vi don't do tag stacking, we have moved the discussion of this feature to Section 8.5.3, "Tag Stacks" in Chapter 8 , where tag stacking is introduced. |
|