|
Creating and Editing Files |
|
By Augie Hansen Much of the time and energy you spend working on any computer system involves creating and editing text tiles. Office workers use editors and word processors to create the basics that make a business work: memos, letters, and dreaded meeting notices. Programmers use editors to create specification and design documents, program source files, and, if encouraged or threatened, support program documentation. Virtually everyone in a computer-equipped organization can benefit from electronic mail, which also involves editing. You need to master the fundamentals of editing early in your training because almost everything else you will learn depends to some extent on your ability to create and maintain text files. In a UNIX environment, a text file is a disk file that contains only ordinary characters: letters, digits, punctuation marks, and a few control characters. These characters are represented within a computer as numeric codes that are defined by the American Standard Code for Information Interchange (ASCII) or by some other codification scheme. You create text files by using a text editor. A text editor is not a word processor. A word processing program combines two tasks-text editing and text processing-into one program. Several word processing programs are available for UNIX systems, but none is provided as a standard component of a UNIX system. The UNIX system offers a ``divide and conquer'' approach to doing document preparation. In this paradigm, you use an editing tool to create files that contain both text and formatting codes. The codes are interpreted by a separate text processing program, such as Nroff (pronounced en-roff), which applies the requested character, paragraph, and other formatting to printed and displayed output. Most word processing programs use special non-ASCII codes to represent formatting information, so they are usually unsuitable for preparing standard text files. Some word processors provide a way for you to save files in pure text form, and they can be used as text editors. Visual EditingYou should learn to use at least one of the supported UNIX editors. Your choices are Ed, Ex, and Vi, which are all available on most UNIX systems. Last month we looked briefly at the line-oriented editor Ed and the process of creating and saving a file. This month we will explore the primary editing features of Vi, the officially supported UNIX full-screen visual editor. We mention the popular public-domain Emacs editor briefly at the end. First you will learn about the initial setup required to use Vi and how to start the editor from the UNIX command line. Then you will learn how to enter text, how to perform the basics of selecting and editing text, and how to save your work and quit editing. The following material tells you the minimum amount of information you would know to start using the Vi editor. When you have become reasonably proficient in using this subset of visual editing features, you can investigate other Vi features that will help you do more complex editing tasks. You can also learn some of the many shortcuts that will help you work more efficiently. Initial SetupBeing a visual editor, Vi must learn how to work with your
terminal. The UNIX system stores information needed by the shell
and application programs in environment variables. One of those
environment variables is Your system administrator may have already set the
Note that the angle brackets surrounding the letters ``CR'' indicate that the letters represent a special key on the keyboard rather than a sequence of individual letters. In particular <CR> means press the carriage return key. Later we will use <ESC> to represent the escape key. If If Look up the ID for your terminal in the user manuals or get
the required information from your system administrator. The
purpose of the To avoid having to key in the Starting the EditorTo start the Vi editor, simply type its name as a command.
You can also provide a file name or a list of file names as
arguments. Let's create a file called This command starts the Vi editor and attempts to read in the
file The editing buffer is an area in the computer's memory where your text input and editing are maintained. All text input and editing actions affect the editing buffer in some way. At any time, all or portion of the editing buffer may be displayed on your terminal screen. The current position in the editing buffer is indicateed by the cursor. Text input and editing operations usually take place at or near the cursor position. The Vi editor usually keeps the cursor in view in the editing window on your screen. Some commands, such as global search and replace, have effects that may not be visible on the screen. Creating FilesYou can append text by using the append ( Let's append a quote from Mark Twain to the file
"Work consists of whatever a
body is obliged to do...
Play consists of whatever a
body is not obliged to do."
Mark Twain
End the text-input operation by pressing the Escape key. Following the append command, the cursor rests on the last text character that you typed into the editing buffer. Listing 1A shows how the screen looks after append operation. Another command that puts text in the editing buffer is the
insert ( Now let's insert a set of title lines at the beginning of the buffer. To do so, we must move the cursor, which is now on line 6, back to the first like in the buffer. To move quickly to any line in the editing buffer, type the
line number followed by the ``goto'' ( Table 2 contains a summary of the primary cursor-positioning commands. Each of the commands can take a preceding count that effectively multiplies the effective range of the action. For example, typing ``5j'' moves the cursor down five lines in the editing buffer, scrolling the window if necessary to keep the cursor in view. If there are not enough characters or lines to satisfy the request, the action is ignored, and the terminal bell sounds. If your terminal has arrow keys, you can probably use them to
move the cursor by column and lines. However, some terminal (HP
2621, for example) require that you press the Shift key at the
same time as the function keys to get the required effect. Many
video terminals don't have function keys or arrow keys. Now type
the insert command, FAMOUS QUOTATIONS ----------------- Terminate the insert operation by pressing the Escape key. The insert operation puts three additional lines in the file ahead of the previously entered text. Following the insert operation, the screen appears as shown in Listing 1B. Saving Text in a FileAfter you have inserted or appended text to the editing
buffer, you should save it to disk by using a write
( For this example, the command is the write command, to which
Vi responds: You can also save the editing buffer to a different file by
using a command of the form Editing TextThe Vi editor is designed around a simple concept. You issue editing commands that specify an action and an object upon which to perform the action. Table 3A lists some of the more important actions, and Table 3B lists some of the more frequently used objects. Forming commands from the actions and objects, you can delete,
change, and substitute text easily. To delete the current word,
type the command Doubling the action letter causes the action to affect entire
lines. Thus Experiment on a file to see how the various actions and objects work together to let you edit text. Don't forget to use repetition counts to multiply the effects of commands. One last command and we'll call it a month. To quit the
editor, use the quit ( Although this month's look at Vi has covered only a small percentage of the editor's commands, it should be sufficient to get you started. In future installments of this column, I will touch on other aspects of visual and line-oriented editing in the context of other UNIX topics. |
Print This Page Send as e-mail |












