CS416 Spring 2001
Tab Removal

Overview

An extremely important aspect of creating high quality code is the readability of that code. In particular, it is critical that other people are able to read your code easily. A critical aspect of code readability is the indentation you use to format your code. A properly indented program shows the overall structure of the program at a glance and is much easier to read and understand.

Programming Assignments

Since we evaluate your program's readability as part of the grading process, it is important that you turn in your assignments in a form that we see the indentation that you want to show us. The biggest problem with this is tabs. Tabs are not handled consistently across all utilities. Most program editors provide options for setting tab spacing, but those settings are not usually associated with the file itself. Furthermore, print utilities seldom do anything except convert tabs to a fixed set of tab positions (usually every 8 spaces). This means that "what you see" is not necessarily "what we see". Many software organizations simply outlaw tabs in any code file.

Removing Tabs from Programs

There is a Unix utility, expand, for removing tabs from any file. If you've been using an editor on a file called A1.C with your tabs set at every 4 spaces, you can replace all your tabs with the command:

% mv A1.C tempfile
% expand -4 tempfile > A1.C

Once you've checked that it worked correctly, you can delete tempfile. The most common mistake you can make is to use the -4 option on a file that was built using a different tab spacing (such as the vi default of 8). If you do that, then your program looks awful and you can only fix it by editing it line by line. If you don't delete the tempfile, you can copy it back and run expand with -8 (or whatever should have been used).

Generating Files with No Tabs using vim

For those of you that use vi, there is an easier solution, however. If you switch from the "old" vi to the new improved version, vim, you can set its options so that you can still enter your program with tabs, but they are not put into the file. The easiest way to do this is to add the following line to your .login file which is located in your home directory:

setenv EXINIT "ai aw tabstop=4 expandtab"

If you do this, every time you run vim, the settings described in the EXINIT environment variable will be established. All of these settings except expandtab are also supported by the older vi.

tabstop=4 sets tabs at every 4 spaces. You can make this any number you want. 2,3,4 are reasonable.

expandtab tells vim to replace any tab typed with the appropriate number of spaces (to the next tabstop).

ai "automatic indent": When set, vim automatically aligns a new line of input (as you are typing) with the indent level of the previous line.

aw "automatic write": When set, vim always writes the current file to disk whenever you leave the editor for any reason.

AutoWrite and Edit/Compile/Run Cycle

The aw option has nothing to do with formatting, but it is a good one to use when you are in an "edit-compile-run-edit" cycle. After making corrections in the editor, you can exit the editor using ctrl Z, instead of :wq or ZZ. Ctrl Z writes your file (if aw is set) and then suspends the editor rather than terminating it; the editor is said to be "in the background". You can now compile and possibly run your program. Let's assume you got some compiler errors. You can return to the suspended editor using the command:

% fg

which says, "find the most recently suspended program in the background and move it to the foreground"; i.e., start running the editor again. You will return to the editor at the same place you were when you did the ctrl Z. You can make new edits, or even "undo" or "redo" previous edits. Note: on Linux boxes, when you ctrl Z from the editor, your screen returns to the command line output that was visible when you entered the editor, such as your list of compiler error messages. Unfortunately, on the cisunix alpha machines, this doesn't work - your screen shows the remains of the editor's last screen.